Skip to content

Commit

Permalink
Merge pull request #2 from alexandriagroup/add_version
Browse files Browse the repository at this point in the history
Add a param version in pydelivengo init
  • Loading branch information
chocobn69 authored Jan 11, 2022
2 parents b701080 + 7d2a371 commit 4a56e65
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 36 deletions.
34 changes: 19 additions & 15 deletions pydelivengo/pydelivengo.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from pydelivengo.exception import PyDelivengoTypeError

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


def add_headers(x, y):
Expand All @@ -35,13 +35,17 @@ def add_headers(x, y):
class PyDelivengo(object):
"""A class to manage the interaction with the MyDelivengo API"""

def __init__(self, api_authorization):
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,
}

def generate_url(self):
return URL.format(VERSION=self.version)

def get_depots(self, params=None):
"""
Return all the deposits.
Expand All @@ -54,7 +58,7 @@ def get_depots(self, params=None):
if params is not None and not isinstance(params, dict):
raise PyDelivengoTypeError('params should be a dictionary and not {}.'.format(type(params)))

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

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

url = 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'})
Expand All @@ -100,7 +104,7 @@ def post_depot(self, data_dict, print_pdf=False):
if not isinstance(data_dict, dict):
raise PyDelivengoTypeError('data_dict should be a dictionary and not {}.'.format(type(data_dict)))

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

if print_pdf: # Merge the 2 dicts
headers = add_headers(self.headers, {'Accept': 'application/pdf'})
Expand All @@ -127,7 +131,7 @@ def get_envois(self, params=None):
if params is not None and not isinstance(params, dict):
raise PyDelivengoTypeError('params should be a dictionary and not {}.'.format(type(params)))

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

Expand All @@ -149,7 +153,7 @@ def get_envoi(self, envoi_id, params=None, print_pdf=False):
if params is not None and not isinstance(params, dict):
raise PyDelivengoTypeError('params should be a dictionary and not {}.'.format(type(params)))

url = 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'})
Expand All @@ -171,7 +175,7 @@ def delete_envois(self, envoi_id):
if not isinstance(envoi_id, int):
raise PyDelivengoTypeError('pli_id should be an integer and not a {}'.format(type(envoi_id)))

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

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

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

if print_pdf: # Merge the 2 dicts
headers = add_headers(self.headers, {'Accept': 'application/pdf'})
Expand All @@ -216,7 +220,7 @@ def get_imputations(self):
:return: a list of imputations.
:rtype: dict
"""
url = URL + 'imputations/'
url = self.generate_url() + 'imputations/'
response = requests.request('GET', url, headers=self.headers)
return json.loads(response.text)

Expand All @@ -232,7 +236,7 @@ def get_imputation(self, imputation_id):
if not isinstance(imputation_id, int):
raise PyDelivengoTypeError('pli_id should be an integer and not a {}'.format(type(imputation_id)))

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

Expand All @@ -250,7 +254,7 @@ def get_plis(self, params=None):
if params is not None and not isinstance(params, dict):
raise PyDelivengoTypeError('params should be a dictionary and not {}.'.format(type(params)))

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

Expand All @@ -270,7 +274,7 @@ def get_pli(self, pli_id, print_pdf=False, params=None):
if not isinstance(pli_id, int):
raise PyDelivengoTypeError('pli_id should be an integer and not a {}'.format(type(pli_id)))

url = 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'})
Expand All @@ -292,7 +296,7 @@ def delete_plis(self, pli_id):
if not isinstance(pli_id, int):
raise PyDelivengoTypeError('pli_id should be an integer and not a {}'.format(type(pli_id)))

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

Expand All @@ -310,6 +314,6 @@ def get_user_info(self, user_id=0):
if not isinstance(user_id, int):
raise PyDelivengoTypeError('user_id should be an integer and not a {}'.format(type(user_id)))

url = URL + 'utilisateurs/' + str(user_id)
url = self.generate_url() + 'utilisateurs/' + str(user_id)
response = requests.request('GET', url, headers=self.headers)
return json.loads(response.text)
4 changes: 2 additions & 2 deletions pydelivengo/tests/tests_depots.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_get_depots():
"""Test get_depots function with a mock."""
with requests_mock.Mocker() as m:
api_response = open("pydelivengo/tests/assets/get_depots_ok.json", "rb", encoding='utf8').read()
m.get('https://mydelivengo.laposte.fr/api/v2/depots', text=api_response)
m.get('https://mydelivengo.laposte.fr/api/v2.4/depots', text=api_response)

api = PyDelivengo(api_authorization='Loremipsumdolorsitametconsectetu')
result = api.get_depots()
Expand Down Expand Up @@ -50,7 +50,7 @@ def test_get_depot():
"""Test get_depot with a mock."""
with requests_mock.Mocker() as m:
api_response = open("pydelivengo/tests/assets/get_depot_ok.json", "rb", encoding='utf8').read()
m.get('https://mydelivengo.laposte.fr/api/v2/depots/1814183', text=api_response)
m.get('https://mydelivengo.laposte.fr/api/v2.4/depots/1814183', text=api_response)

