In this lab you will deploy a sample API with Google Cloud Endpoints, which are a set of tools for generating APIs from within an App Engine application. The sample code will include:
A REST API that you can query to find the name of an airport from its three-letter IATA code (for example, SFO, JFK, AMS).
A script that uploads the API configuration to Cloud Endpoints.
A script that deploys a Google App Engine flexible backend to host the sample API.
After you send some requests to the sample API, you can view the Cloud Endpoints Activity Graphs and Logs. These are tools that allow you to monitor your APIs and gain insights into their usage
Task 1. Getting the sample code
Enter the following command in Cloud Shell to get the sample API and scripts:
gsutil cp gs://spls/gsp164/endpoints-quickstart.zip .
unzip endpoints-quickstart.zip
Copied!
Change to the directory that contains the sample code:
cd endpoints-quickstart















Task 2. Deploying the Endpoints configuration
To publish a REST API to Endpoints, an OpenAPI configuration file that describes the API is required. The lab's sample API comes with a pre-configured OpenAPI file called openapi.yaml.
Endpoints uses Google Service Management, an infrastructure service of Google Cloud, to create and manage APIs and services. To use Endpoints to manage an API, you deploy the API's OpenAPI configuration to Service Management.
To deploy the Endpoints configuration...
In the endpoints-qwikstart directory, enter the following:
cd scripts
Copied!
Run the following script, which is included in the sample:
./deploy_api.sh
Copied!
Cloud Endpoints uses the host field in the OpenAPI configuration file to identify the service. The deploy_api.sh script sets the ID of your Cloud project as part of the name configured in the host field. (When you prepare an OpenAPI configuration file for your own service, you will need to do this manually.)
The script then deploys the OpenAPI configuration to Service Management using the command: gcloud endpoints services deploy openapi.yaml
As it is creating and configuring the service, Service Management outputs some information to the console. You can safely ignore the warnings about the paths in openapi.yaml not requiring an API key. On successful completion, you see a line like the following that displays the service configuration ID and the service
Task 3. Deploying the API backend
So far you have deployed the OpenAPI configuration to Service Management, but you have not yet deployed the code that will serve the API backend. The deploy_app.sh script included in the lab sample creates an App Engine flexible environment to host the API backend, and then the script deploys the API to App Engine.
To deploy the API backend, make sure you are in the endpoints-quickstart/scripts directory. Then, run the following script:
./deploy_app.sh
Task 4. Sending requests to the API
After deploying the sample API, you can send requests to it by running the following script:
./query_api.sh
Copied!
The script echoes the curl command that it uses to send a request to the API, and then displays the result. You'll see something like the following in Cloud Shell:
curl "https://example-project.appspot.com/airportName?iataCode=SFO"
San Francisco International Airport
The API expects one query parameter, iataCode, that is set to a valid IATA airport code such as SEA or JFK.
To test, run this example in Cloud Shell:
./query_api.sh JFK
Copied!
You just deployed and tested an API in Cloud Endpoints!
No comments:
Post a Comment