From c8375105453b583c8994cab3642494807b3658b4 Mon Sep 17 00:00:00 2001 From: aschenbecherwespe <94011659+aschenbecherwespe@users.noreply.github.com> Date: Wed, 14 Sep 2022 20:26:27 +0200 Subject: [PATCH] Remove token renewal (#39) * remove token renewal * update changelog * remove unused import * remove unneeded loop * shouldn't be yield * fix spelling * still need a token somewhere... * forgot black * fix return types * fix some documentation typos * fix changelog --- CHANGELOG.md | 6 ++++++ pkg/reduct/bucket.py | 10 +++++----- pkg/reduct/client.py | 4 ++-- pkg/reduct/http.py | 37 ++++++++++++------------------------- 4 files changed, 25 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d458ab..80fae8f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [unreleased] + +### Removed: + +- Token renewal - deprecated in API v0.8, [PR-39](https://github.com/reduct-storage/reduct-py/pull/39) + ## [0.4.0] - 2022-08-24 ### Added: diff --git a/pkg/reduct/bucket.py b/pkg/reduct/bucket.py index 8c586f9..fda6543 100644 --- a/pkg/reduct/bucket.py +++ b/pkg/reduct/bucket.py @@ -121,7 +121,7 @@ def __init__(self, name: str, http: HttpClient): async def get_settings(self) -> BucketSettings: """ - Get current settings of bucket + Get current bucket settings Returns: BucketSettings: Raises: @@ -151,7 +151,7 @@ async def info(self) -> BucketInfo: async def get_entry_list(self) -> List[EntryInfo]: """ - Get list of entries with its stats + Get list of entries with their stats Returns: List[EntryInfo] Raises: @@ -172,7 +172,7 @@ async def read(self, entry_name: str, timestamp: Optional[int] = None) -> bytes: Read a record from entry Args: entry_name: name of entry in the bucket - timestamp: UNIX timestamp in microseconds if None get the latest record + timestamp: UNIX timestamp in microseconds - if None: get the latest record Returns: bytes: Raises: @@ -188,7 +188,7 @@ async def read_by( self, entry_name: str, timestamp: Optional[int] = None, chunk_size: int = 1024 ) -> AsyncIterator[bytes]: """ - Read a record from entry by chunks + Read a record from entry in chunks >>> async for chunk in bucket.read_by("entry-1", chunk_size=1024): >>> print(chunk) @@ -254,7 +254,7 @@ async def list( self, entry_name: str, start: int, stop: int ) -> List[Tuple[int, int]]: """ - Get list of records in entry for time interval + Get a list of records in an entry for a specified time interval Args: entry_name: name of entry in the bucket start: the beginning of the time interval diff --git a/pkg/reduct/client.py b/pkg/reduct/client.py index 85b195c..b5b0664 100644 --- a/pkg/reduct/client.py +++ b/pkg/reduct/client.py @@ -57,8 +57,8 @@ def __init__( Args: url: URL to connect to the storage - api_token: API token if the storage uses it for autherization - timeout: total timout for connection, reqest and responose in seconds + api_token: API token if the storage uses it for authorization + timeout: total timeout for connection, request and response in seconds Examples: >>> client = Client("http://127.0.0.1:8383") diff --git a/pkg/reduct/http.py b/pkg/reduct/http.py index bf6a6d1..72a443a 100644 --- a/pkg/reduct/http.py +++ b/pkg/reduct/http.py @@ -1,5 +1,4 @@ """Internal HTTP helper""" -import json from contextlib import asynccontextmanager from typing import Optional, AsyncIterator @@ -17,7 +16,9 @@ def __init__( ): self.url = url self.api_token = api_token - self.headers = {} + self.headers = ( + {"Authorization": f"Bearer {api_token}"} if api_token is not None else {} + ) self.timeout = ClientTimeout(timeout) @asynccontextmanager @@ -30,31 +31,17 @@ async def request(self, method: str, path: str = "", **kwargs) -> ClientResponse del kwargs["content_length"] async with aiohttp.ClientSession(timeout=self.timeout) as session: - while True: # We need cycle to repeat request if the token expires - async with session.request( - method, - f"{self.url}{path.strip()}", - headers=dict(self.headers, **extra_headers), - **kwargs, - ) as response: + async with session.request( + method, + f"{self.url}{path.strip()}", + headers=dict(self.headers, **extra_headers), + **kwargs, + ) as response: - if response.ok: - yield response - break - - if response.status == 401: - # Authentication issue, try to refresh token and repeat request - async with session.post( - f"{self.url}/auth/refresh", - headers={"Authorization": f"Bearer {self.api_token}"}, - ) as auth_resp: - if auth_resp.status == 200: - data = json.loads(await auth_resp.read()) - self.headers = { - "Authorization": f'Bearer {data["access_token"]}' - } - continue + if response.ok: + yield response + else: raise ReductError(response.status, await response.text()) async def request_all(self, method: str, path: str = "", **kwargs) -> bytes: