def construct(self): """ Construct and return the API connection and parameters objects. """ # Require an API key or token if not self.api_key and not self.api_token: error_response('Must supply either an API key or a token to make a request', cli=self.cli) # Retrieve a token if not supplied if not self._get_token(): error_response('Failed to retrieve API token', response=self.token_rsp, cli=self.cli) # API connector parameters self.params = { 'user': self.api_user, 'group': self.api_group, 'token': self.api_token, 'url': self.api_url, 'key': self.api_key } # Return the API client return APIBase( user = self.api_user, group = self.api_group, token = self.api_token, url = self.api_url, cli = self.cli ), self.params
def _return(self, response): """ Parse and return the response. """ # Parse the response parsed = parse_response(response) # Parse the body try: parsed['body'] = json.loads(parsed['body']) except: pass # If there was an error during the request if parsed['code'] != 200: error_response(parsed['body']['message'], response=parsed, cli=self.cli) # Return a successfull response return parsed