Skip to content

Commit

Permalink
feat: add method to get api details (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
jyecusch authored Jan 9, 2023
2 parents 0a515a1 + eb03a1d commit 9cf122f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
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

0 comments on commit 9cf122f

Please sign in to comment.