Skip to content

Commit

Permalink
Add neptune_storage_api
Browse files Browse the repository at this point in the history
  • Loading branch information
kgodlewski committed Jan 30, 2025
1 parent ed8c053 commit 895f83f
Show file tree
Hide file tree
Showing 17 changed files with 1,160 additions and 2 deletions.
2 changes: 1 addition & 1 deletion openapi-generator-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ post_hooks:
- "ruff check . --fix --target-version=py38"
- "ruff format "
- "isort ."
- "echo >> py.typed"
- "echo -n > py.typed"
content_type_overrides:
application/x-protobuf: application/octet-stream
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ exclude = ["src/neptune_api/proto", "src/neptune_retrieval_api/proto"]
select = ["F", "UP"]

[tool.mypy]
files = ['src/neptune_api', 'src/neptune_retrieval_api']
files = ['src/neptune_api', 'src/neptune_retrieval_api', 'src/neptune_storage_api']
mypy_path = "stubs"
install_types = "True"
non_interactive = "True"
Expand Down
86 changes: 86 additions & 0 deletions scripts/update-storagebridge.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/usr/bin/env bash

if [ ! -f "storagebridge_swagger.json" ]; then
echo "storagebridge_swagger.json not found. Make sure it's in CWD when running the script"
exit 1
fi

# Show every command and exit on error
set -ex

## Preserve specific files
mkdir -p tmp


# Function to download Swagger JSON
download_swagger() {
local swagger_url=$1
local output_file=$2

curl -o "${output_file}" "${swagger_url}"
}

# Function to convert Swagger 2.0 to OpenAPI 3.0
convert_swagger_to_openapi() {
local input_file=$1
local output_file=$2

curl -X POST "https://converter.swagger.io/api/convert" \
-H "Content-Type: application/json" \
-d @"${input_file}" \
-o "${output_file}"
}


# Modify the Swagger JSON to support application/x-protobuf
jq '
.paths |= with_entries(
.value |= with_entries(
if .value.produces? and (.value.produces | index("application/json")) and (.value.produces | index("application/x-protobuf"))
then
.value.produces = ["application/x-protobuf"]
else
.
end
)
)
' storagebridge_swagger.json > tmp/storagebridge_swagger.json && mv tmp/storagebridge_swagger.json storagebridge_swagger.json

jq '
.paths |= with_entries(
.value |= with_entries(
if .value.produces? and (.value.produces | index("application/x-protobuf"))
then
.value.responses."200".schema = {
"type": "string",
"format": "binary"
}
else
.
end
)
)
' storagebridge_swagger.json > tmp/storagebridge_swagger.json && mv tmp/storagebridge_swagger.json storagebridge_swagger.json

## Convert the Swagger JSON to OpenAPI 3.0
convert_swagger_to_openapi "storagebridge_swagger.json" "storagebridge_openapi.json"
# Add license information using jq
jq '.info.license = {"name": "", "url": ""}' storagebridge_openapi.json > tmp_storagebridge_openapi.json && mv tmp_storagebridge_openapi.json storagebridge_openapi.json


openapi-python-client generate \
--overwrite \
--meta none \
--path "storagebridge_openapi.json" \
--custom-template-path=templates/ \
--config openapi-generator-config.yaml \
--output-path src/neptune_storage_api


# Clean tmp directories
rm -rf tmp


cat scripts/preserve_files.txt | while read entry; do
git checkout HEAD -- $entry
done
11 changes: 11 additions & 0 deletions src/neptune_storage_api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""A client library for accessing storagebridge"""

from .client import (
AuthenticatedClient,
Client,
)

__all__ = (
"AuthenticatedClient",
"Client",
)
1 change: 1 addition & 0 deletions src/neptune_storage_api/api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Contains methods for accessing the API"""
Empty file.
173 changes: 173 additions & 0 deletions src/neptune_storage_api/api/storagebridge/signed_url.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
from http import HTTPStatus
from typing import (
Any,
Optional,
Union,
cast,
)

import httpx

from ... import errors
from ...client import (
AuthenticatedClient,
Client,
)
from ...models.create_signed_urls_request import CreateSignedUrlsRequest
from ...models.create_signed_urls_response import CreateSignedUrlsResponse
from ...types import Response


def _get_kwargs(
*,
body: CreateSignedUrlsRequest,
) -> dict[str, Any]:
headers: dict[str, Any] = {}

_kwargs: dict[str, Any] = {
"method": "post",
"url": "/api/storagebridge/v1/azure/signedUrl",
}

_body = body.to_dict()

_kwargs["json"] = _body
headers["Content-Type"] = "application/json"

_kwargs["headers"] = headers
return _kwargs


def _parse_response(
*, client: Union[AuthenticatedClient, Client], response: httpx.Response
) -> Optional[Union[Any, CreateSignedUrlsResponse]]:
if response.status_code == 200:
response_200 = CreateSignedUrlsResponse.from_dict(response.json())

return response_200
if response.status_code == 401:
response_401 = cast(Any, None)
return response_401
if response.status_code == 403:
response_403 = cast(Any, None)
return response_403
if response.status_code == 413:
response_413 = cast(Any, None)
return response_413
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None


def _build_response(
*, client: Union[AuthenticatedClient, Client], response: httpx.Response
) -> Response[Union[Any, CreateSignedUrlsResponse]]:
return Response(
status_code=HTTPStatus(response.status_code),
content=response.content,
headers=response.headers,
parsed=_parse_response(client=client, response=response),
)


def sync_detailed(
*,
client: Union[AuthenticatedClient, Client],
body: CreateSignedUrlsRequest,
) -> Response[Union[Any, CreateSignedUrlsResponse]]:
"""
Args:
body (CreateSignedUrlsRequest):
Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.
Returns:
Response[Union[Any, CreateSignedUrlsResponse]]
"""

kwargs = _get_kwargs(
body=body,
)

response = client.get_httpx_client().request(
**kwargs,
)

return _build_response(client=client, response=response)


def sync(
*,
client: Union[AuthenticatedClient, Client],
body: CreateSignedUrlsRequest,
) -> Optional[Union[Any, CreateSignedUrlsResponse]]:
"""
Args:
body (CreateSignedUrlsRequest):
Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.
Returns:
Union[Any, CreateSignedUrlsResponse]
"""

return sync_detailed(
client=client,
body=body,
).parsed


async def asyncio_detailed(
*,
client: Union[AuthenticatedClient, Client],
body: CreateSignedUrlsRequest,
) -> Response[Union[Any, CreateSignedUrlsResponse]]:
"""
Args:
body (CreateSignedUrlsRequest):
Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.
Returns:
Response[Union[Any, CreateSignedUrlsResponse]]
"""

kwargs = _get_kwargs(
body=body,
)

response = await client.get_async_httpx_client().request(**kwargs)

return _build_response(client=client, response=response)


async def asyncio(
*,
client: Union[AuthenticatedClient, Client],
body: CreateSignedUrlsRequest,
) -> Optional[Union[Any, CreateSignedUrlsResponse]]:
"""
Args:
body (CreateSignedUrlsRequest):
Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.
Returns:
Union[Any, CreateSignedUrlsResponse]
"""

return (
await asyncio_detailed(
client=client,
body=body,
)
).parsed
Loading

0 comments on commit 895f83f

Please sign in to comment.