Skip to content

Commit

Permalink
Add User-Agent in pydelivengo 1.6
Browse files Browse the repository at this point in the history
  • Loading branch information
chocobn69 committed Jan 11, 2022
1 parent 4a56e65 commit d3971c0
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 56 deletions.
2 changes: 0 additions & 2 deletions pydelivengo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@
#
# Distributed under terms of the MIT license.

from __future__ import absolute_import

from pydelivengo.pydelivengo import PyDelivengo
4 changes: 2 additions & 2 deletions pydelivengo/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
#
# Distributed under terms of the MIT license.

from __future__ import absolute_import


class PyDelivengoException(Exception):
""""""

pass


class PyDelivengoTypeError(PyDelivengoException):
""""""

pass
133 changes: 82 additions & 51 deletions pydelivengo/pydelivengo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
#
# Distributed under terms of the MIT license.

from __future__ import absolute_import

import json

import requests

from pydelivengo.exception import PyDelivengoTypeError

URL = 'https://mydelivengo.laposte.fr/api/v{VERSION}/'
URL = "https://mydelivengo.laposte.fr/api/v{VERSION}/"


def add_headers(x, y):
Expand All @@ -39,8 +37,9 @@ def __init__(self, api_authorization, version="2.4"):
"""Create the headers for the connection with MyDelivengo API"""
self.version = version
self.headers = {
'cookie': 'locale=fr;',
'api-authorization': api_authorization,
"cookie": "locale=fr;",
"API-Authorization": api_authorization,
"User-Agent": "PyDelivengo",
}

def generate_url(self):
Expand All @@ -56,10 +55,12 @@ def get_depots(self, params=None):
:rtype: dict
"""
if params is not None and not isinstance(params, dict):
raise PyDelivengoTypeError('params should be a dictionary and not {}.'.format(type(params)))
raise PyDelivengoTypeError(
"params should be a dictionary and not {}.".format(type(params))
)

url = self.generate_url() + 'depots'
response = requests.request('GET', url, headers=self.headers, params=params)
url = self.generate_url() + "depots"
response = requests.request("GET", url, headers=self.headers, params=params)
return json.loads(response.text)

def get_depot(self, depot_id, params=None, print_pdf=False):
Expand All @@ -76,18 +77,22 @@ def get_depot(self, depot_id, params=None, print_pdf=False):
:rtype: dict
"""
if not isinstance(depot_id, int):
raise PyDelivengoTypeError('depot_id should be a dictionary and not {}.'.format(type(depot_id)))
raise PyDelivengoTypeError(
"depot_id should be a dictionary and not {}.".format(type(depot_id))
)
if params is not None and not isinstance(params, dict):
raise PyDelivengoTypeError('params should be a dictionary and not {}.'.format(type(params)))
raise PyDelivengoTypeError(
"params should be a dictionary and not {}.".format(type(params))
)

url = self.generate_url() + 'depots/' + str(depot_id)
url = self.generate_url() + "depots/" + str(depot_id)

if print_pdf: # Merge the 2 dicts
headers = add_headers(self.headers, {'Accept': 'application/pdf'})
headers = add_headers(self.headers, {"Accept": "application/pdf"})
else:
headers = self.headers

response = requests.request('GET', url, headers=headers, params=params)
response = requests.request("GET", url, headers=headers, params=params)
return json.loads(response.text)

def post_depot(self, data_dict, print_pdf=False):
Expand All @@ -102,19 +107,21 @@ def post_depot(self, data_dict, print_pdf=False):
:rtype: dict
"""
if not isinstance(data_dict, dict):
raise PyDelivengoTypeError('data_dict should be a dictionary and not {}.'.format(type(data_dict)))
raise PyDelivengoTypeError(
"data_dict should be a dictionary and not {}.".format(type(data_dict))
)

url = self.generate_url() + 'depots/'
url = self.generate_url() + "depots/"

if print_pdf: # Merge the 2 dicts
headers = add_headers(self.headers, {'Accept': 'application/pdf'})
headers = add_headers(self.headers, {"Accept": "application/pdf"})
else:
headers = self.headers

# Json encoding
data_json = json.dumps(data_dict)

response = requests.request('POST', url, data=data_json, headers=headers)
response = requests.request("POST", url, data=data_json, headers=headers)
return json.loads(response.text)

# -------------------------------------------------------------------------
Expand All @@ -129,10 +136,12 @@ def get_envois(self, params=None):
:rtype: dict
"""
if params is not None and not isinstance(params, dict):
raise PyDelivengoTypeError('params should be a dictionary and not {}.'.format(type(params)))
raise PyDelivengoTypeError(
"params should be a dictionary and not {}.".format(type(params))
)

