Skip to content

Commit

Permalink
add support for get of invoices profiles (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
NileshPant1999 authored Dec 6, 2023
1 parent 4b3d43b commit 9d1c3cd
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 88 deletions.
5 changes: 5 additions & 0 deletions travelperk/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from travelperk.core.client import Travelperk

__all__ = [
'Travelperk'
]
5 changes: 5 additions & 0 deletions travelperk/apis/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .invoice_profiles import InvoiceProfiles

__all__ = [
'InvoiceProfiles'
]
67 changes: 67 additions & 0 deletions travelperk/apis/api_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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)
18 changes: 18 additions & 0 deletions travelperk/apis/invoice_profiles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
Travelperk Invoices
"""
from .api_base import ApiBase


class InvoiceProfiles(ApiBase):
"""Class for Invoice Profiles APIs."""

GET_INVOICE_PROFILES = '/profiles'

def get_all(self):
"""Get a list of the existing Invoice Profiles in the Organization.
Returns:
List with dicts in Invoice Profile schema.
"""
return self._get_request('profiles', InvoiceProfiles.GET_INVOICE_PROFILES)
File renamed without changes.
61 changes: 11 additions & 50 deletions travelperksdk/travelperksdk.py → travelperk/core/client.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
"""
Travelperk Python SDK
Travelperk Python Class
"""
import json
from typing import List, Dict
import requests
from future.moves.urllib.parse import urlencode

from travelperk.exceptions import *
from travelperk.apis.invoice_profiles import InvoiceProfiles

class TravelperkSDK:
class Travelperk:
"""
Travelperk SDK
Travelperk Python Class
"""

def __init__(self, client_id: str, client_secret: str,
Expand Down Expand Up @@ -37,6 +38,8 @@ def __init__(self, client_id: str, client_secret: str,

self.__access_token = None

self.invoice_profiles = InvoiceProfiles()

self.update_access_token()
self.update_server_url()

Expand All @@ -46,7 +49,7 @@ def update_server_url(self):
"""
base_url = self.__base_url

self.invoices.set_server_url(base_url)
self.invoice_profiles.set_server_url(base_url)

def update_access_token(self):
"""
Expand All @@ -55,7 +58,7 @@ def update_access_token(self):
self.__get_access_token()
access_token = self.__access_token

self.invoices.change_access_token(access_token)
self.invoice_profiles.change_access_token(access_token)

def __get_access_token(self):
"""Get the access token using a HTTP post.
Expand All @@ -71,14 +74,11 @@ def __get_access_token(self):
'client_secret': self.__client_secret
}

api_data = urlencode(api_data)

headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}

response = requests.post(url=self.__token_url, data=api_data, headers=headers)

response = requests.post(url=self.__token_url, data=urlencode(api_data), headers=headers)
if response.status_code == 200:
auth = json.loads(response.text)
self.__access_token = auth['access_token']
Expand All @@ -97,43 +97,4 @@ def __get_access_token(self):
raise InternalServerError('Internal server error', response.text)

else:
raise TravelperkSDKError('Error: {0}'.format(response.status_code), response.text)

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'
}

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 TravelperkSDKError('Error: {0}'.format(response.status_code), response.text)
raise TravelperkError('Error: {0}'.format(response.status_code), response.text)
File renamed without changes.
File renamed without changes.
20 changes: 10 additions & 10 deletions travelperksdk/exceptions.py → travelperk/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""
TravelPerk SDK Exceptions
TravelPerk Exceptions
"""


class TravelperkSDKError(Exception):
class TravelperkError(Exception):
"""The base exception class for Travelperk.
Parameters:
Expand All @@ -12,33 +12,33 @@ class TravelperkSDKError(Exception):
"""

def __init__(self, msg, response=None):
super(TravelperkSDKError, self).__init__(msg)
super(TravelperkError, self).__init__(msg)
self.message = msg
self.response = response

def __str__(self):
return repr(self.message)


class UnauthorizedClientError(TravelperkSDKError):
class UnauthorizedClientError(TravelperkError):
"""Wrong client secret and/or refresh token, 401 error."""


class ForbiddenClientError(TravelperkSDKError):
class ForbiddenClientError(TravelperkError):
"""The user has insufficient privilege, 403 error."""


class BadRequestError(TravelperkSDKError):
class BadRequestError(TravelperkError):
"""Some of the parameters are wrong, 400 error."""


class NotFoundError(TravelperkSDKError):
class NotFoundError(TravelperkError):
"""Not found the item from URL, 404 error."""


class InternalServerError(TravelperkSDKError):
"""The rest TravelperkSDK errors, 500 error."""
class InternalServerError(TravelperkError):
"""The rest Travelperk errors, 500 error."""


class RateLimitError(TravelperkSDKError):
class RateLimitError(TravelperkError):
"""To many requests, 429 error."""
28 changes: 0 additions & 28 deletions travelperksdk/__init__.py

This file was deleted.

Empty file removed travelperksdk/apis/invoices.py
Empty file.
Empty file removed travelperksdk/core/__init__.py
Empty file.
Empty file removed travelperksdk/core/client.py
Empty file.

0 comments on commit 9d1c3cd

Please sign in to comment.