-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for webhooks subscriptions (#101)
- Loading branch information
1 parent
30738ef
commit af0f366
Showing
14 changed files
with
226 additions
and
79 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from connectors.travelperk.core.client import Travelperk | ||
|
||
__all__ = [ | ||
'Travelperk' | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from .invoice_profiles import InvoiceProfiles | ||
from .webhooks import WebhooksSubscriptions | ||
|
||
__all__ = [ | ||
'InvoiceProfiles', | ||
'WebhooksSubscriptions' | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Empty file.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.