diff --git a/travelperk/core/__init__.py b/connectors/__init__.py similarity index 100% rename from travelperk/core/__init__.py rename to connectors/__init__.py diff --git a/connectors/travelperk/__init__.py b/connectors/travelperk/__init__.py new file mode 100644 index 00000000..a61c670a --- /dev/null +++ b/connectors/travelperk/__init__.py @@ -0,0 +1,5 @@ +from connectors.travelperk.core.client import Travelperk + +__all__ = [ + 'Travelperk' +] diff --git a/connectors/travelperk/apis/__init__.py b/connectors/travelperk/apis/__init__.py new file mode 100644 index 00000000..fbe0d149 --- /dev/null +++ b/connectors/travelperk/apis/__init__.py @@ -0,0 +1,7 @@ +from .invoice_profiles import InvoiceProfiles +from .webhooks import WebhooksSubscriptions + +__all__ = [ + 'InvoiceProfiles', + 'WebhooksSubscriptions' +] diff --git a/connectors/travelperk/apis/api_base.py b/connectors/travelperk/apis/api_base.py new file mode 100644 index 00000000..a73425de --- /dev/null +++ b/connectors/travelperk/apis/api_base.py @@ -0,0 +1,154 @@ +from typing import List, Dict +import requests +import json + +from connectors.travelperk.exceptions import * + + +class ApiBase: + """The base class for all API classes.""" + + def __init__(self): + self.__access_token = None + self.__server_url = None + + def change_access_token(self, access_token): + """Change the old access token with the new one. + + Parameters: + access_token (str): The new access token. + """ + self.__access_token = access_token + + def set_server_url(self, server_url): + """Set the server URL dynamically upon creating a connection + + Parameters: + server_url(str): The current server URL + """ + self.__server_url = server_url + + def _get_request(self, object_type: str, api_url: str) -> List[Dict] or Dict: + """Create a HTTP GET request. + + Parameters: + api_url (str): Url for the wanted API. + + Returns: + A response from the request (dict). + """ + + api_headers = { + 'Authorization': 'Bearer {0}'.format(self.__access_token), + 'Accept': 'application/json', + 'Api-Version': '1' + } + + response = requests.get( + '{0}{1}'.format(self.__server_url, api_url), + headers=api_headers + ) + + if response.status_code == 200: + result = json.loads(response.text) + return result[object_type] + + elif response.status_code == 400: + raise BadRequestError('Something wrong with the request body', response.text) + + elif response.status_code == 401: + raise UnauthorizedClientError('Wrong client secret or/and refresh token', response.text) + + elif response.status_code == 404: + raise NotFoundError('Client ID doesn\'t exist', response.text) + + elif response.status_code == 500: + raise InternalServerError('Internal server error', response.text) + + else: + raise TravelperkError('Error: {0}'.format(response.status_code), response.text) + + def _post_request(self, api_url: str, data: Dict) -> Dict: + """Create a HTTP POST request. + + Parameters: + api_url (str): Url for the wanted API. + data (dict): The parameters for the request. + + Returns: + A response from the request (dict). + """ + + api_headers = { + 'Authorization': 'Bearer {0}'.format(self.__access_token), + 'Accept': 'application/json', + 'Api-Version': '1' + } + + response = requests.post( + '{0}{1}'.format(self.__server_url, api_url), + headers=api_headers, + json=data + ) + + if response.status_code == 200: + result = json.loads(response.text) + return result + + elif response.status_code == 400: + raise BadRequestError('Something wrong with the request body', response.text) + + elif response.status_code == 401: + raise UnauthorizedClientError('Wrong client secret or/and refresh token', response.text) + + elif response.status_code == 404: + raise NotFoundError('Client ID doesn\'t exist', response.text) + + elif response.status_code == 500: + raise InternalServerError('Internal server error', response.text) + + elif response.status_code == 409: + raise BadRequestError('The webhook already exists', response.text) + + else: + raise TravelperkError('Error: {0}'.format(response.status_code), response.text) + + def _delete_request(self, api_url: str) -> Dict: + """Create a HTTP DELETE request. + + Parameters: + api_url (str): Url for the wanted API. + + Returns: + A response from the request (dict). + """ + + api_headers = { + 'Authorization': 'Bearer {0}'.format(self.__access_token), + 'Accept': 'application/json', + 'Api-Version': '1' + } + + response = requests.delete( + '{0}{1}'.format(self.__server_url, api_url), + headers=api_headers + ) + + if response.status_code == 200: + result = json.loads(response.text) + return result + + elif response.status_code == 400: + raise BadRequestError('Something wrong with the request body', response.text) + + elif response.status_code == 401: + raise UnauthorizedClientError('Wrong client secret or/and refresh token', response.text) + + elif response.status_code == 404: + raise NotFoundError('Client ID doesn\'t exist', response.text) + + elif response.status_code == 500: + raise InternalServerError('Internal server error', response.text) + + else: + raise TravelperkError('Error: {0}'.format(response.status_code), response.text) \ No newline at end of file diff --git a/travelperk/apis/invoice_profiles.py b/connectors/travelperk/apis/invoice_profiles.py similarity index 100% rename from travelperk/apis/invoice_profiles.py rename to connectors/travelperk/apis/invoice_profiles.py diff --git a/connectors/travelperk/apis/webhooks.py b/connectors/travelperk/apis/webhooks.py new file mode 100644 index 00000000..08617cb4 --- /dev/null +++ b/connectors/travelperk/apis/webhooks.py @@ -0,0 +1,54 @@ +""" +Travelperk Webhooks +""" +from .api_base import ApiBase + + +class WebhooksSubscriptions(ApiBase): + """Class for Webhooks Subscriptions APIs.""" + + GET_WEBHOOKS_SUBSCRIPTIONS = '/webhooks' + POST_WEBHOOKS_SUBSCRIPTIONS = '/webhooks' + GET_WEBHOOKS_SUBSCRIPTIONS_BY_ID = '/webhooks/{}' + DELETE_WEBHOOKS_SUBSCRIPTIONS = '/webhooks/{}' + + def get_all(self): + """Get a list of the existing Webhooks Subscriptions in the Organization. + + Returns: + List with dicts in Webhooks Subscriptions schema. + """ + return self._get_request('webhooks', WebhooksSubscriptions.GET_WEBHOOKS_SUBSCRIPTIONS) + + def get_by_id(self, subscription_id): + """Get a Webhooks Subscription in the Organization. + + Args: + subscription_id (str): The id of the Webhooks Subscription. + + Returns: + Dict in Webhooks Subscriptions schema. + """ + return self._get_request('webhooks', WebhooksSubscriptions.GET_WEBHOOKS_SUBSCRIPTIONS_BY_ID.format(subscription_id)) + + def create(self, data): + """Create a new Webhooks Subscription in the Organization. + + Args: + data (dict): Dict in Webhooks Subscriptions schema. + + Returns: + Dict in Webhooks Subscriptions schema. + """ + return self._post_request(WebhooksSubscriptions.POST_WEBHOOKS_SUBSCRIPTIONS, data=data) + + def delete(self, subscription_id): + """Delete a Webhooks Subscription in the Organization. + + Args: + subscription_id (str): The id of the Webhooks Subscription. + + Returns: + Dict in Webhooks Subscriptions schema. + """ + return self._delete_request(WebhooksSubscriptions.DELETE_WEBHOOKS_SUBSCRIPTIONS.format(subscription_id)) \ No newline at end of file diff --git a/travelperk/core/helpers.py b/connectors/travelperk/core/__init__.py similarity index 100% rename from travelperk/core/helpers.py rename to connectors/travelperk/core/__init__.py diff --git a/travelperk/core/client.py b/connectors/travelperk/core/client.py similarity index 90% rename from travelperk/core/client.py rename to connectors/travelperk/core/client.py index 4f06cb6e..e0d0e4c3 100644 --- a/travelperk/core/client.py +++ b/connectors/travelperk/core/client.py @@ -5,8 +5,9 @@ import requests from future.moves.urllib.parse import urlencode -from travelperk.exceptions import * -from travelperk.apis.invoice_profiles import InvoiceProfiles +from connectors.travelperk.exceptions import * +from connectors.travelperk.apis.invoice_profiles import InvoiceProfiles +from connectors.travelperk.apis.webhooks import WebhooksSubscriptions class Travelperk: """ @@ -39,6 +40,7 @@ def __init__(self, client_id: str, client_secret: str, self.__access_token = None self.invoice_profiles = InvoiceProfiles() + self.webhooks = WebhooksSubscriptions() self.update_access_token() self.update_server_url() @@ -50,6 +52,7 @@ def update_server_url(self): base_url = self.__base_url self.invoice_profiles.set_server_url(base_url) + self.webhooks.set_server_url(base_url) def update_access_token(self): """ @@ -59,6 +62,7 @@ def update_access_token(self): access_token = self.__access_token self.invoice_profiles.change_access_token(access_token) + self.webhooks.change_access_token(access_token) def __get_access_token(self): """Get the access token using a HTTP post. diff --git a/travelperk/core/schema/read_only.py b/connectors/travelperk/core/helpers.py similarity index 100% rename from travelperk/core/schema/read_only.py rename to connectors/travelperk/core/helpers.py diff --git a/connectors/travelperk/core/schema/read_only.py b/connectors/travelperk/core/schema/read_only.py new file mode 100644 index 00000000..e69de29b diff --git a/travelperk/exceptions.py b/connectors/travelperk/exceptions.py similarity index 100% rename from travelperk/exceptions.py rename to connectors/travelperk/exceptions.py diff --git a/travelperk/__init__.py b/travelperk/__init__.py deleted file mode 100644 index fd7ed79e..00000000 --- a/travelperk/__init__.py +++ /dev/null @@ -1,5 +0,0 @@ -from travelperk.core.client import Travelperk - -__all__ = [ - 'Travelperk' -] diff --git a/travelperk/apis/__init__.py b/travelperk/apis/__init__.py deleted file mode 100644 index bc554169..00000000 --- a/travelperk/apis/__init__.py +++ /dev/null @@ -1,5 +0,0 @@ -from .invoice_profiles import InvoiceProfiles - -__all__ = [ - 'InvoiceProfiles' -] diff --git a/travelperk/apis/api_base.py b/travelperk/apis/api_base.py deleted file mode 100644 index 1d11b994..00000000 --- a/travelperk/apis/api_base.py +++ /dev/null @@ -1,67 +0,0 @@ -from typing import List, Dict -import requests -import json - -from travelperk.exceptions import * - -class ApiBase: - """The base class for all API classes.""" - - def __init__(self): - self.__access_token = None - self.__server_url = None - - def change_access_token(self, access_token): - """Change the old access token with the new one. - - Parameters: - access_token (str): The new access token. - """ - self.__access_token = access_token - - def set_server_url(self, server_url): - """Set the server URL dynamically upon creating a connection - - Parameters: - server_url(str): The current server URL - """ - self.__server_url = server_url - - def _get_request(self, object_type: str, api_url: str) -> List[Dict] or Dict: - """Create a HTTP GET request. - - Parameters: - api_url (str): Url for the wanted API. - - Returns: - A response from the request (dict). - """ - - api_headers = { - 'Authorization': 'Bearer {0}'.format(self.__access_token), - 'Accept': 'application/json', - 'Api-Version': '1' - } - - response = requests.get( - '{0}{1}'.format(self.__server_url, api_url), - headers=api_headers - ) - if response.status_code == 200: - result = json.loads(response.text) - return result[object_type] - - elif response.status_code == 400: - raise BadRequestError('Something wrong with the request body', response.text) - - elif response.status_code == 401: - raise UnauthorizedClientError('Wrong client secret or/and refresh token', response.text) - - elif response.status_code == 404: - raise NotFoundError('Client ID doesn\'t exist', response.text) - - elif response.status_code == 500: - raise InternalServerError('Internal server error', response.text) - - else: - raise TravelperkError('Error: {0}'.format(response.status_code), response.text)