1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.components.devtools_bridge.apiary;
7 import android.util.JsonReader;
9 import org.apache.http.HttpResponse;
10 import org.apache.http.client.HttpClient;
11 import org.apache.http.client.HttpResponseException;
12 import org.apache.http.client.ResponseHandler;
13 import org.apache.http.client.methods.HttpDelete;
14 import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
15 import org.apache.http.client.methods.HttpGet;
16 import org.apache.http.client.methods.HttpPost;
17 import org.apache.http.client.methods.HttpRequestBase;
18 import org.apache.http.entity.StringEntity;
20 import org.chromium.components.devtools_bridge.commands.Command;
21 import org.chromium.components.devtools_bridge.gcd.InstanceCredential;
22 import org.chromium.components.devtools_bridge.gcd.InstanceDescription;
23 import org.chromium.components.devtools_bridge.gcd.MessageReader;
24 import org.chromium.components.devtools_bridge.gcd.MessageWriter;
26 import java.io.IOException;
27 import java.io.UnsupportedEncodingException;
31 * Client for accessing GCD API.
33 public class GCDClient {
34 private static final String API_BASE = "https://www.googleapis.com/clouddevices/v1";
35 public static final String ENCODING = "UTF-8";
36 protected static final String CONTENT_TYPE = "application/json; charset=" + ENCODING;
38 protected final HttpClient mHttpClient;
39 private final String mAPIKey;
40 private final String mOAuthToken;
42 GCDClient(HttpClient httpClient, String apiKey, String oAuthToken) {
43 mHttpClient = httpClient;
45 mOAuthToken = oAuthToken;
48 GCDClient(HttpClient httpClient, String apiKey) {
49 this(httpClient, apiKey, null);
53 * Creation of a registration ticket is the first step in instance registration. Client must
54 * have user credentials. If the ticket has been registered it will be associated with the
55 * user. Next step is registration ticket patching.
57 public final String createRegistrationTicket() throws IOException {
58 assert mOAuthToken != null;
60 return mHttpClient.execute(
61 newHttpPost("/registrationTickets", "{\"userEmail\":\"me\"}"),
62 new JsonResponseHandler<String>() {
64 public String readResponse(JsonReader reader) throws IOException {
65 return new MessageReader(reader).readTicketId();
71 * Patching registration ticket. GCD gets device definition including commands metadata,
72 * GCM channel description and user-visible instance name.
74 public final void patchRegistrationTicket(String ticketId, InstanceDescription description)
76 String content = new MessageWriter().writeTicketPatch(description).close().toString();
79 newHttpPatch("/registrationTickets/" + ticketId, content),
80 new EmptyResponseHandler());
84 * Finalizing registration. Client must be anonymous (GCD requirement). GCD provides
85 * instance credentials needed for handling commands.
87 public final InstanceCredential finalizeRegistration(String ticketId) throws IOException {
88 return mHttpClient.execute(
89 newHttpPost("/registrationTickets/" + ticketId + "/finalize", ""),
90 new JsonResponseHandler<InstanceCredential>() {
92 public InstanceCredential readResponse(JsonReader reader) throws IOException {
93 return new MessageReader(reader).readInstanceCredential();
99 * Deletes registered instance (unregisters). If client has instance credentials then
100 * instanceId must be it's own ID. If client has user credentials then instance must belong
103 public void deleteInstance(String instanceId) throws IOException {
105 newHttpDelete("/devices/" + instanceId),
106 new EmptyResponseHandler());
110 * Patches the command (previously received with Notification) with out-parameters or
113 public final void patchCommand(Command command) throws IOException {
114 String content = new MessageWriter().writeCommandPatch(command).close().toString();
117 newHttpPatch("/commands/" + command.id, content),
118 new EmptyResponseHandler());
121 protected final HttpGet newHttpGet(String path) {
122 return prepare(new HttpGet(buildUrl(path)));
125 protected final HttpPost newHttpPost(String path, String content)
126 throws UnsupportedEncodingException {
127 return prepare(new HttpPost(buildUrl(path)), content);
130 protected final HttpPost newHttpPost(String path, String query, String content)
131 throws UnsupportedEncodingException {
132 return prepare(new HttpPost(buildUrl(path, query)), content);
135 protected final HttpPatch newHttpPatch(String path, String content)
136 throws UnsupportedEncodingException {
137 return prepare(new HttpPatch(buildUrl(path)), content);
140 protected final HttpDelete newHttpDelete(String path) {
141 return prepare(new HttpDelete(buildUrl(path)));
144 private String buildUrl(String path) {
145 return API_BASE + path + "?key=" + mAPIKey;
148 private String buildUrl(String path, String query) {
149 return API_BASE + path + "?" + query + "&key=" + mAPIKey;
152 private <T extends HttpEntityEnclosingRequestBase> T prepare(T request, String content)
153 throws UnsupportedEncodingException {
154 request.setEntity(new StringEntity(content, ENCODING));
155 request.addHeader("Content-Type", CONTENT_TYPE);
156 return prepare(request);
159 private <T extends HttpRequestBase> T prepare(T request) {
160 if (mOAuthToken != null) {
161 request.addHeader("Authorization", "Bearer " + mOAuthToken);
166 private static final class HttpPatch extends HttpEntityEnclosingRequestBase {
167 public HttpPatch(String uri) {
168 setURI(URI.create(uri));
171 public String getMethod() {
176 private static class EmptyResponseHandler implements ResponseHandler<Void> {
178 public Void handleResponse(HttpResponse response) throws HttpResponseException {
179 JsonResponseHandler.checkStatus(response);