-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqualtrics_api_base.py
More file actions
66 lines (56 loc) · 2.67 KB
/
qualtrics_api_base.py
File metadata and controls
66 lines (56 loc) · 2.67 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
import requests
from requests.exceptions import HTTPError
import time
class QualtricsAPIBase(object):
def __init__(self, api_token, data_center, retries=0):
if not api_token:
raise ValueError("error: api_token must be set before making API calls")
if not data_center:
raise ValueError("error: data_center must be set before making API calls")
self.api_token = api_token
self.data_center = data_center
self._max_request_attempts = retries + 1
def get_base_url(self):
return "https://" + self.data_center + ".qualtrics.com/API/v3/"
def get_headers(self):
return {"X-API-TOKEN": self.api_token,
"Content-Type": "application/json"}
def get(self, url, **kwargs):
return self._execute_request('get', url, **kwargs)
def post(self, url, **kwargs):
return self._execute_request('post', url, **kwargs)
def put(self, url, **kwargs):
return self._execute_request('put', url, **kwargs)
def delete(self, url, **kwargs):
return self._execute_request('delete', url, **kwargs)
def _execute_request(self, request_type, url, **kwargs):
if request_type not in ['get', 'post', 'put', 'delete']:
raise TypeError('_execute_request request_type must be get, post, put, or delete')
for i in xrange(self._max_request_attempts):
try:
response = None
if request_type == 'get':
response = requests.get(url, **kwargs)
elif request_type == 'post':
response = requests.post(url, **kwargs)
elif request_type == 'put':
response = requests.put(url, **kwargs)
else:
response = requests.delete(url, **kwargs)
QualtricsAPIBase._check_for_status(response)
return response
except HTTPError as e:
if i == self._max_request_attempts - 1:
raise
time.sleep(2)
@staticmethod
def _check_for_status(response):
if not response.status_code == 200:
msg = 'Status Code {} '\
'for {} request on {} '\
'with Body: ({}) and Headers: ({})'.format(response.status_code,
response.request.method,
response.request.url,
response.request.body,
response.request.headers)
raise HTTPError(msg)