Skip to content
Open
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
7 changes: 2 additions & 5 deletions src/api_insee/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 3 additions & 4 deletions src/api_insee/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',

}
16 changes: 5 additions & 11 deletions src/api_insee/exeptions/auth_exeption.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/api_insee/request/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
25 changes: 0 additions & 25 deletions src/api_insee/request/request_token.py

This file was deleted.

24 changes: 3 additions & 21 deletions src/api_insee/utils/auth_service.py
Original file line number Diff line number Diff line change
@@ -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"
19 changes: 0 additions & 19 deletions src/api_insee/utils/client_credentials.py

This file was deleted.

15 changes: 0 additions & 15 deletions src/api_insee/utils/client_token.py

This file was deleted.

11 changes: 8 additions & 3 deletions tests/conftest.example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"


Expand All @@ -43,4 +41,11 @@ def exec(api_request):
if request.config.getoption("--api-insee-execute-request"):
api_request.get()

return exec
return exec

from api_insee import ApiInsee

@pytest.fixture
def api():
api = ApiInsee(token=SIRENE_API_TOKEN)
return api
36 changes: 0 additions & 36 deletions tests/test_authentication.py

This file was deleted.

3 changes: 1 addition & 2 deletions tests/test_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)

Expand Down
2 changes: 0 additions & 2 deletions tests/test_cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion tests/test_liens_sucession_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import pytest

from api_insee import ApiInsee
from api_insee.conf import API_VERSION
import api_insee.criteria as Criteria

Expand Down
2 changes: 0 additions & 2 deletions tests/test_siren.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 0 additions & 2 deletions tests/test_siret.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down