-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
314 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# | ||
# Copyright (c) 2024, Neptune Labs Sp. z o.o. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
from __future__ import annotations | ||
|
||
__all__ = ["ApiClient"] | ||
|
||
|
||
from dataclasses import dataclass | ||
|
||
from neptune_api import ( | ||
AuthenticatedClient, | ||
Client, | ||
) | ||
from neptune_api.api.backend import get_client_config | ||
from neptune_api.api.data_ingestion import submit_operation | ||
from neptune_api.auth_helpers import exchange_api_key | ||
from neptune_api.credentials import Credentials | ||
from neptune_api.models import ( | ||
ClientConfig, | ||
Error, | ||
) | ||
from neptune_api.proto.neptune_pb.ingest.v1.pub.ingest_pb2 import RunOperation | ||
|
||
from neptune_scale.core.components.abstract import Resource | ||
|
||
|
||
class ApiClient(Resource): | ||
def __init__(self, api_token: str) -> None: | ||
credentials = Credentials.from_api_key(api_key=api_token) | ||
config, token_urls = get_config_and_token_urls(credentials=credentials) | ||
self._backend = create_auth_api_client(credentials=credentials, config=config, token_refreshing_urls=token_urls) | ||
|
||
def submit(self, operation: RunOperation) -> None: | ||
_ = submit_operation.sync(client=self._backend, body=operation) | ||
|
||
def cleanup(self) -> None: | ||
pass | ||
|
||
def close(self) -> None: | ||
self._backend.__exit__() | ||
|
||
|
||
@dataclass | ||
class TokenRefreshingURLs: | ||
authorization_endpoint: str | ||
token_endpoint: str | ||
|
||
@classmethod | ||
def from_dict(cls, data: dict) -> TokenRefreshingURLs: | ||
return TokenRefreshingURLs( | ||
authorization_endpoint=data["authorization_endpoint"], token_endpoint=data["token_endpoint"] | ||
) | ||
|
||
|
||
def get_config_and_token_urls(*, credentials: Credentials) -> tuple[ClientConfig, TokenRefreshingURLs]: | ||
with Client(base_url=credentials.base_url) as client: | ||
config = get_client_config.sync(client=client) | ||
if config is None or isinstance(config, Error): | ||
raise RuntimeError(f"Failed to get client config: {config}") | ||
response = client.get_httpx_client().get(config.security.open_id_discovery) | ||
token_urls = TokenRefreshingURLs.from_dict(response.json()) | ||
return config, token_urls | ||
|
||
|
||
def create_auth_api_client( | ||
*, credentials: Credentials, config: ClientConfig, token_refreshing_urls: TokenRefreshingURLs | ||
) -> AuthenticatedClient: | ||
return AuthenticatedClient( | ||
base_url=credentials.base_url, | ||
credentials=credentials, | ||
client_id=config.security.client_id, | ||
token_refreshing_endpoint=token_refreshing_urls.token_endpoint, | ||
api_key_exchange_callback=exchange_api_key, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from __future__ import annotations | ||
|
||
__all__ = ("datetime_to_proto", "make_step") | ||
|
||
from datetime import datetime | ||
|
||
from google.protobuf.timestamp_pb2 import Timestamp | ||
from neptune_api.proto.neptune_pb.ingest.v1.common_pb2 import Step | ||
|
||
|
||
def datetime_to_proto(dt: datetime) -> Timestamp: | ||
dt_ts = dt.timestamp() | ||
return Timestamp(seconds=int(dt_ts), nanos=int((dt_ts % 1) * 1e9)) | ||
|
||
|
||
def make_step(number: float | int, raise_on_step_precision_loss: bool = False) -> Step: | ||
""" | ||
Converts a number to protobuf Step value. Example: | ||
>>> assert make_step(7.654321, True) == Step(whole=7, micro=654321) | ||
Args: | ||
number: step expressed as number | ||
raise_on_step_precision_loss: inform converter whether it should silently drop precision and | ||
round down to 6 decimal places or raise an error. | ||
Returns: Step protobuf used in Neptune API. | ||
""" | ||
m = int(1e6) | ||
micro: int = int(number * m) | ||
if raise_on_step_precision_loss and number * m - micro != 0: | ||
raise ValueError(f"step must not use more than 6-decimal points, got: {number}") | ||
|
||
whole = micro // m | ||
micro = micro % m | ||
|
||
return Step(whole=whole, micro=micro) |
Oops, something went wrong.