Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove token renewal #39

Merged
merged 11 commits into from
Sep 14, 2022
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions pkg/reduct/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/reduct/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
37 changes: 12 additions & 25 deletions pkg/reduct/http.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Internal HTTP helper"""
import json
from contextlib import asynccontextmanager
from typing import Optional, AsyncIterator

Expand All @@ -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
Expand All @@ -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:
Expand Down