url = self.generate_url() + 'envois'
response = requests.request('GET', url, headers=self.headers, params=params)
url = self.generate_url() + "envois"
response = requests.request("GET", url, headers=self.headers, params=params)
return json.loads(response.text)

def get_envoi(self, envoi_id, params=None, print_pdf=False):
Expand All @@ -149,18 +158,22 @@ def get_envoi(self, envoi_id, params=None, print_pdf=False):
:rtype: dict
"""
if not isinstance(envoi_id, int):
raise PyDelivengoTypeError('Type of envoi_id should be int and not {}.'.format(type(envoi_id)))
raise PyDelivengoTypeError(
"Type of envoi_id should be int and not {}.".format(type(envoi_id))
)
if params is not None and not isinstance(params, dict):
raise PyDelivengoTypeError('params should be a dictionary and not {}.'.format(type(params)))
raise PyDelivengoTypeError(
"params should be a dictionary and not {}.".format(type(params))
)

url = self.generate_url() + 'envois/' + str(envoi_id)
url = self.generate_url() + "envois/" + str(envoi_id)

if print_pdf: # Merge the 2 dicts
headers = add_headers(self.headers, {'Accept': 'application/pdf'})
headers = add_headers(self.headers, {"Accept": "application/pdf"})
else:
headers = self.headers

response = requests.request('GET', url, headers=headers, params=params)
response = requests.request("GET", url, headers=headers, params=params)
return json.loads(response.text)

def delete_envois(self, envoi_id):
Expand All @@ -173,10 +186,12 @@ def delete_envois(self, envoi_id):
:rtype: bool
"""
if not isinstance(envoi_id, int):
raise PyDelivengoTypeError('pli_id should be an integer and not a {}'.format(type(envoi_id)))
raise PyDelivengoTypeError(
"pli_id should be an integer and not a {}".format(type(envoi_id))
)

url = self.generate_url() + 'envois/' + str(envoi_id)
response = requests.request('DELETE', url, headers=self.headers)
url = self.generate_url() + "envois/" + str(envoi_id)
response = requests.request("DELETE", url, headers=self.headers)
return response.ok

def post_envois(self, data_dict, print_pdf=False, params=None):
Expand All @@ -193,22 +208,28 @@ def post_envois(self, data_dict, print_pdf=False, params=None):
:rtype: dict
"""
if not isinstance(data_dict, dict):
raise PyDelivengoTypeError('data_dict should be a and not a {}'.format(type(data_dict)))
raise PyDelivengoTypeError(
"data_dict should be a and not a {}".format(type(data_dict))
)
if params is not None and not isinstance(params, dict):
raise PyDelivengoTypeError('params should be a dictionary and not {}.'.format(type(params)))
raise PyDelivengoTypeError(
"params should be a dictionary and not {}.".format(type(params))
)

url = self.generate_url() + 'envois/'
url = self.generate_url() + "envois/"

if print_pdf: # Merge the 2 dicts
headers = add_headers(self.headers, {'Accept': 'application/pdf'})
headers = add_headers(self.headers, {"Accept": "application/pdf"})
else:
headers = self.headers

# Json encoding
data_json = json.dumps(data_dict)

# Request
response = requests.request('POST', url, data=data_json, headers=headers, params=params)
response = requests.request(
"POST", url, data=data_json, headers=headers, params=params
)
return json.loads(response.text)

# -------------------------------------------------------------------------
Expand All @@ -220,8 +241,8 @@ def get_imputations(self):
:return: a list of imputations.
:rtype: dict
"""
url = self.generate_url() + 'imputations/'
response = requests.request('GET', url, headers=self.headers)
url = self.generate_url() + "imputations/"
response = requests.request("GET", url, headers=self.headers)
return json.loads(response.text)

