forked from anirudhagar13/chicago-crime-analysis-bigdata
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrest_client.py
More file actions
50 lines (38 loc) · 1.31 KB
/
rest_client.py
File metadata and controls
50 lines (38 loc) · 1.31 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
# REST API Client
import requests
class API_client:
def __init__(self, url, username, password):
self._url = url
self._auth = (username, password)
def set_url(self, url):
'''
to reset url
'''
self._url = url
def get_response(self):
'''
get response from url
'''
resp = requests.get(self._url, auth=self._auth)
if resp.status_code != 200:
# This means something went wrong.
raise ApiError('GET rest API issue: {}'.format(resp.status_code))
return resp.json()
def put_response(self, data):
'''
put response
'''
resp = requests.post(self._url, auth=self._auth, json=data)
if resp.status_code != 201:
raise ApiError('POST rest API issue: {}'.format(resp.status_code))
return 'POST successful'
def __str__(self):
return 'This is a REST API functions wrapper'
if __name__ == '__main__':
# Code to test above class
host_name = 'http://localhost:8091'
url = '/pools/default/buckets'
# Instantiating rest client class
rest_client = API_client(host_name+url, 'admin', 'password')
# Getting response
print (rest_client.get_response())