Skip to content
This repository has been archived by the owner on Oct 20, 2021. It is now read-only.

Commit

Permalink
Extract Clubhouse Client
Browse files Browse the repository at this point in the history
  • Loading branch information
j-martin committed Oct 29, 2018
1 parent e53c686 commit c7a86f4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 30 deletions.
47 changes: 47 additions & 0 deletions clubhouse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import logging

from os import path
from typing import Dict
from urllib.parse import urlparse

import requests

# Using beta api to support External Tickets
API_ENDPOINT = 'https://api.clubhouse.io/api/beta'

ClubhouseStory = Dict[str, object]
ClubhouseUser = Dict[str, object]
ClubhouseComment = Dict[str, str]
ClubhouseTask = Dict[str, str]
ClubhouseLabel = Dict[str, str]
ClubhouseFile = Dict[str, str]

logger = logging.getLogger('clubhouse')


class ClubhouseClient(object):
def __init__(self, api_key):
self.api_key = api_key

def get(self, *segments, **kwargs):
return self._request('get', *segments, **kwargs)

def post(self, *segments, **kwargs):
return self._request('post', *segments, **kwargs)

def put(self, *segments, **kwargs):
return self._request('put', *segments, **kwargs)

def delete(self, *segments, **kwargs):
return self._request('delete', *segments, **kwargs)

def _request(self, method, *segments, **kwargs):
url = path.join(API_ENDPOINT, *[str(s) for s in segments])
prefix = "&" if urlparse(url)[4] else "?"
response = requests.request(method, url + f"{prefix}token={self.api_key}", **kwargs)
if response.status_code > 299:
logger.error(f"Status code: {response.status_code}, Content: {response.text}")
response.raise_for_status()
if response.status_code == 204:
return {}
return response.json()
33 changes: 3 additions & 30 deletions importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,15 @@
import requests
from binaryornot import check
from jinja2 import Template
from marshmallow.compat import urlparse

from clubhouse import ClubhouseClient, ClubhouseFile, ClubhouseComment, ClubhouseTask, \
ClubhouseStory, ClubhouseLabel, ClubhouseUser

logger = logging.getLogger('importer')

T = TypeVar('T')
AsanaTask = Dict
AsanaUser = Dict
ClubhouseStory = Dict[str, object]
ClubhouseUser = Dict[str, object]
ClubhouseComment = Dict[str, str]
ClubhouseTask = Dict[str, str]
ClubhouseLabel = Dict[str, str]
ClubhouseFile = Dict[str, str]


class ClubhouseClient(object):
def __init__(self, api_key):
self.api_key = api_key

def get(self, *segments, **kwargs):
return self._request('get', *segments, **kwargs)

def post(self, *segments, **kwargs):
return self._request('post', *segments, **kwargs)

def _request(self, method, *segments, **kwargs):
# Using beta api to support External Tickets
url = path.join('https://api.clubhouse.io/api/beta', *[str(s) for s in segments])
prefix = "&" if urlparse.urlparse(url)[4] else "?"
response = requests.request(method, url + f"{prefix}token={self.api_key}", **kwargs)
if response.status_code not in [200, 201]:
logger.error(f"Status code: {response.status_code}, Content: {response.text}")
response.raise_for_status()
return response.json()


description_template = Template("""
{{ notes|trim }}
Expand Down

0 comments on commit c7a86f4

Please sign in to comment.