Skip to content

Commit

Permalink
Add trust_env parametr to USOSClient
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoni-Czaplicki committed Nov 26, 2024
1 parent acd332b commit e1a96e5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "usos-api"
version = "0.3.1"
version = "0.3.2"
description = "Asynchronous USOS API for Python"
authors = ["Antoni-Czaplicki <56671347+Antoni-Czaplicki@users.noreply.github.com>"]
readme = "README.md"
Expand Down
12 changes: 10 additions & 2 deletions usos_api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,28 @@ class AuthManager:
# List of available scopes can be found at https://apps.usos.edu.pl/developers/api/authorization/#scopes
SCOPES = "|".join(["offline_access", "studies"])

def __init__(self, api_base_address: str, consumer_key: str, consumer_secret: str):
def __init__(
self,
api_base_address: str,
consumer_key: str,
consumer_secret: str,
trust_env: bool = False,
):
"""
Initialize the authentication manager.
:param api_base_address: The base address of the USOS API.
:param consumer_key: Consumer key obtained from the USOS API.
:param consumer_secret: Consumer secret obtained from the USOS API.
:param trust_env: Whether to trust the environment variables for the connection, see https://docs.aiohttp.org/en/stable/client_reference.html#aiohttp.ClientSession for more information.
"""
self.base_address = api_base_address.rstrip("/") + "/"
self.consumer_key = consumer_key
self.consumer_secret = consumer_secret
self.access_token = None
self.access_token_secret = None
self._session = None
self.trust_env = trust_env
self._oauth_client = Client(consumer_key, consumer_secret)

async def __aenter__(self) -> "AuthManager":
Expand All @@ -60,7 +68,7 @@ async def open(self):
"""
Open the manager.
"""
self._session = aiohttp.ClientSession()
self._session = aiohttp.ClientSession(trust_env=self.trust_env)

async def close(self):
"""
Expand Down
9 changes: 7 additions & 2 deletions usos_api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,22 @@ class USOSClient:
"""

def __init__(
self, api_base_address: str, consumer_key: str, consumer_secret: str
self,
api_base_address: str,
consumer_key: str,
consumer_secret: str,
trust_env: bool = False,
) -> None:
"""
Initialize the USOS API client.
:param api_base_address: The base address of the USOS API.
:param consumer_key: Consumer key obtained from the USOS API.
:param consumer_secret: Consumer secret obtained from the USOS API.
:param trust_env: Whether to trust the environment variables for the connection, see https://docs.aiohttp.org/en/stable/client_reference.html#aiohttp.ClientSession for more information.
"""
self.connection = USOSAPIConnection(
api_base_address, consumer_key, consumer_secret
api_base_address, consumer_key, consumer_secret, trust_env
)
self.user_service = UserService(self.connection)
self.group_service = GroupService(self.connection)
Expand Down
14 changes: 11 additions & 3 deletions usos_api/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,27 @@ class USOSAPIConnection:
A connection to the USOS API.
"""

def __init__(self, api_base_address: str, consumer_key: str, consumer_secret: str):
def __init__(
self,
api_base_address: str,
consumer_key: str,
consumer_secret: str,
trust_env: bool = False,
):
"""
Initialize the USOS API connection.
:param api_base_address: The base address of the USOS API.
:param consumer_key: Consumer key obtained from the USOS API.
:param consumer_secret: Consumer secret obtained from the USOS API.
:param trust_env: Whether to trust the environment variables for the connection, see https://docs.aiohttp.org/en/stable/client_reference.html#aiohttp.ClientSession for more information.
"""
self.base_address = api_base_address.rstrip("/") + "/"
self.auth_manager = AuthManager(
self.base_address, consumer_key, consumer_secret
self.base_address, consumer_key, consumer_secret, trust_env
)
self._session = None
self.trust_env = trust_env

async def __aenter__(self) -> "USOSAPIConnection":
"""
Expand All @@ -52,7 +60,7 @@ async def open(self):
"""
Open the connection.
"""
self._session = aiohttp.ClientSession()
self._session = aiohttp.ClientSession(trust_env=self.trust_env)
await self.auth_manager.open()

async def close(self):
Expand Down

0 comments on commit e1a96e5

Please sign in to comment.