-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbase.py
38 lines (31 loc) · 1014 Bytes
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import json
import requests
import logging
logging.basicConfig(level=logging.INFO)
logging.info("Logger initialized.")
"""Knack API requests and responses wrapper/proxy.
Objective: have a simple way of sharing and using access tokens
and serializing/deserializing data to/from the API.
"""
class Base(object):
application_id = None
rest_api_key = None
_session = None
def make_request(klass, url, data=None):
logging.debug("GET {url} - data: {data}".format(url=str(url), data=str(data)))
response = klass._session.get(url)
try:
return response.json()
except:
logging.info(response)
raise
@classmethod
def setup(klass, application_id, rest_api_key):
klass.rest_api_key = rest_api_key
klass.application_id = application_id
klass.headers = {
'X-Knack-Application-Id': klass.application_id,
'X-Knack-REST-API-Key': klass.rest_api_key
}
klass._session = requests.Session()
klass._session.headers.update(klass.headers)