This page shows you how to configure, deploy, and send requests to a sample API by using Cloud Endpoints Frameworks for Python. Endpoints Frameworks for Python is integrated with the App Engine standard Python 2.7 runtime environment. Endpoints Frameworks consists of tools, libraries, and capabilities that let you generate APIs and client libraries from an App Engine application.
Use the following high-level task list as you work through the tutorial. All tasks are required to successfully send requests to the API.
In this document, you use the following billable components of Google Cloud:
To generate a cost estimate based on your projected usage,
use the pricing calculator.
When you finish the tasks that are described in this document, you can avoid continued billing by deleting the resources that you created. For more information, see Clean up.
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
roles/resourcemanager.projectCreator), which contains the
resourcemanager.projects.create permission. Learn how to grant
roles.
Verify that billing is enabled for your Google Cloud project.
app-engine-python
and app-engine-python-extras gcloud components.
gcloud components update
gcloud) is authorized to access
your data and services on Google Cloud:
gcloud auth login
gcloud config set project [YOUR_PROJECT_ID]
Replace [YOUR_PROJECT_ID] with your Google Cloud
project ID. If you have other Google Cloud projects, and you want
to use gcloud to manage them, see
Managing gcloud CLI
configurations.
You need an application to send requests to the sample API.
curl, which typically comes pre-installed on your operating system.
If you don't have curl, you can download it from the curl
Releases and downloads page.
Invoke-WebRequest, which is
supported in PowerShell 3.0 and later.
build-essential)
or the Python development headers (sometimes in a package called
python-dev).
For Linux, set the ENDPOINTS_GAE_SDK environment variable to
the path of your App Engine SDK folder:
[Path-to-Google-Cloud-SDK]/platform/google_appengine.
Replace [Path-to-Google-Cloud-SDK] with the output of the
following command:
gcloud info --format="value(installation.sdk_root)"
gcloud app regions list
[YOUR_PROJECT_ID] with your Google Cloud
project ID and [YOUR_REGION] with the region that you want
the App Engine application created in.
gcloud app create \ --project=[YOUR_PROJECT_ID] \ --region=[YOUR_REGION]
For example:
gcloud app create --project=example-project-12345 --region=us-central
To clone the sample API from GitHub:
Clone the sample repository to your local machine:
git clone https://github.com/GoogleCloudPlatform/python-docs-samples
Change to the directory containing the sample code:
cd python-docs-samples/appengine/standard/endpoints-frameworks-v2/echo
To configure Endpoints, you first have to install the Endpoints Frameworks Python library. You then use a tool from the Endpoints Frameworks library to generate an OpenAPI document for the sample API. You need the Endpoints Frameworks library and the OpenAPI document so that Endpoints can manage the API. For more information see Adding API management.
This section walks you through using Python's pip to add the
Endpoints Frameworks library to the sample API's project directory.
To add the Endpoints Frameworks library to the sample:
Make sure you are in the sample API's main directory,
python-docs-samples/appengine/standard/endpoints-frameworks-v2/echo.
Make a /lib subdirectory in the project:
mkdir lib
From the sample main directory
python-docs-samples/appengine/standard/endpoints-frameworks-v2/echo,
run the install command:
pip install --target lib --requirement requirements.txt --ignore-installed
Note the following:
This pip command may use GNU Compiler Collection (GCC) to compile
extension modules. If you are on macOS and this is the first time you have
run GCC on your system, you may have to accept Apple's XCode license. To
do so, run sudo xcodebuild -license.
If you have multiple Python versions installed on your computer, make sure
you are using a version of pip corresponding to the Python version you
are using in this tutorial. Version mismatches (pip from Python 3.4
while using python from Python 2.7, for example) can cause errors which
can be difficult to understand. If need be, you can run pip as a Python
module: replace the pip in the preceding command with python -m pip.
If pip is unable to find a suitable package when running the command,
try upgrading it by running pip install --upgrade pip. After the upgrade
is complete, try the installation command again.
On some Debian and Ubuntu releases pip might fail with
DistutilsOptionError. If you see this error, add --system flag.
On successful completion, the lib directory is populated with the files
required to build the Endpoints Frameworks application.
You use a tool from the Endpoints Frameworks library to generate a document that describes the sample code's REST API.
To generate the OpenAPI document:
Make sure you are in the sample main directory:
python-docs-samples/appengine/standard/endpoints-frameworks-v2/echo
Generate the OpenAPI document:
python lib/endpoints/endpointscfg.py get_openapi_spec main.EchoApi --hostname [YOUR_PROJECT_ID].appspot.com
Replace [YOUR_PROJECT_ID] with your Google Cloud project ID. Ignore
the warnings that are displayed. The Endpoints tool generates
an OpenAPI document called echov1openapi.json in the current directory.
The Endpoints tool names the file based on the name and
version of the service specified in the @endpoints.api decorator. See
Creating the API
for more information.
Endpoints uses the text specified in the hostname argument
as the service name. The YOUR_PROJECT_ID.appspot.com name format matches
the DNS entry that is created automatically when you deploy the API to the
App Engine backend. So in this case, both the
Endpoints service name and fully qualified domain name (FQDN)
are the same.
On successful completion, the following message is displayed:
OpenAPI spec written to ./echov1openapi.json
To deploy the Endpoints configuration, you use Service Infrastructure, Google's foundational services platform, used by Endpoints and other services to create and manage APIs and services.
To deploy the configuration file:
python-docs-samples/appengine/standard/endpoints-frameworks-v2/echo
gcloud endpoints services deploy echov1openapi.json
This creates a new Endpoints service with the name that you
specified in the hostname argument when you ran the
Endpoints tool to generate the OpenAPI document. No matter what
the Endpoints service name is, when you deploy the API on
App Engine, a DNS record is created using the name format
YOUR_PROJECT_ID.appspot.com, which is the FQDN that you use when
you send requests to the API.
As it is creating and configuring the service, Service Management outputs a
great deal of information to the terminal. You can safely ignore the warnings
about the paths in echov1openapi.json not requiring an API key.
When the deployment completes, a message similar to the following displays:
Service Configuration [2017-02-13r2] uploaded for service [example-project-12345.appspot.com]
In the preceding example, 2017-02-13-r2 is the service
configuration ID and example-project-12345.appspot.com is the
service name.
See gcloud
endpoints services deploy in the gcloud reference
documentation for more information.
| Name | Title |
|---|---|
servicemanagement.googleapis.com |
Service Management API |
servicecontrol.googleapis.com |
Service Control API |
In most cases, the gcloud endpoints services deploy command enables these
required services. However, the gcloud command completes successfully but
doesn't enable the required services in the following circumstances:
If you used a third-party application such as Terraform, and you don't include these services.
You deployed the Endpoints configuration to an existing Google Cloud project in which these services were explicitly disabled.
Use the following command to confirm that the required services are enabled:
gcloud services list
If you do not see the required services listed, enable them:
gcloud services enable servicemanagement.googleapis.com
gcloud services enable servicecontrol.googleapis.comAlso enable your Endpoints service:
gcloud services enable ENDPOINTS_SERVICE_NAME
To determine the ENDPOINTS_SERVICE_NAME you can either:
After deploying the Endpoints configuration, go to the Endpoints page in the Cloud console. The list of possible ENDPOINTS_SERVICE_NAME are shown under the Service name column.
For OpenAPI, the ENDPOINTS_SERVICE_NAME is what you specified in the host field of your OpenAPI spec. For gRPC, the ENDPOINTS_SERVICE_NAME is what you specified in the name field of your gRPC Endpoints configuration.
For more information about the gcloud commands, see
gcloud services.
After deploying the Endpoints configuration, you can run the sample locally by using the Local development server.
Make sure you are in the sample main directory:
python-docs-samples/appengine/standard/endpoints-frameworks-v2/echo
Start the local development server:
dev_appserver.py ./app.yaml
By default, the development server listens on http://localhost:8080,
as indicated in the Google Cloud console logs printed by dev_appserver.py:
INFO 2018-01-01 [...] Starting module "default" running at: http://localhost:8080
Send a request to the local development server:
curl \
--request POST \
--header "Content-Type: application/json" \
--data '{"message":"hello world"}' \
http://localhost:8080/_ah/api/echo/v1/echo
In the preceding curl:
--data option specifies the data to post to the API.--header option specifies that the data is in JSON format.(Invoke-WebRequest -Method POST -Body '{"message": "hello world"}' `
-Headers @{"content-type"="application/json"} `
-URI "http://localhost:8080/_ah/api/echo/v1/echo").Content
In the previous example, the first two lines end in a backtick. When you paste the example into PowerShell, make sure there isn't a space following the backticks. For information about the options used in the example request, see Invoke-WebRequest in the Microsoft documentation.
The API echoes back the message that you send it, and responds with the following:
{
"message": "hello world"
}
So far you have deployed the OpenAPI document to Service Management, but you haven't yet deployed the code that serves the API backend. This section walks you through deploying the sample API to App Engine.
To deploy the API backend:
gcloud endpoints configs list --service=[YOUR_PROJECT_ID].appspot.com
Replace [YOUR_PROJECT_ID] with your project ID.
For example:
gcloud endpoints configs list --service=example-project-12345.appspot.com
app.yaml file in the
python-docs-samples/appengine/standard/endpoints-frameworks-v2/echo
directory.