From eb03a1d47ef9cc880a4b4989009fe75d77809d75 Mon Sep 17 00:00:00 2001 From: Ryan Cartwright Date: Thu, 22 Dec 2022 14:19:31 +1100 Subject: [PATCH] add method to get api details --- nitric/resources/__init__.py | 3 ++- nitric/resources/apis.py | 32 ++++++++++++++++++++++++++++++++ setup.py | 2 +- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/nitric/resources/__init__.py b/nitric/resources/__init__.py index e63ab5a..ab381e5 100644 --- a/nitric/resources/__init__.py +++ b/nitric/resources/__init__.py @@ -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 @@ -30,6 +30,7 @@ "api", "Api", "ApiOptions", + "ApiDetails", "JwtSecurityDefinition", "MethodOptions", "bucket", diff --git a/nitric/resources/apis.py b/nitric/resources/apis.py index 1da45ec..b435f04 100644 --- a/nitric/resources/apis.py +++ b/nitric/resources/apis.py @@ -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.""" @@ -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.""" diff --git a/setup.py b/setup.py index 02a9b3c..70dce5e 100644 --- a/setup.py +++ b/setup.py @@ -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", ],