-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsession.py
More file actions
158 lines (130 loc) · 4.89 KB
/
session.py
File metadata and controls
158 lines (130 loc) · 4.89 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# :coding: utf-8
# :copyright: Copyright (c) 2021 Lluis Casals Marsol
# Please see the LICENSE file that should have been included as part of this
# package.
import os
import json
import logging
from functools import partial
import requests
class Session(object):
'''Attach session authentication information to requests.'''
@property
def server_url(self):
'''Return server url used for session.'''
return self._server_url
@property
def api_key(self):
'''Return API key used for session.'''
return self._api_key
def __init__(
self, server_url='https://api.covalenthq.com',
api_key=None,
api_key_is_username=True,
timeout=60
):
'''
Initialize Session with the given *server_url* and *api_key*
:param server_url: Default covalent api url: https://api.covalenthq.com
If None, will check for envar:: COVALENT_SERVER environment variable.
:type server_url: string
:param api_key: Personal covalent API key. If None check for
envar:: COVALENT_API_KEY environment variable.
:type api_key: string
:param timeout: Timeout in case the server is not responding.
:type api_key_is_username: bool
:param api_key_is_username: If True, the string in api_key is the username of the request auth, rather than the password
This is how covalent does Basic Auth per https://www.covalenthq.com/docs/api/#overview--supported-networks
:type timeout: float
'''
super(Session, self).__init__()
logging.basicConfig()
self.logger = logging.getLogger(
__name__ + '.' + self.__class__.__name__
)
self.logger.setLevel(logging.INFO)
#If no server_url provided check the environment variable.
if server_url is None:
server_url = os.environ.get('COVALENT_SERVER')
if not server_url:
raise TypeError(
'Required "server_url" not specified. Pass as argument or set '
'in environment variable COVALENT_SERVER.'
)
self._server_url = server_url
#If no api_key provided check the environment variable.
if api_key is None:
api_key = os.environ.get('COVALENT_API_KEY')
if not api_key:
raise TypeError(
'Required "api_key" not specified. Pass as argument or set in '
'environment variable COVALENT_API_KEY.'
)
self._api_key = api_key
self._request = requests.Session()
if not api_key_is_username:
self._request.auth = ('', self._api_key)
else:
self._request.auth = (self._api_key, '')
self.request_timeout = timeout
def decode(self, string):
'''
Return decoded JSON *string* as Python object.
'''
return json.loads(string)
def encode(self, data):
'''Return encoded *data* as JSON'''
return json.dumps(
data,
sort_keys=True
)
def _check_params(self, params):
'''
Check given *params* to remove invalid ones.
:param params: Dictionary with parameters.
:type params: dictionary
'''
if not isinstance(params, dict):
raise Exception("params should be a dicctionary")
params_to_pop = []
for k, v in params.items():
if v==None:
params_to_pop.append(k)
for param in params_to_pop:
params.pop(param)
return params
def query(self, url, params=None, decode=True):
'''
Query the *url* request with the given *params*
:param url: path url to query.
:type url: string
:param params: Dictionary with url parameters
:type params: dictionary
:param decode: Json decode the returned response from the server.
True by default.
:type decode: boolean
'''
url = "{}{}?&key={}".format(self._server_url, url, self.api_key)
self.logger.debug("Url: {}".format(url))
#params = self.encode(params)
params = self._check_params(params)
self.logger.debug("Parameters: {}".format(params))
response = self._request.get(
url,
params=params,
timeout=self.request_timeout
)
result = response.text
if not result:
return result
if decode:
try:
result = self.decode(result)
except Exception as e:
error_message = (
'Server reported error in unexpected format. '
'Raw error was: {0}. \n Exception: {1}'.format(response.text, e)
)
self.logger.exception(error_message)
raise Exception(error_message)
return result