api = PyDelivengo(api_authorization='Loremipsumdolorsitametconsectetu')
result = api.get_depot(1814183)
Expand Down
6 changes: 3 additions & 3 deletions pydelivengo/tests/tests_envois.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_get_envois():
"""Test get_envois with a mock."""
with requests_mock.Mocker() as m:
api_response = open("pydelivengo/tests/assets/get_envois_ok.json", "rb", encoding='utf8').read()
m.get('https://mydelivengo.laposte.fr/api/v2/envois', text=api_response)
m.get('https://mydelivengo.laposte.fr/api/v2.4/envois', text=api_response)

api = PyDelivengo(api_authorization='Loremipsumdolorsitametconsectetu')
result = api.get_envois()
Expand All @@ -48,7 +48,7 @@ def test_get_envoi_pdf():
"""Test get_envoi with a mock."""
with requests_mock.Mocker() as m:
api_response = open("pydelivengo/tests/assets/get_envoi_ok.json", "rb", encoding='utf8').read()
m.get('https://mydelivengo.laposte.fr/api/v2/envois/5306429', text=api_response)
m.get('https://mydelivengo.laposte.fr/api/v2.4/envois/5306429', text=api_response)

api = PyDelivengo(api_authorization='Loremipsumdolorsitametconsectetu')
result = api.get_envoi(5306429)
Expand All @@ -62,7 +62,7 @@ def test_get_envoi():
"""Test get_envoi with pdf"""
with requests_mock.Mocker() as m:
api_response = open("pydelivengo/tests/assets/get_envoi_pdf_ok.json", "rb", encoding='utf8').read()
m.get('https://mydelivengo.laposte.fr/api/v2/envois/5306429', text=api_response)
m.get('https://mydelivengo.laposte.fr/api/v2.4/envois/5306429', text=api_response)

api = PyDelivengo(api_authorization='Loremipsumdolorsitametconsectetu')
result = api.get_envoi(5306429, print_pdf=True)
Expand Down
4 changes: 2 additions & 2 deletions pydelivengo/tests/tests_plis.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_get_plis():
"""Test get_plis with a mock."""
with requests_mock.Mocker() as m:
api_response = open("pydelivengo/tests/assets/get_plis_ok.json", "rb", encoding='utf8').read()
m.get('https://mydelivengo.laposte.fr/api/v2/plis', text=api_response)
m.get('https://mydelivengo.laposte.fr/api/v2.4/plis', text=api_response)

api = PyDelivengo(api_authorization='Loremipsumdolorsitametconsectetu')
result = api.get_plis()
Expand All @@ -48,7 +48,7 @@ def test_get_pli():
"""Test get_pli when the pdf is requested."""
with requests_mock.Mocker() as m:
api_response = open("pydelivengo/tests/assets/get_pli_pdf_ok.json", "rb", encoding='utf8').read()
m.get('https://mydelivengo.laposte.fr/api/v2/plis/11437479', text=api_response)
m.get('https://mydelivengo.laposte.fr/api/v2.4/plis/11437479', text=api_response)

api = PyDelivengo(api_authorization='Loremipsumdolorsitametconsectetu')
result = api.get_pli(11437479, print_pdf=True)
Expand Down
2 changes: 1 addition & 1 deletion pydelivengo/tests/tests_utilisateurs.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_get_user_info():
"""Test get_user_info with a mock."""
with requests_mock.Mocker() as m:
api_response = open("pydelivengo/tests/assets/get_utilisateur_ok.json", "rb", encoding='utf8').read()
m.get('https://mydelivengo.laposte.fr/api/v2/utilisateurs/0', text=api_response)
m.get('https://mydelivengo.laposte.fr/api/v2.4/utilisateurs/0', text=api_response)

api = PyDelivengo(api_authorization='Loremipsumdolorsitametconsectetu')
result = api.get_user_info()
Expand Down
12 changes: 6 additions & 6 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
coverage==4.4.2
pytest==3.3.2
pytest-cov==2.5.1
requests==2.20.0
requests-mock==1.4.0
tox==2.9.1
coverage
pytest
pytest-cov
requests
requests-mock
tox
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

setuptools.setup(
name="pydelivengo",
version="v1.4",
version="v1.5",
license='MIT',
author="Alicia FLOREZ",
author_email="[email protected]",
Expand All @@ -26,7 +26,7 @@
long_description_content_type="text/markdown",
url="https://github.com/alexandriagroup/pydelivengo",
project_urls = {
'Source': 'https://github.com/alexandriagroup/pydelivengo/archive/v1.4.tar.gz',
'Source': 'https://github.com/alexandriagroup/pydelivengo/archive/v1.5.tar.gz',
'Documentation': 'https://alexandriagroup.github.io/pydelivengo/',
'Travis': 'https://travis-ci.org/alexandriagroup/pydelivengo',
},
Expand Down
10 changes: 5 additions & 5 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = py27,py35,py363,py372
envlist = py36,py37,py38,py39
skip_missing_interpreters = True


Expand All @@ -13,10 +13,10 @@ passenv =

[travis]
python =
2.7: py27
3.5: py35
3.6.3: py363
3.7.2: py372
3.6: py36
3.7: py37
3.8: py38
3.9: py39

[pytest]
addopts = --ignore=setup.py
Expand Down

0 comments on commit 4a56e65

Please sign in to comment.