Skip to content

Commit 813e489

Browse files
committed
Added authentication API calls, changed general API call method into a general API post method.
1 parent b3a1d05 commit 813e489

File tree

5 files changed

+83
-44
lines changed

5 files changed

+83
-44
lines changed

demandapi/__init__.py

Whitespace-only changes.

demandapi/api.py

Lines changed: 0 additions & 44 deletions
This file was deleted.

dynatademand/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from .api import DemandAPIClient # noqa
2+
from .errors import DemandAPIError # noqa

dynatademand/api.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import os
2+
import requests
3+
from .errors import DemandAPIError
4+
5+
6+
class DemandAPIClient(object):
7+
def __init__(self):
8+
self.client_id = os.getenv('DYNATA_DEMAND_CLIENT_ID', None)
9+
self.username = os.getenv('DYNATA_DEMAND_USERNAME', None)
10+
self.password = os.getenv('DYNATA_DEMAND_PASSWORD', None)
11+
if None in [self.client_id, self.username, self.password]:
12+
raise DemandAPIError("All authentication data is required.")
13+
self._access_token = None
14+
self._refresh_token = None
15+
self.base_host = os.getenv('DYNATA_DEMAND_BASE_URL', 'https://api.researchnow.com')
16+
self.auth_base_url = '{}/auth/v1'.format(self.base_host)
17+
self.base_url = '{}/sample/v1'.format(self.base_host)
18+
19+
def _check_authentication(self):
20+
if self._access_token is None:
21+
raise DemandAPIError('The API instance must be authenticated before calling this method.')
22+
23+
def _api_post(self, uri, payload):
24+
# Send an authenticated POST request to an API endpoint.
25+
self._check_authentication()
26+
url = '{}{}'.format(self.base_url, uri)
27+
request_headers = {
28+
'oauth_access_token': self._access_token,
29+
'Content-Type': "application/json",
30+
}
31+
response = requests.post(url=url, json=payload, headers=request_headers)
32+
if response.status_code > 399:
33+
raise DemandAPIError('Demand API request to {} failed with status {}. Response: {}'.format(
34+
url, response.status_code, response.content
35+
))
36+
return response.json()
37+
38+
def authenticate(self):
39+
url = '{}/token/password'.format(self.auth_base_url)
40+
auth_response = requests.post(url, json={
41+
'clientId': self.client_id,
42+
'password': self.password,
43+
'username': self.username,
44+
})
45+
if auth_response.status_code > 399:
46+
raise DemandAPIError('Authentication failed with status {} and error: {}'.format(
47+
auth_response.status_code,
48+
auth_response.json())
49+
)
50+
response_data = auth_response.json()
51+
self._access_token = response_data.get('accessToken')
52+
self._refresh_token = response_data.get('refreshToken')
53+
return response_data
54+
55+
def refresh_access_token(self):
56+
url = '{}/token/refresh'.format(self.auth_base_url)
57+
refresh_response = requests.post(url, json={
58+
'clientId': self.client_id,
59+
'refreshToken': self._refresh_token
60+
})
61+
if refresh_response.status_code != 200:
62+
raise DemandAPIError("Refreshing Access Token failed with status {} and error: {}".format(
63+
refresh_response.status_code, refresh_response.content
64+
))
65+
response_data = refresh_response.json()
66+
self._access_token = response_data.get('accessToken')
67+
self._refresh_token = response_data.get('refreshToken')
68+
return response_data
69+
70+
def logout(self):
71+
url = '{}/logout'.format(self.auth_base_url)
72+
logout_response = requests.post(url, json={
73+
'clientId': self.client_id,
74+
'refreshToken': self._refresh_token,
75+
'accessToken': self._access_token
76+
})
77+
if logout_response.status_code != 204:
78+
raise DemandAPIError("Log out failed with status {} and error: {}".format(
79+
logout_response.status_code, logout_response.content
80+
))
81+
return logout_response.json()
File renamed without changes.

0 commit comments

Comments
 (0)