From f83a77ee3eca812ba6650f5a8cdbafa2f095863d Mon Sep 17 00:00:00 2001 From: BW27492 Date: Thu, 4 Jan 2024 13:56:33 +0000 Subject: [PATCH 1/2] TokenAgent implemented. Fix to agent inheritance --- lib/python-beta/src/bailo/__init__.py | 4 +-- lib/python-beta/src/bailo/core/agent.py | 47 +++++++++++++++++++++---- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/lib/python-beta/src/bailo/__init__.py b/lib/python-beta/src/bailo/__init__.py index 131a40c54..74a46d701 100644 --- a/lib/python-beta/src/bailo/__init__.py +++ b/lib/python-beta/src/bailo/__init__.py @@ -1,8 +1,8 @@ from __future__ import annotations -__version__ = "0.2.2" +__version__ = "0.3.0" -from bailo.core.agent import Agent +from bailo.core.agent import Agent, PkiAgent, TokenAgent from bailo.core.client import Client from bailo.core.enums import ModelVisibility, Role, SchemaKind from bailo.helper.access_request import AccessRequest diff --git a/lib/python-beta/src/bailo/core/agent.py b/lib/python-beta/src/bailo/core/agent.py index d1cd760d1..ffc3d0547 100644 --- a/lib/python-beta/src/bailo/core/agent.py +++ b/lib/python-beta/src/bailo/core/agent.py @@ -3,6 +3,7 @@ from json import JSONDecodeError import requests +from requests.auth import HTTPBasicAuth from bailo.core.exceptions import BailoException, ResponseException @@ -10,8 +11,10 @@ class Agent: def __request(self, method, *args, **kwargs): if "timeout" not in kwargs: kwargs["timeout"] = 5 + if "verify" not in kwargs: + kwargs["verify"] = True - res = requests.request(method, *args, verify=True, **kwargs) + res = requests.request(method, *args, **kwargs) # Check response for a valid range if res.status_code < 400: @@ -62,16 +65,48 @@ def __init__( self.auth = auth def get(self, *args, **kwargs): - return self.get(*args, cert=(self.cert, self.key), verify=self.auth, **kwargs) + return super().get(*args, cert=(self.cert, self.key), verify=self.auth, **kwargs) def post(self, *args, **kwargs): - return self.post(*args, cert=(self.cert, self.key), verify=self.auth, **kwargs) + return super().post(*args, cert=(self.cert, self.key), verify=self.auth, **kwargs) def put(self, *args, **kwargs): - return self.put(*args, cert=(self.cert, self.key), verify=self.auth, **kwargs) + return super().put(*args, cert=(self.cert, self.key), verify=self.auth, **kwargs) def patch(self, *args, **kwargs): - return self.patch(*args, cert=(self.cert, self.key), verify=self.auth, **kwargs) + return super().patch(*args, cert=(self.cert, self.key), verify=self.auth, **kwargs) def delete(self, *args, **kwargs): - return self.delete(*args, cert=(self.cert, self.key), verify=self.auth, **kwargs) + return super().delete(*args, cert=(self.cert, self.key), verify=self.auth, **kwargs) + + +class TokenAgent(Agent): + def __init__( + self, + access_key: str, + secret_key: str, + ): + """ + Initiates an agent for API token authentication. + + :param access_key: Access key + :param secret_key: Secret key + """ + self.access_key = access_key + self.secret_key = secret_key + self.basic = HTTPBasicAuth(access_key, secret_key) + + def get(self, *args, **kwargs): + return super().get(*args, auth=self.basic, **kwargs) + + def post(self, *args, **kwargs): + return super().post(*args, auth=self.basic, **kwargs) + + def put(self, *args, **kwargs): + return super().put(*args, auth=self.basic, **kwargs) + + def patch(self, *args, **kwargs): + return super().patch(*args, auth=self.basic, **kwargs) + + def delete(self, *args, **kwargs): + return super().delete(*args, auth=self.basic, **kwargs) From dd854473fc64c3da4f90fd4c4ee74b658575e47f Mon Sep 17 00:00:00 2001 From: BW27492 Date: Thu, 4 Jan 2024 14:23:18 +0000 Subject: [PATCH 2/2] Added comment --- lib/python-beta/src/bailo/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/python-beta/src/bailo/__init__.py b/lib/python-beta/src/bailo/__init__.py index 74a46d701..872b84065 100644 --- a/lib/python-beta/src/bailo/__init__.py +++ b/lib/python-beta/src/bailo/__init__.py @@ -1,5 +1,6 @@ from __future__ import annotations +# SEMANTIC VERSION __version__ = "0.3.0" from bailo.core.agent import Agent, PkiAgent, TokenAgent