def getoffsets_sub(self, sub, offset='all', **reqkwargs): """ Retrieve the current positions of min,max and current offsets. Args: sub (str): The subscription name. offset(str): The name of the offset.If not specified, it will return all three of them as a dict. Kwargs: reqkwargs: keyword argument that will be passed to underlying python-requests library call. """ route = self.routes["sub_offsets"] # Compose url url = route[1].format(self.endpoint, self.token, self.project, sub) method = getattr(self, 'do_{0}'.format(route[0])) r = method(url, "sub_offsets", **reqkwargs) try: if offset != 'all': return r[offset] return r except KeyError as e: errormsg = { 'error': { 'message': str(e) + " is not valid offset position" } } raise AmsServiceException(json=errormsg, request="sub_offsets")
def auth_via_cert(self, cert, key, **reqkwargs): """ Retrieve an ams token based on the provided certificate Args: cert(str): a path to a valid certificate file key(str): a path to the associated key file for the provided certificate Kwargs: reqkwargs: keyword argument that will be passed to underlying python-requests library call. """ if cert == "" and key == "": errord = { "error": { "code": 400, "message": "No certificate provided." } } raise AmsServiceException(json=errord, request="auth_x509") # create the certificate tuple needed by the requests library reqkwargs = {"cert": (cert, key)} route = self.routes["auth_x509"] # Compose url url = route[1].format(self.endpoint, self.authn_port) method = getattr(self, 'do_{0}'.format(route[0])) try: r = method(url, "auth_x509", **reqkwargs) # if the `token` field was not found in the response, raise an error if "token" not in r: errord = { "error": { "code": 500, "message": "Token was not found in the response.Response: " + str(r) } } raise AmsServiceException(json=errord, request="auth_x509") return r["token"] except (AmsServiceException, AmsConnectionException) as e: raise e
def _make_request(self, url, body=None, route_name=None, **reqkwargs): """Common method for PUT, GET, POST HTTP requests with appropriate service error handling. For known error HTTP statuses, returned JSON will be used as exception error message, otherwise assume and build one from response content string. """ m = self.routes[route_name][0] decoded = None try: # the get request based on requests. reqmethod = getattr(requests, m) r = reqmethod(url, data=body, **reqkwargs) if r.status_code == 200: decoded = json.loads(r.content) if r.content else {} # JSON error returned by AMS elif r.status_code != 200 and r.status_code in self.errors_route[ route_name][1]: decoded = json.loads(r.content) if r.content else {} raise AmsServiceException(json=decoded, request=route_name) # handle other erroneous behaviour and construct error message from # JSON or plaintext content in response elif r.status_code != 200 and r.status_code not in self.errors_route[ route_name][1]: try: errormsg = json.loads(r.content) except ValueError: errormsg = { 'error': { 'code': r.status_code, 'message': r.content } } raise AmsServiceException(json=errormsg, request=route_name) except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as e: raise AmsConnectionException(e, route_name) else: return decoded if decoded else {}
def do_delete(self, url, route_name, **reqkwargs): """Delete method that is used to make the appropriate request. Used for (topics, subscriptions). Args: url: str. The final messaging service endpoint route_name: str. The name of the route to follow selected from the route list reqkwargs: keyword argument that will be passed to underlying python-requests library call. """ # try to send a delete request to the messaging service. # if a connection problem araises a Connection error exception is raised. m = self.routes[route_name][0] try: # the delete request based on requests. r = requests.delete(url, **reqkwargs) # JSON error returned by AMS if r.status_code != 200 and r.status_code in self.errors[m]: decoded = json.loads(r.content) if r.content else {} raise AmsServiceException(json=decoded, request=route_name) # handle other erroneous behaviour elif r.status_code != 200 and r.status_code not in self.errors[m]: errormsg = { 'error': { 'code': r.status_code, 'message': r.content } } raise AmsServiceException(json=errormsg, request=route_name) else: return True except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as e: raise AmsConnectionException(e, route_name)