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

feat: add method to get api details #90

Merged
merged 1 commit into from
Jan 9, 2023
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
3 changes: 2 additions & 1 deletion nitric/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#
"""Nitric Python SDK API Documentation. See: https://nitric.io/docs?lang=python for full framework documentation."""

from nitric.resources.apis import Api, api, MethodOptions, ApiOptions, JwtSecurityDefinition
from nitric.resources.apis import Api, api, MethodOptions, ApiOptions, ApiDetails, JwtSecurityDefinition
from nitric.resources.buckets import Bucket, bucket
from nitric.resources.collections import Collection, collection
from nitric.resources.queues import Queue, queue
Expand All @@ -30,6 +30,7 @@
"api",
"Api",
"ApiOptions",
"ApiDetails",
"JwtSecurityDefinition",
"MethodOptions",
"bucket",
Expand Down
32 changes: 32 additions & 0 deletions nitric/resources/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,26 @@
ApiSecurityDefinition,
ApiSecurityDefinitionJwt,
ResourceDeclareRequest,
ResourceDetailsRequest,
)
from grpclib import GRPCError
from nitric.api.exception import exception_from_grpc_error


@dataclass
class ApiDetails:
"""Represents the APIs deployment details."""

# the identifier of the resource
id: str
# The provider this resource is deployed with (e.g. aws)
provider: str
# The service this resource is deployed on (e.g. ApiGateway)
service: str
# The url of the API
url: str


@dataclass
class JwtSecurityDefinition:
"""Represents the JWT security definition for an API."""
Expand Down Expand Up @@ -248,6 +263,23 @@ def decorator(function: HttpMiddleware):

return decorator

async def _details(self) -> ApiDetails:
"""Get the API deployment details."""
try:
res = await self._resources_stub.details(
resource_details_request=ResourceDetailsRequest(
resource=_to_resource(self),
)
)
return ApiDetails(res.id, res.provider, res.service, res.api.url)
except GRPCError as grpc_err:
raise exception_from_grpc_error(grpc_err)

async def URL(self) -> str:
"""Get the APIs live URL."""
details = await self._details()
return details.url


class Route:
"""An HTTP route."""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def get_current_version_tag():
],
setup_requires=["wheel"],
install_requires=[
"nitric-api==0.18.0",
"nitric-api==0.21.0",
"protobuf==3.19.4",
"asyncio",
],
Expand Down