Hay más ejemplos de AWS SDK disponibles en el GitHub repositorio de ejemplos de AWS Doc SDK
Las traducciones son generadas a través de traducción automática. En caso de conflicto entre la traducción y la version original de inglés, prevalecerá la version en inglés.
Úselo CreateRestApi
con un AWS SDK o CLI
En los siguientes ejemplos de código, se muestra cómo utilizar CreateRestApi
.
Los ejemplos de acciones son extractos de código de programas más grandes y deben ejecutarse en contexto. Puede ver esta acción en contexto en el siguiente ejemplo de código:
- CLI
-
- AWS CLI
-
Creación de una API
Comando:
aws apigateway create-rest-api --name '
My First API
' --description 'This is my first API
'Para crear una API duplicada a partir de una API existente
Comando:
aws apigateway create-rest-api --name '
Copy of My First API
' --description 'This is a copy of my first API
' --clone-from1234123412
-
Para obtener información sobre la API, consulte CreateRestApi
la Referencia de AWS CLI comandos.
-
- Java
-
- SDK para Java 2.x
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. public static String createAPI(ApiGatewayClient apiGateway, String restApiId, String restApiName) { try { CreateRestApiRequest request = CreateRestApiRequest.builder() .cloneFrom(restApiId) .description("Created using the Gateway Java API") .name(restApiName) .build(); CreateRestApiResponse response = apiGateway.createRestApi(request); System.out.println("The id of the new api is " + response.id()); return response.id(); } catch (ApiGatewayException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; }
-
Para obtener más información sobre la API, consulta CreateRestApila Referencia AWS SDK for Java 2.x de la API.
-
- Python
-
- SDK para Python (Boto3)
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS
. class ApiGatewayToService: """ Encapsulates Amazon API Gateway functions that are used to create a REST API that integrates with another AWS service. """ def __init__(self, apig_client): """ :param apig_client: A Boto3 API Gateway client. """ self.apig_client = apig_client self.api_id = None self.root_id = None self.stage = None def create_rest_api(self, api_name): """ Creates a REST API on API Gateway. The default API has only a root resource and no HTTP methods. :param api_name: The name of the API. This descriptive name is not used in the API path. :return: The ID of the newly created API. """ try: result = self.apig_client.create_rest_api(name=api_name) self.api_id = result["id"] logger.info("Created REST API %s with ID %s.", api_name, self.api_id) except ClientError: logger.exception("Couldn't create REST API %s.", api_name) raise try: result = self.apig_client.get_resources(restApiId=self.api_id) self.root_id = next( item for item in result["items"] if item["path"] == "/" )["id"] except ClientError: logger.exception("Couldn't get resources for API %s.", self.api_id) raise except StopIteration as err: logger.exception("No root resource found in API %s.", self.api_id) raise ValueError from err return self.api_id
-
Para obtener más información sobre la API, consulta CreateRestApila AWS Referencia de API de SDK for Python (Boto3).
-