Introduction to google endpoints

25
Introduction to Google Endpoints SHINTO ANTO

Transcript of Introduction to google endpoints

Page 1: Introduction to google endpoints

Introduction to Google Endpoints

SHINTO ANTO

Page 2: Introduction to google endpoints

Your Application

How can you build a shared backend for web clients and mobile clients such as Android and IOS.

Page 3: Introduction to google endpoints

• How to create a shared backend for the web and mobile clients?

• How to handle the scalability and availability?

• How to migrate the current backend version to all the clients?

Page 4: Introduction to google endpoints

Endpoints provides a simple way to develop a shared web backend and also provides critical infrastructures, such as OAuth 2.0 authentication, eliminating a great deal of work that would otherwise be needed.

Page 5: Introduction to google endpoints

Google Cloud Endpoints consists of tools, libraries and capabilities that allow you to generate APIs and client libraries from an App Engine application, referred to as an API backend, to simplify client access to data from other applications

Page 6: Introduction to google endpoints

Google Cloud Endpoints

Cloud Endpoints makes it easy to expose your code via RESTful API & RPC services that can easily be consumed by web and mobile applications.

Page 7: Introduction to google endpoints

V1 V2

API Burgeoning support by Cloud Endpoints

Different Api versions can co-exist and you can do a gradual stage of roll out of these changes.

New clients can talk to V2 where as the older ones can slowly move over until we deprecate the V1

Page 8: Introduction to google endpoints
Page 9: Introduction to google endpoints

Why App Engine?

Page 10: Introduction to google endpoints

With App Engine comes the ability to scale very easily.

As we get more traffic Google automatically spins up more app engine instances.

When the traffic levels are off the peak it kills off instances that are not needed anymore

Page 11: Introduction to google endpoints

Endpoints libraries, tools, and samples

Google Cloud Endpoints

Google Cloud Endpoints provide the following libraries and tools:• The Endpoints library in the SDK.• Maven artifacts for creating new backend API projects,

generating client libraries and discovery docs• Alternatively to Maven, the endpoints.sh command line

tool (for Linux), or endpoints.cmd (for Windows) that you can use to generate client libraries and discovery documents.

• Another alternative to Maven is Endpoints support in the Google Plugin for Eclipse.

Page 12: Introduction to google endpoints

Google Cloud Endpoints

• To use Endpoints, be sure you're using the latest version of the Google App Engine Java SDK.

• If you are using the Google Plugin for Eclipse (GPE) to generate endpoints and client libraries, you'll need the latest GPE version.

• You'll also need to be familiar with development using Google App Engine and the Java runtime.

• And finally, you'll need to know how to develop the client of your choice, such as JavaScript web clients, or mobile clients of your choice, such as Android or iOS.

Requirements

Page 13: Introduction to google endpoints

Google Cloud Endpoints

• Create your backend API project (using Maven to do this is the easiest method), then write your API backend code.

• Annotate your API backend code, so classes and client libraries can be generated from it. (Alternatively, use the Google Plugin for Eclipse, which can annotate for you.)

• Generate the client library using Maven, or alternatively, the endpoints.sh command line tool. (Another alternative is to use the Google Plugin for Eclipse to generate the client library.)

• Write your client app, using the client library when making calls to the API backend.

The development process

Page 14: Introduction to google endpoints

Endpoint Annotations• @Api: API-Scoped Annotations• @ApiMethod: Method-Scoped Annotations• @Named• @ApiClass• @ApiNamespace

• Required Imports– import com.google.api.server.spi.config.Api;

import com.google.api.server.spi.config.AnnotationBoolean;import com.google.api.server.spi.config.ApiMethod;import com.google.api.server.spi.config.ApiMethod.HttpMethod;import javax.inject.Named;

Page 15: Introduction to google endpoints

/** * Defines conference APIs. */@Api(name = "conference", version = "v1", scopes = {Constants.EMAIL_SCOPE }, clientIds = {Constants.WEB_CLIENT_ID,Constants.API_EXPLORER_CLIENT_ID }, description = "API for the Conference Central Backend application.")

public class ConferenceApi {/** * Returns a Conference object with the given conferenceId. * */ @ApiMethod(name = "getConference“,path = "conference/{websafeConferenceKey}“, httpMethod = HttpMethod.GET) public Conference getConference(@Named("websafeConferenceKey") final String websafeConferenceKey) throws NotFoundException { Key<Conference> conferenceKey = Key.create(websafeConferenceKey); Conference conference = ofy().load().key(conferenceKey).now(); if (conference == null) { throw new NotFoundException("No Conference found with key: " + websafeConferenceKey); } return conference; }

Page 16: Introduction to google endpoints

<?xml version="1.0" encoding="utf-8"?><appengine-web-app xmlns="http://appengine.google.com/ns/1.0">    <application>your-app-id</application>    <version>version_number</version>    <threadsafe>true</threadsafe>

    <system-properties>        <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>    </system-properties></appengine-web-app>

appengine-web.xml

Required Files and Configuration

Page 17: Introduction to google endpoints

web.xml<?xml version="1.0" encoding="utf-8" standalone="no"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">    <servlet>        <servlet-name>SystemServiceServlet</servlet-name>        <servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class>        <init-param>            <param-name>services</param-name>            <param-value>com.google.devrel.training.conference.spi.ConferenceApi</param-value>        </init-param>    </servlet>    <servlet-mapping>        <servlet-name>SystemServiceServlet</servlet-name>        <url-pattern>/_ah/spi/*</url-pattern>    </servlet-mapping>    <welcome-file-list>        <welcome-file>index.html</welcome-file>    </welcome-file-list></web-app>

Page 18: Introduction to google endpoints

Running and testing API backends locally

• Start the API backend by invoking the development app server.

• In your browser, visit the URL

• Double-click the API you created to display the list of its methods.

mvn appengine:devserver

Wait till...INFO: Dev App Server is now running.

http://localhost:8080/_ah/api/explorer

Page 19: Introduction to google endpoints

Deploying an API backend• Change directory to your project main directory

containing the pom.xml file.

• Invoke Maven as follows:

• After you deploy, you can use the API Explorer to experiment with your API without using a client app by visiting.

mvn appengine:update

https://your_app_id.appspot.com/_ah/api/explorer

Page 20: Introduction to google endpoints

Api Explorer

Page 21: Introduction to google endpoints

List of Created Api’s

Page 22: Introduction to google endpoints

Parameters for API Call

Page 23: Introduction to google endpoints
Page 24: Introduction to google endpoints

Enabling a JavaScript client for Endpoints

• Add the script that includes the Google-hosted JavaScript client library to your HTML:

• Notice that the JavaScript line above includes an onload property specifying the function to be used as a callback after the library loads. Replace "init" with the name of your page initialization function if you did not name it init.

• Inside your callback function, load your Endpoint:

<script src="https://apis.google.com/js/client.js?onload=init"></script><script src="https://apis.google.com/js/client.js?onload=init"></script>

<script src="https://apis.google.com/js/client.js?onload=init"></script>

function init() { gapi.client.load('conference', 'v1', null, '//' + window.location.host +'/_ah/api'); gapi.client.load('oauth2', 'v2', function () { angular.bootstrap(document, ['conferenceApp']); }); };

Page 25: Introduction to google endpoints

So Start Coding Now