-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest.py
183 lines (132 loc) · 5.81 KB
/
rest.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# -*- coding: utf-8 -*-
'''Python module for initiating and executing commands via REST API.'''
# pylint: disable=too-many-branches, too-many-statements
# pyling: disable=too-many-return-statements
import ujson as json
import requests
from constants import CREDS
from constants import API
from logger import CustomLogger
LOG = CustomLogger(__name__)
class REST(object):
'''Rest class for invoking REST calls GET, POST, PUT, PATCH, DELETE.'''
def __init__(self, **kwargs):
'''This class defines methods to invoke REST calls.
Args:
:pcIP (str): IP address.
:username (str, optional): Username for auth. Default: 'admin'.
:password (str, optional): Passwd for auth. Default: 'Password'.
:port (int, optional): Port for sending REST calls. Default: 80.
:baseURL (str, optional): URI for REST calls. Default: .
Returns:
Returns REST object instance.
Raises: NA
'''
self.username = kwargs.pop('username', CREDS.USERNAME)
self.password = kwargs.pop('password', CREDS.DEFAULT_PASSWORD)
self.port = kwargs.pop('port', CREDS.PORT)
self.ip = kwargs.pop('pcIP', CREDS.IP)
if not self.ip:
raise NameError('IP address {} not set'.format(self.ip))
# Disable HTTPS certificate warning.
requests.packages.urllib3.disable_warnings()
def post(self, relative_url, **kwargs):
'''This routine is used to invoke POST call for REST API.
Args:
:relative_url(str): Relative URL for the particular API call.
:kwargs headers(str, optional): Custom headers for REST call.
:kwargs payload (str, optional): payload to be send for REST call.
Returns:
str: response text.
'''
kwargs['operation'] = 'post'
return self.__performOperation(relative_url, **kwargs)
def get(self, relative_url, **kwargs):
'''This routine is used to invoke GET call for REST API.
Args:
:relative_url(str): Relative URL for the particular API call.
:kwargs headers(str, optional): Custom headers for REST call.
:kwargs payload (str, optional): payload to be send for REST call.
Returns:
str: response text.
'''
kwargs['operation'] = 'get'
return self.__performOperation(relative_url, **kwargs)
def patch(self, relative_url, **kwargs):
'''This routine is used to invoke PATCH call for REST API.
Args:
:relative_url(str): Relative URL for the particular API call.
:kwargs headers(str, optional): Custom headers for REST call.
:kwargs payload (str, optional): payload to be send for REST call.
Returns:
str: response text.
'''
kwargs['operation'] = 'patch'
return self.__performOperation(relative_url, **kwargs)
def put(self, relative_url, **kwargs):
'''This routine is used to invoke PUT call for REST API.
Args:
:relative_url(str): Relative URL for the particular API call.
:kwargs headers(str, optional): Custom headers for REST call.
:kwargs payload (str, optional): payload to be send for REST call.
Returns:
str: response text.
'''
kwargs['operation'] = 'put'
return self.__performOperation(relative_url, **kwargs)
def delete(self, relative_url, **kwargs):
'''This routine is used to invoke DELETE call for REST API.
Args:
:relative_url (str): Relative URL for the particular API call.
:kwargs headers (str, optional): Custom headers for REST call.
:kwargs payload (str, optional): payload to be send for REST call.
Returns:
str: response text.
'''
kwargs['operation'] = 'delete'
return self.__performOperation(relative_url, **kwargs)
def __performOperation(self, relative_url, **kwargs):
'''
Private Method to perform ops post, get, patch, delete and put.
Args:
:relative_url (str): Relative url
Returns:
str: Response text.
Raises: Exception
'''
apiVersion = kwargs.pop('apiVersion', API.DEFAULT)
if apiVersion not in API.VERSION_URL_MAP.keys():
raise ValueError('apiVersion {} is invalid'.format(apiVersion))
URL = API.VERSION_URL_MAP.get(apiVersion)
self.baseURL = 'https://{0}:{1}{2}'.format(
self.ip, self.port, URL
)
mainURI = '{0}{1}'.format(self.baseURL, relative_url)
headers = kwargs.pop('headers', {'content-type': 'application/json'})
verify = kwargs.pop('verify', False)
payload = kwargs.pop('payload', {})
payload = json.dumps(payload, indent=4) if payload else {}
auth = (self.username, self.password)
timeout = kwargs.pop('timeout', 480)
stream = kwargs.pop('stream', False)
params = kwargs.pop('params', {})
operation = kwargs.pop('operation')
methodToCall = getattr(requests, operation)
LOG.url('[{0}]: {1}'.format(LOG.yellow(operation.upper()), mainURI))
if payload:
LOG.payload("{}".format(payload))
response = methodToCall(
mainURI, headers=headers, verify=verify, data=payload,
auth=auth, timeout=timeout, stream=stream, params=params
)
LOG.status(response.status_code)
# raise exception on failure
response.raise_for_status()
if response.status_code != 204 and not stream:
LOG.response(json.dumps(json.loads(response.content), indent=4))
if stream:
return response
elif response.text is not u'':
return response.json()
else:
return response.text