Book Image

Python for Google App Engine

By : Massimiliano Pippi
Book Image

Python for Google App Engine

By: Massimiliano Pippi

Overview of this book

Table of Contents (15 chapters)
Python for Google App Engine
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Implementing API endpoints


As we already mentioned, our REST API will be integrated with the existing App Engine application without altering its behavior, so we need to specify a new WSGI application that will handle the URLs we map to the API endpoints. Let's start with the app.yaml file, where we add the following code:

handlers:
- url: /static
  static_dir: static

- url: /_ah/spi/.*
  script: notes_api.app

- url: .*
  script: main.app

libraries:
- name: webapp2
  version: "2.5.2"

- name: jinja2
  version: latest

- name: endpoints
  version: 1.0

The regular expression that matches API URLs is actually /_ah/spi/.*. Even if we perform requests to an URL such as https://example.com/_ah/api/v1/an-endpoint, Cloud Endpoints will take care of the proper redirects. The handler script of the API URLs points to the app variable in the notes_api module, which we are yet to create. In a new file called notes_api.py, we add the following code:

import endpoints

app = endpoints.api_server([])

This...