py_jama_rest_client.core
1import math 2 3import requests 4import time 5import logging 6 7__DEBUG__ = False 8 9py_jama_rest_client_logger = logging.getLogger('py_jama_rest_client-core') 10 11 12class CoreException(Exception): 13 """This is the base class for all exceptions raised by the Core""" 14 15 def __init__(self, message, status_code=None, reason=None): 16 super(CoreException, self).__init__(message) 17 self.status_code = status_code 18 self.reason = reason 19 20 21class UnauthorizedTokenException(CoreException): 22 """This exception is thrown whenever fetching the oauth token returns a 401 unauthorized response.""" 23 pass 24 25 26class Core: 27 """ This Class will contain a collection of methods that interact directly with the Jama API and return A Requests 28 Response Object. This class will give the user more fine grained access to the JAMA API. For more information 29 on the Requests library visit: http://docs.python-requests.org/en/master/""" 30 31 def __init__(self, host_name, user_credentials, api_version='/rest/v1/', oauth=False, verify=True): 32 # Instance variables 33 self.__api_version = api_version 34 self.__host_name = host_name + self.__api_version 35 self.__credentials = user_credentials 36 self.__oauth = oauth 37 self.__verify = verify 38 self.__session = requests.Session() 39 40 # Setup OAuth if needed. 41 if self.__oauth: 42 self.__token_host = host_name + '/rest/oauth/token' 43 self.__token = None 44 self.__get_fresh_token() 45 46 def delete(self, resource, **kwargs): 47 """ This method will perform a delete operation on the specified resource""" 48 url = self.__host_name + resource 49 kwargs['verify'] = self.__verify 50 51 if self.__oauth: 52 self.__check_oauth_token() 53 kwargs['headers'] = self.__add_auth_header(**kwargs) 54 return self.__session.delete(url, **kwargs) 55 56 return self.__session.delete(url, auth=self.__credentials, **kwargs) 57 58 def get(self, resource, params=None, **kwargs): 59 """ This method will perform a get operation on the specified resource""" 60 url = self.__host_name + resource 61 kwargs['verify'] = self.__verify 62 63 if self.__oauth: 64 self.__check_oauth_token() 65 kwargs['headers'] = self.__add_auth_header(**kwargs) 66 return self.__session.get(url, params=params, **kwargs) 67 68 return self.__session.get(url, auth=self.__credentials, params=params, **kwargs) 69 70 def patch(self, resource, params=None, data=None, json=None, **kwargs): 71 """ This method will perform a patch operation to the specified resource""" 72 url = self.__host_name + resource 73 kwargs['verify'] = self.__verify 74 75 if self.__oauth: 76 self.__check_oauth_token() 77 kwargs['headers'] = self.__add_auth_header(**kwargs) 78 return self.__session.patch(url, params=params, data=data, json=json, **kwargs) 79 80 return self.__session.patch(url, auth=self.__credentials, params=params, data=data, json=json, **kwargs) 81 82 def post(self, resource, params=None, data=None, json=None, **kwargs): 83 """ This method will perform a post operation to the specified resource.""" 84 url = self.__host_name + resource 85 kwargs['verify'] = self.__verify 86 87 if self.__oauth: 88 self.__check_oauth_token() 89 kwargs['headers'] = self.__add_auth_header(**kwargs) 90 return self.__session.post(url, params=params, data=data, json=json, **kwargs) 91 92 return self.__session.post(url, auth=self.__credentials, params=params, data=data, json=json, **kwargs) 93 94 def put(self, resource, params=None, data=None, json=None, **kwargs): 95 """ This method will perform a put operation to the specified resource""" 96 url = self.__host_name + resource 97 kwargs['verify'] = self.__verify 98 99 if self.__oauth: 100 self.__check_oauth_token() 101 kwargs['headers'] = self.__add_auth_header(**kwargs) 102 return self.__session.put(url, data=data, params=params, json=json, **kwargs) 103 104 return self.__session.put(url, auth=self.__credentials, data=data, params=params, json=json, **kwargs) 105 106 def __check_oauth_token(self): 107 108 if self.__token is None: 109 self.__get_fresh_token() 110 111 else: 112 time_elapsed = time.time() - self.__token_acquired_at 113 time_remaining = self.__token_expires_in - time_elapsed 114 if time_remaining < 60: 115 # if less than a minute remains, just get another token. 116 self.__get_fresh_token() 117 118 def __get_fresh_token(self): 119 """This method will fetch a new oauth bearer token from the oauth token server.""" 120 data = { 121 'grant_type': 'client_credentials' 122 } 123 124 # By getting the system time before we get the token we avoid a potential bug where the token may be expired. 125 time_before_request = time.time() 126 127 # Post to the token server, check if authorized 128 try: 129 response = requests.post(self.__token_host, auth=self.__credentials, data=data, verify=self.__verify) 130 response.raise_for_status() 131 except requests.exceptions.HTTPError as err: 132 message = "Unable to fetch token: " 133 raise UnauthorizedTokenException(message + str(err), response.status_code) 134 135 # If success get relevant data 136 if response.status_code in [200, 201]: 137 response_json = response.json() 138 self.__token = response_json['access_token'] 139 self.__token_expires_in = response_json['expires_in'] 140 self.__token_acquired_at = math.floor(time_before_request) 141 142 else: 143 py_jama_rest_client_logger.error('Failed to retrieve OAuth Token') 144 145 def __add_auth_header(self, **kwargs): 146 headers = kwargs.get('headers') 147 if headers is None: 148 headers = {} 149 headers['Authorization'] = 'Bearer ' + self.__token 150 return headers
class
CoreException(builtins.Exception):
13class CoreException(Exception): 14 """This is the base class for all exceptions raised by the Core""" 15 16 def __init__(self, message, status_code=None, reason=None): 17 super(CoreException, self).__init__(message) 18 self.status_code = status_code 19 self.reason = reason
This is the base class for all exceptions raised by the Core
Inherited Members
- builtins.BaseException
- with_traceback
class
Core:
27class Core: 28 """ This Class will contain a collection of methods that interact directly with the Jama API and return A Requests 29 Response Object. This class will give the user more fine grained access to the JAMA API. For more information 30 on the Requests library visit: http://docs.python-requests.org/en/master/""" 31 32 def __init__(self, host_name, user_credentials, api_version='/rest/v1/', oauth=False, verify=True): 33 # Instance variables 34 self.__api_version = api_version 35 self.__host_name = host_name + self.__api_version 36 self.__credentials = user_credentials 37 self.__oauth = oauth 38 self.__verify = verify 39 self.__session = requests.Session() 40 41 # Setup OAuth if needed. 42 if self.__oauth: 43 self.__token_host = host_name + '/rest/oauth/token' 44 self.__token = None 45 self.__get_fresh_token() 46 47 def delete(self, resource, **kwargs): 48 """ This method will perform a delete operation on the specified resource""" 49 url = self.__host_name + resource 50 kwargs['verify'] = self.__verify 51 52 if self.__oauth: 53 self.__check_oauth_token() 54 kwargs['headers'] = self.__add_auth_header(**kwargs) 55 return self.__session.delete(url, **kwargs) 56 57 return self.__session.delete(url, auth=self.__credentials, **kwargs) 58 59 def get(self, resource, params=None, **kwargs): 60 """ This method will perform a get operation on the specified resource""" 61 url = self.__host_name + resource 62 kwargs['verify'] = self.__verify 63 64 if self.__oauth: 65 self.__check_oauth_token() 66 kwargs['headers'] = self.__add_auth_header(**kwargs) 67 return self.__session.get(url, params=params, **kwargs) 68 69 return self.__session.get(url, auth=self.__credentials, params=params, **kwargs) 70 71 def patch(self, resource, params=None, data=None, json=None, **kwargs): 72 """ This method will perform a patch operation to the specified resource""" 73 url = self.__host_name + resource 74 kwargs['verify'] = self.__verify 75 76 if self.__oauth: 77 self.__check_oauth_token() 78 kwargs['headers'] = self.__add_auth_header(**kwargs) 79 return self.__session.patch(url, params=params, data=data, json=json, **kwargs) 80 81 return self.__session.patch(url, auth=self.__credentials, params=params, data=data, json=json, **kwargs) 82 83 def post(self, resource, params=None, data=None, json=None, **kwargs): 84 """ This method will perform a post operation to the specified resource.""" 85 url = self.__host_name + resource 86 kwargs['verify'] = self.__verify 87 88 if self.__oauth: 89 self.__check_oauth_token() 90 kwargs['headers'] = self.__add_auth_header(**kwargs) 91 return self.__session.post(url, params=params, data=data, json=json, **kwargs) 92 93 return self.__session.post(url, auth=self.__credentials, params=params, data=data, json=json, **kwargs) 94 95 def put(self, resource, params=None, data=None, json=None, **kwargs): 96 """ This method will perform a put operation to the specified resource""" 97 url = self.__host_name + resource 98 kwargs['verify'] = self.__verify 99 100 if self.__oauth: 101 self.__check_oauth_token() 102 kwargs['headers'] = self.__add_auth_header(**kwargs) 103 return self.__session.put(url, data=data, params=params, json=json, **kwargs) 104 105 return self.__session.put(url, auth=self.__credentials, data=data, params=params, json=json, **kwargs) 106 107 def __check_oauth_token(self): 108 109 if self.__token is None: 110 self.__get_fresh_token() 111 112 else: 113 time_elapsed = time.time() - self.__token_acquired_at 114 time_remaining = self.__token_expires_in - time_elapsed 115 if time_remaining < 60: 116 # if less than a minute remains, just get another token. 117 self.__get_fresh_token() 118 119 def __get_fresh_token(self): 120 """This method will fetch a new oauth bearer token from the oauth token server.""" 121 data = { 122 'grant_type': 'client_credentials' 123 } 124 125 # By getting the system time before we get the token we avoid a potential bug where the token may be expired. 126 time_before_request = time.time() 127 128 # Post to the token server, check if authorized 129 try: 130 response = requests.post(self.__token_host, auth=self.__credentials, data=data, verify=self.__verify) 131 response.raise_for_status() 132 except requests.exceptions.HTTPError as err: 133 message = "Unable to fetch token: " 134 raise UnauthorizedTokenException(message + str(err), response.status_code) 135 136 # If success get relevant data 137 if response.status_code in [200, 201]: 138 response_json = response.json() 139 self.__token = response_json['access_token'] 140 self.__token_expires_in = response_json['expires_in'] 141 self.__token_acquired_at = math.floor(time_before_request) 142 143 else: 144 py_jama_rest_client_logger.error('Failed to retrieve OAuth Token') 145 146 def __add_auth_header(self, **kwargs): 147 headers = kwargs.get('headers') 148 if headers is None: 149 headers = {} 150 headers['Authorization'] = 'Bearer ' + self.__token 151 return headers
This Class will contain a collection of methods that interact directly with the Jama API and return A Requests Response Object. This class will give the user more fine grained access to the JAMA API. For more information on the Requests library visit: http://docs.python-requests.org/en/master/
Core( host_name, user_credentials, api_version='/rest/v1/', oauth=False, verify=True)
32 def __init__(self, host_name, user_credentials, api_version='/rest/v1/', oauth=False, verify=True): 33 # Instance variables 34 self.__api_version = api_version 35 self.__host_name = host_name + self.__api_version 36 self.__credentials = user_credentials 37 self.__oauth = oauth 38 self.__verify = verify 39 self.__session = requests.Session() 40 41 # Setup OAuth if needed. 42 if self.__oauth: 43 self.__token_host = host_name + '/rest/oauth/token' 44 self.__token = None 45 self.__get_fresh_token()
def
delete(self, resource, **kwargs):
47 def delete(self, resource, **kwargs): 48 """ This method will perform a delete operation on the specified resource""" 49 url = self.__host_name + resource 50 kwargs['verify'] = self.__verify 51 52 if self.__oauth: 53 self.__check_oauth_token() 54 kwargs['headers'] = self.__add_auth_header(**kwargs) 55 return self.__session.delete(url, **kwargs) 56 57 return self.__session.delete(url, auth=self.__credentials, **kwargs)
This method will perform a delete operation on the specified resource
def
get(self, resource, params=None, **kwargs):
59 def get(self, resource, params=None, **kwargs): 60 """ This method will perform a get operation on the specified resource""" 61 url = self.__host_name + resource 62 kwargs['verify'] = self.__verify 63 64 if self.__oauth: 65 self.__check_oauth_token() 66 kwargs['headers'] = self.__add_auth_header(**kwargs) 67 return self.__session.get(url, params=params, **kwargs) 68 69 return self.__session.get(url, auth=self.__credentials, params=params, **kwargs)
This method will perform a get operation on the specified resource
def
patch(self, resource, params=None, data=None, json=None, **kwargs):
71 def patch(self, resource, params=None, data=None, json=None, **kwargs): 72 """ This method will perform a patch operation to the specified resource""" 73 url = self.__host_name + resource 74 kwargs['verify'] = self.__verify 75 76 if self.__oauth: 77 self.__check_oauth_token() 78 kwargs['headers'] = self.__add_auth_header(**kwargs) 79 return self.__session.patch(url, params=params, data=data, json=json, **kwargs) 80 81 return self.__session.patch(url, auth=self.__credentials, params=params, data=data, json=json, **kwargs)
This method will perform a patch operation to the specified resource
def
post(self, resource, params=None, data=None, json=None, **kwargs):
83 def post(self, resource, params=None, data=None, json=None, **kwargs): 84 """ This method will perform a post operation to the specified resource.""" 85 url = self.__host_name + resource 86 kwargs['verify'] = self.__verify 87 88 if self.__oauth: 89 self.__check_oauth_token() 90 kwargs['headers'] = self.__add_auth_header(**kwargs) 91 return self.__session.post(url, params=params, data=data, json=json, **kwargs) 92 93 return self.__session.post(url, auth=self.__credentials, params=params, data=data, json=json, **kwargs)
This method will perform a post operation to the specified resource.
def
put(self, resource, params=None, data=None, json=None, **kwargs):
95 def put(self, resource, params=None, data=None, json=None, **kwargs): 96 """ This method will perform a put operation to the specified resource""" 97 url = self.__host_name + resource 98 kwargs['verify'] = self.__verify 99 100 if self.__oauth: 101 self.__check_oauth_token() 102 kwargs['headers'] = self.__add_auth_header(**kwargs) 103 return self.__session.put(url, data=data, params=params, json=json, **kwargs) 104 105 return self.__session.put(url, auth=self.__credentials, data=data, params=params, json=json, **kwargs)
This method will perform a put operation to the specified resource