3 # Copyright (c) 2017 IXIA and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
15 okStates = [200, 201, 202]
19 'Manual Step Required',
28 notStartedState = 'Not_Started'
29 errorStates = ['Error', 'Aborted', 'Force Continue']
30 finishedStates = ['Manual Step Required', 'Finished']
32 logger = logging.getLogger(__name__)
35 class TestFailedError(Exception):
41 def postWithPayload(loginUrl, payload=None):
42 urlHeadersJson = {'content-type': 'application/json'}
44 h = httplib2.Http('.cache',
45 disable_ssl_certificate_validation=True)
47 logger.debug('POST: ' + loginUrl)
48 (response, content) = h.request(loginUrl, 'POST', '',
52 logger.debug('POST: ' + loginUrl + ' <- Data: ' + str(payload))
53 (response, content) = h.request(loginUrl, 'POST',
55 headers=urlHeadersJson)
56 logger.debug(response)
59 raise Exception('Got an error code: ', e)
63 def postWithPayloadAndHeaders(loginUrl, urlHeadersJson,
66 h = httplib2.Http('.cache',
67 disable_ssl_certificate_validation=True)
69 logger.debug('POST: ' + loginUrl)
70 (response, content) = h.request(loginUrl, 'POST', '',
73 logger.debug('POST: ' + loginUrl + ' <- Data: ' + str(payload))
74 (response, content) = h.request(loginUrl, 'POST',
76 headers=urlHeadersJson)
78 raise Exception('Got an error code: ', e)
82 def postOperation(url, apiKey, payload=''):
83 urlHeadersJson = {'content-type': 'application/json',
84 'X-Api-Key': '%s' % str(apiKey)}
86 h = httplib2.Http('.cache',
87 disable_ssl_certificate_validation=True)
89 logger.debug('POST: ' + url)
90 (response, content) = h.request(url, 'POST',
94 logger.debug('POST: ' + url + ' <- Data: ' + str(payload))
95 (response, content) = h.request(url, 'POST',
97 headers=urlHeadersJson)
99 raise Exception('Got an error code: ', e)
103 def patch(url, payload, apiKey):
104 urlHeadersJson = {'content-type': 'application/json',
105 'X-Api-Key': '%s' % str(apiKey)}
107 h = httplib2.Http('.cache',
108 disable_ssl_certificate_validation=True)
109 logger.debug('PATCH: ' + url + ' <-- Attribute: ' +
111 (response, content) = h.request(url, 'PATCH',
116 # print (response, content)
118 raise Exception('Got an error code: ', e)
122 def delete(url, apiKey):
123 urlHeadersJson = {'content-type': 'application/json',
124 'X-Api-Key': '%s' % str(apiKey)}
126 h = httplib2.Http('.cache',
127 disable_ssl_certificate_validation=True)
128 (response, content) = h.request(url, 'DELETE', '', urlHeadersJson)
129 logger.debug('DELETE: ' + url)
131 raise Exception('Got an error code: ', e)
132 if response.status not in okStates:
133 raise TestFailedError(json.loads(content)['error'])
134 return json.loads(content)
137 def getWithHeaders(url, apiKey):
138 urlHeadersJson = {'content-type': 'application/json',
139 'X-Api-Key': '%s' % str(apiKey)}
141 h = httplib2.Http('.cache',
142 disable_ssl_certificate_validation=True)
143 logger.debug('GET: ' + url)
144 (response, content) = h.request(url, 'GET', '', urlHeadersJson)
146 raise Exception('Got an error code: ', e)
147 if response.status not in okStates:
148 raise TestFailedError(json.loads(content)['error'])
149 output = json.loads(content)