diff --git a/src/api_insee/api.py b/src/api_insee/api.py index 163a833..f8ab58b 100644 --- a/src/api_insee/api.py +++ b/src/api_insee/api.py @@ -6,15 +6,12 @@ class ApiInsee(): - def __init__(self, key, secret, format='json', noauth=False): + def __init__(self, token: str, format='json', noauth=False): if noauth: self.auth = MockAuth() else: - self.auth = AuthService( - key = key, - secret = secret - ) + self.auth = AuthService(token=token) self.format = format self.use('siren', RequestEntrepriseServiceSiren) diff --git a/src/api_insee/conf.py b/src/api_insee/conf.py index fb70956..ad0b5f7 100644 --- a/src/api_insee/conf.py +++ b/src/api_insee/conf.py @@ -2,9 +2,8 @@ API_VERSION = { "url" : 'https://api.insee.fr', - "path_token" : '/token', - "path_siren" : '/entreprises/sirene/V3.11/siren', - "path_siret" : '/entreprises/sirene/V3.11/siret', - "path_liens_succession" : '/entreprises/sirene/V3.11/siret/liensSuccession', + "path_siren" : '/api-sirene/3.11/siren', + "path_siret" : '/api-sirene/3.11/siret', + "path_liens_succession" : '/api-sirene/3.11/siret/liensSuccession', } diff --git a/src/api_insee/exeptions/auth_exeption.py b/src/api_insee/exeptions/auth_exeption.py index ff4ffb3..1904849 100644 --- a/src/api_insee/exeptions/auth_exeption.py +++ b/src/api_insee/exeptions/auth_exeption.py @@ -2,24 +2,18 @@ class AuthExeption(Exception): - credential = None + token: str = None - def __init__(self, credential): - self.credential = credential + def __init__(self, token): + self.token = token def invalidkeyAndSecret(self): - self.message = "Invalid consumer key or secret. key : %s secret : %s" % ( - self.credential.key, - self.credential.secret - ) + self.message = "Invalid consumer key or secret. token : %s" % self.token return self def unauthorized(self, reason=False): - self.message = "Api connection unauthorized. key : %s secret : %s" % ( - self.credential.key, - self.credential.secret - ) + self.message = "Api connection unauthorized. token : %s" % self.token if reason: self.message += "\n %s" % (reason) diff --git a/src/api_insee/request/request.py b/src/api_insee/request/request.py index ea8fa84..cea6d45 100644 --- a/src/api_insee/request/request.py +++ b/src/api_insee/request/request.py @@ -154,7 +154,7 @@ def data(self): def header(self): return { 'Accept' : self._accept_format, - 'Authorization' : 'Bearer %s' % (self.token.access_token) + 'X-INSEE-Api-Key-Integration' : self.token, } @property @@ -177,7 +177,7 @@ def catchHTTPError(self, error): raise RequestExeption(self).badRequest() elif error.code == 401: - raise AuthExeption(self.credentials).unauthorized(error.reason) + raise AuthExeption(self.token).unauthorized(error.reason) else: raise error diff --git a/src/api_insee/request/request_token.py b/src/api_insee/request/request_token.py deleted file mode 100644 index cb822e7..0000000 --- a/src/api_insee/request/request_token.py +++ /dev/null @@ -1,25 +0,0 @@ -import urllib.request as ur -import urllib.error as ue -import json - -from api_insee.conf import API_VERSION -from .request import RequestService - -class RequestTokenService(RequestService): - - def __init__(self, credentials): - self.credentials = credentials - - @property - def url_path(self): - return API_VERSION['url'] + API_VERSION['path_token'] - - @property - def data(self): - return "grant_type=client_credentials".encode('ascii') - - @property - def header(self): - return { - 'Authorization' : 'Basic %s' % (self.credentials.encoded) - } diff --git a/src/api_insee/utils/auth_service.py b/src/api_insee/utils/auth_service.py index 150e274..7c588b2 100644 --- a/src/api_insee/utils/auth_service.py +++ b/src/api_insee/utils/auth_service.py @@ -1,30 +1,12 @@ -from api_insee.utils.client_credentials import ClientCredentials -from api_insee.utils.client_token import ClientToken -from api_insee.request.request_token import RequestTokenService - class AuthService(): token = None - def __init__(self, key=False, secret=False): - - self.credentials = ClientCredentials( - key = key, - secret = secret - ) - self.generateToken() - - def generateToken(self): - data = RequestTokenService(self.credentials).get() - self.token = ClientToken(**data) + def __init__(self, token = None): + self.token = token class MockAuth(AuthService): def __init__(self): - self.token = ClientToken( - token_type='Bearer', - expires_in=100000, - access_token='No Auth', - scope='No Scope' - ) + self.token = "mocked-api-key" \ No newline at end of file diff --git a/src/api_insee/utils/client_credentials.py b/src/api_insee/utils/client_credentials.py deleted file mode 100644 index 3f65e49..0000000 --- a/src/api_insee/utils/client_credentials.py +++ /dev/null @@ -1,19 +0,0 @@ -import base64 -from api_insee.exeptions.auth_exeption import AuthExeption - -class ClientCredentials(): - - def __init__(self, key=False, secret=False): - - self.key = key - self.secret = secret - - if not self.key or not self.secret: - raise AuthExeption(self).invalidkeyAndSecret() - - self.encoded = self.getEncodedCredential() - - def getEncodedCredential(self): - blike = (self.key+':'+self.secret).encode('utf-8') - encoded = base64.b64encode(blike).decode('utf-8') - return encoded diff --git a/src/api_insee/utils/client_token.py b/src/api_insee/utils/client_token.py deleted file mode 100644 index e3cefb9..0000000 --- a/src/api_insee/utils/client_token.py +++ /dev/null @@ -1,15 +0,0 @@ -import time - -class ClientToken(): - - token_type = False - epoch_expiration = False - access_token = False - scope = False - - def __init__(self, **data): - - self.token_type = data.get('token_type', False) - self.epoch_expiration = data.get('expires_in', 0) + int(time.time()) - self.access_token = data.get('access_token', False) - self.scope = data.get('scope', False) diff --git a/tests/conftest.example.py b/tests/conftest.example.py index 12450b5..ae88067 100644 --- a/tests/conftest.example.py +++ b/tests/conftest.example.py @@ -23,8 +23,6 @@ # Replace API consumer data with yours # -SIRENE_API_CONSUMER_KEY = "your-key" -SIRENE_API_CONSUMER_SECRET = "your-secret" SIRENE_API_TOKEN = "your-token" @@ -43,4 +41,11 @@ def exec(api_request): if request.config.getoption("--api-insee-execute-request"): api_request.get() - return exec \ No newline at end of file + return exec + +from api_insee import ApiInsee + +@pytest.fixture +def api(): + api = ApiInsee(token=SIRENE_API_TOKEN) + return api \ No newline at end of file diff --git a/tests/test_authentication.py b/tests/test_authentication.py deleted file mode 100644 index 8ef3b06..0000000 --- a/tests/test_authentication.py +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - -import pytest -import conftest as conf - -from api_insee import ApiInsee -from api_insee.exeptions.auth_exeption import AuthExeption - -__author__ = "Lenselle Nicolas" -__copyright__ = "Lenselle Nicolas" -__license__ = "mit" - -def test_missing_credentials(): - - with pytest.raises(AuthExeption): - api = ApiInsee(False, False) - -@pytest.mark.http -def test_unauthorized_credentials(): - - with pytest.raises(AuthExeption): - api = ApiInsee( - key = conf.SIRENE_API_CONSUMER_KEY+'wrongly', - secret= conf.SIRENE_API_CONSUMER_SECRET - ) - -@pytest.mark.http -def test_generate_token(): - - api = ApiInsee( - key = conf.SIRENE_API_CONSUMER_KEY, - secret= conf.SIRENE_API_CONSUMER_SECRET - ) - - assert len(api.auth.token.access_token) > 0 diff --git a/tests/test_csv.py b/tests/test_csv.py index 85fd209..4d221aa 100644 --- a/tests/test_csv.py +++ b/tests/test_csv.py @@ -26,8 +26,7 @@ def test_request_format_fallback_is_json(api, execute_request): def test_request_format_fallback_is_csv(api, execute_request): api_csv = ApiInsee( - key = conf.SIRENE_API_CONSUMER_KEY, - secret = conf.SIRENE_API_CONSUMER_SECRET, + token=conf.SIRENE_API_TOKEN, format = 'csv' ) diff --git a/tests/test_cursor.py b/tests/test_cursor.py index e4e73d2..56d4918 100644 --- a/tests/test_cursor.py +++ b/tests/test_cursor.py @@ -2,9 +2,7 @@ # -*- coding: utf-8 -*- import pytest -import conftest as conf -from api_insee import ApiInsee from api_insee.conf import API_VERSION import api_insee.criteria as Criteria diff --git a/tests/test_liens_sucession_service.py b/tests/test_liens_sucession_service.py index e862ce6..8fe0a9d 100644 --- a/tests/test_liens_sucession_service.py +++ b/tests/test_liens_sucession_service.py @@ -3,7 +3,6 @@ import pytest -from api_insee import ApiInsee from api_insee.conf import API_VERSION import api_insee.criteria as Criteria diff --git a/tests/test_siren.py b/tests/test_siren.py index b64c30c..a3e323c 100644 --- a/tests/test_siren.py +++ b/tests/test_siren.py @@ -2,9 +2,7 @@ # -*- coding: utf-8 -*- import pytest -import conftest as conf -from api_insee import ApiInsee from api_insee.conf import API_VERSION import api_insee.criteria as Criteria diff --git a/tests/test_siret.py b/tests/test_siret.py index cc61c51..2733df3 100644 --- a/tests/test_siret.py +++ b/tests/test_siret.py @@ -2,9 +2,7 @@ # -*- coding: utf-8 -*- import pytest -import conftest as conf -from api_insee import ApiInsee from api_insee.conf import API_VERSION import api_insee.criteria as Criteria