Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for webhooks subscriptions #101

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
5 changes: 5 additions & 0 deletions connectors/travelperk/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from connectors.travelperk.core.client import Travelperk

__all__ = [
'Travelperk'
]
7 changes: 7 additions & 0 deletions connectors/travelperk/apis/__init__.py
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'
]
154 changes: 154 additions & 0 deletions connectors/travelperk/apis/api_base.py
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)
54 changes: 54 additions & 0 deletions connectors/travelperk/apis/webhooks.py
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.
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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()
Expand All @@ -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):
"""
Expand All @@ -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.
Expand Down
Empty file.
File renamed without changes.
5 changes: 0 additions & 5 deletions travelperk/__init__.py

This file was deleted.

5 changes: 0 additions & 5 deletions travelperk/apis/__init__.py

This file was deleted.

67 changes: 0 additions & 67 deletions travelperk/apis/api_base.py

This file was deleted.

Loading