def get_imputation(self, imputation_id):
Expand All @@ -234,10 +255,12 @@ def get_imputation(self, imputation_id):
:rtype: dict
"""
if not isinstance(imputation_id, int):
raise PyDelivengoTypeError('pli_id should be an integer and not a {}'.format(type(imputation_id)))
raise PyDelivengoTypeError(
"pli_id should be an integer and not a {}".format(type(imputation_id))
)

url = self.generate_url() + 'imputations/' + str(imputation_id)
response = requests.request('GET', url, headers=self.headers)
url = self.generate_url() + "imputations/" + str(imputation_id)
response = requests.request("GET", url, headers=self.headers)
return json.loads(response.text)

# -------------------------------------------------------------------------
Expand All @@ -252,10 +275,12 @@ def get_plis(self, params=None):
:rtype: dict
"""
if params is not None and not isinstance(params, dict):
raise PyDelivengoTypeError('params should be a dictionary and not {}.'.format(type(params)))
raise PyDelivengoTypeError(
"params should be a dictionary and not {}.".format(type(params))
)

url = self.generate_url() + 'plis'
response = requests.request('GET', url, headers=self.headers, params=params)
url = self.generate_url() + "plis"
response = requests.request("GET", url, headers=self.headers, params=params)
return json.loads(response.text)

def get_pli(self, pli_id, print_pdf=False, params=None):
Expand All @@ -272,16 +297,18 @@ def get_pli(self, pli_id, print_pdf=False, params=None):
:rtype: dict
"""
if not isinstance(pli_id, int):
raise PyDelivengoTypeError('pli_id should be an integer and not a {}'.format(type(pli_id)))
raise PyDelivengoTypeError(
"pli_id should be an integer and not a {}".format(type(pli_id))
)

url = self.generate_url() + 'plis/' + str(pli_id)
url = self.generate_url() + "plis/" + str(pli_id)

if print_pdf: # Merge the 2 dicts
headers = add_headers(self.headers, {'Accept': 'application/pdf'})
headers = add_headers(self.headers, {"Accept": "application/pdf"})
else:
headers = self.headers

response = requests.request('GET', url, headers=headers, params=params)
response = requests.request("GET", url, headers=headers, params=params)
return json.loads(response.text)

def delete_plis(self, pli_id):
Expand All @@ -294,10 +321,12 @@ def delete_plis(self, pli_id):
:rtype: bool
"""
if not isinstance(pli_id, int):
raise PyDelivengoTypeError('pli_id should be an integer and not a {}'.format(type(pli_id)))
raise PyDelivengoTypeError(
"pli_id should be an integer and not a {}".format(type(pli_id))
)

url = self.generate_url() + 'plis/' + str(pli_id)
response = requests.request('DELETE', url, headers=self.headers)
url = self.generate_url() + "plis/" + str(pli_id)
response = requests.request("DELETE", url, headers=self.headers)
return response.ok

# -------------------------------------------------------------------------
Expand All @@ -312,8 +341,10 @@ def get_user_info(self, user_id=0):
:rtype: dict
"""
if not isinstance(user_id, int):
raise PyDelivengoTypeError('user_id should be an integer and not a {}'.format(type(user_id)))
raise PyDelivengoTypeError(
"user_id should be an integer and not a {}".format(type(user_id))
)

url = self.generate_url() + 'utilisateurs/' + str(user_id)
response = requests.request('GET', url, headers=self.headers)
url = self.generate_url() + "utilisateurs/" + str(user_id)
response = requests.request("GET", url, headers=self.headers)
return json.loads(response.text)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

setuptools.setup(
name="pydelivengo",
version="v1.5",
version="1.6",
license='MIT',
author="Alicia FLOREZ",
author_email="[email protected]",
Expand Down

0 comments on commit d3971c0

Please sign in to comment.