Skip to content
Snippets Groups Projects
Commit 540d585a authored by Pablo Boizeau's avatar Pablo Boizeau
Browse files

Move get_userinfo to connectionmethod

parent c6407391
No related branches found
No related tags found
1 merge request!41Draft: Add userinfo feature and code coverage
Pipeline #300509 passed
......@@ -40,6 +40,14 @@ class OAuth2ConnectionMethod(BareConnectionMethod):
"""Return the headers."""
return {"authorization": f"bearer {self.oauth2_session.get_access_token()}"}
def get_userinfo(self):
"""Get the userinfo endpoint."""
openapi_url = retrieve_token_endpoint().replace("/token", "/userinfo")
_session = create_session()
headers = OAuth2ConnectionMethod().get_headers()
return _session.get(openapi_url, timeout=10, headers=headers).json()
class ApiKeyConnectionMethod(BareConnectionMethod):
"""API key connection method."""
......@@ -50,6 +58,10 @@ class ApiKeyConnectionMethod(BareConnectionMethod):
"""Return the headers."""
return self.api_key.to_dict()
def get_userinfo(self):
"""User info method for API key. Not available."""
raise NotImplementedError("No userinfo available with API key method.")
class HTTPSession:
"""HTTP session class."""
......@@ -75,14 +87,6 @@ class HTTPSession:
self.prepare_connection_method()
return self._method
def get_userinfo(self):
"""Get the userinfo endpoint."""
openapi_url = retrieve_token_endpoint().replace("/token", "/userinfo")
_session = create_session()
headers = OAuth2ConnectionMethod().get_headers()
return _session.get(openapi_url, timeout=10, headers=headers).json()
def prepare_connection_method(self):
"""Set the connection method."""
# Custom server without authentication method
......@@ -125,4 +129,4 @@ def get_headers() -> dict[str, Any]:
def get_userinfo() -> dict[str, str]:
"""Return userinfo."""
return session.get_userinfo()
return OAuth2ConnectionMethod().get_userinfo()
......@@ -5,7 +5,7 @@ import dinamis_sdk
def test_userinfo():
"""Test userinfo method."""
dinamis_sdk.get_userinfo()
print(dinamis_sdk.get_userinfo())
test_userinfo()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment