client.py 2.31 KB
Newer Older
1
import logging
Jo Sama's avatar
Jo Sama committed
2
3
import requests

Jo Sama's avatar
Jo Sama committed
4
from urllib.parse import urljoin, urlparse
Jo Sama's avatar
Jo Sama committed
5

Jo Sama's avatar
Jo Sama committed
6
from .model import Operator
Jo Sama's avatar
Jo Sama committed
7

8
9
logger = logging.getLogger(__name__)

Jo Sama's avatar
Jo Sama committed
10
11
12
13
14
15
16
17

class Endpoints:
    def __init__(self, base_url):
        self.base_url = base_url

    def _prepend_base_url(self, path):
        return urljoin(self.base_url, path)

Jo Sama's avatar
Jo Sama committed
18
19
    def get_operator(self, identity):
        return urljoin(self._prepend_base_url('/operators/'), identity)
Jo Sama's avatar
Jo Sama committed
20

Jo Sama's avatar
Jo Sama committed
21
22
23
    def get_operators(self):
        return self._prepend_base_url('/operators/')

Jo Sama's avatar
Jo Sama committed
24

Jo Sama's avatar
Jo Sama committed
25
class TopDeskClient:
Jo Sama's avatar
Jo Sama committed
26
27
    def __init__(self, url, headers=None, rewrite_url=None):
        self.urls = Endpoints(url)
28
29
30
31
32
33
34
35
36
37
        self.rewrite_url = rewrite_url
        self.headers = headers

    def _build_headers(self, headers):
        request_headers = {}
        for h in (self.headers):
            request_headers[h] = self.headers[h]
        for h in (headers or ()):
            request_headers[h] = headers[h]
        return request_headers
Jo Sama's avatar
Jo Sama committed
38

39
40
41
42
43
44
45
46
47
48
    def call(self,
             method_name,
             url,
             headers=None,
             params=None,
             **kwargs):
        logger.debug('Calling %s %s with params=%r',
                     method_name,
                     urlparse(url).path,
                     params)
Jo Sama's avatar
Jo Sama committed
49
        r = requests.request(method_name,
Jo Sama's avatar
Jo Sama committed
50
                             (url if self.rewrite_url is None
51
                              else url.replace(*self.rewrite_url)),
Jo Sama's avatar
Jo Sama committed
52
53
                             headers=(None if headers is None else
                                      self._build_headers(headers)),
54
                             params=params if params is not None else {},
Jo Sama's avatar
Jo Sama committed
55
                             **kwargs)
56
        logger.debug('GOT HTTP %d: %r', r.status_code, r.content)
Jo Sama's avatar
Jo Sama committed
57
58
59
60
61
62
63
64
65
66
67
68
69
        r.raise_for_status()
        return r.json()

    def post(self, url, **kwargs):
        return self.call('POST', url, **kwargs)

    def get(self, url, **kwargs):
        return self.call('GET', url, **kwargs)

    def put(self, url, **kwargs):
        return self.call('PUT', url, **kwargs)

    def get_operator(self, identity):
Jo Sama's avatar
Jo Sama committed
70
71
        url = self.urls.get_operator(identity)
        return Operator.from_dict(self.get(url))
Jo Sama's avatar
Jo Sama committed
72
73

    def list_operators(self):
Jo Sama's avatar
Jo Sama committed
74
75
        for x in self.get(self.urls.get_operators()):
            yield Operator.from_dict(x)
Jo Sama's avatar
Jo Sama committed
76
77
78
79


def get_client(config):
    return TopDeskClient(**config)