-
Notifications
You must be signed in to change notification settings - Fork 2
/
EndpointAPI.java
104 lines (79 loc) · 3.69 KB
/
EndpointAPI.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package com.victor.kaiser.pendergrast.glass.server;
import java.io.IOException;
import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiMethod.HttpMethod;
import com.google.appengine.api.oauth.OAuthRequestException;
import com.google.appengine.api.users.User;
import com.google.apphosting.datastore.EntityV4.PartitionId.Constants;
import com.victor.kaiser.pendergrast.glass.server.data.UserData;
import com.victor.kaiser.pendergrast.glass.server.data.UserDatabase;
@Api(name = "endpoint", version = "v1")
public class EndpointAPI {
/**
* All of the authentication constants
*/
private static class Constants {
public static final String WEB_CLIENT_ID = "81905218945-ahl3tchl11rcsviiivne7unpl5mnijfh.apps.googleusercontent.com";
public static final String DEVICE_CLIENT_ID = "81905218945.apps.googleusercontent.com";
//public static final String ANDROID_CLIENT_ID1 = "965606050172-6ovcrmugbs5na7p9i1et1tjbf4isv08p.apps.googleusercontent.com";
//public static final String ANDROID_CLIENT_ID2 = "965606050172.apps.googleusercontent.com";
public static final String ANDROID_AUDIENCE = WEB_CLIENT_ID;
public static final String SCOPE_EMAIL = "https://www.googleapis.com/auth/userinfo.email";
public static final String SCOPE_PLUS_PROFILE = "https://www.googleapis.com/auth/plus.me";
}
/**
* Used by the client to retrieve notes
*/
@ApiMethod(name = "notes.list", path = "notes_get", httpMethod = HttpMethod.GET, scopes = { Constants.SCOPE_EMAIL }, clientIds = { Constants.WEB_CLIENT_ID, Constants.DEVICE_CLIENT_ID }, audiences = { Constants.WEB_CLIENT_ID })
public UserData getNotes(User user) throws OAuthRequestException, IOException {
// Check that the user is signed in
if (user == null) {
throw new OAuthRequestException("Couldn't authenticate");
}
//UserData userData = UserDatabase.getUserByEmail(user.getEmail());
UserData userData = UserDatabase.getUserFromDatabaseByEmail(user.getEmail());
if(userData == null){
// Just return the request data with no notes and
// no request time
return new UserData(user.getEmail(), "", 0);
}
return userData;
}
/**
* Used by the client to set notes
*/
@ApiMethod(name = "notes.put", path="notes_put", httpMethod = HttpMethod.PUT, scopes = { Constants.SCOPE_EMAIL }, clientIds = { Constants.WEB_CLIENT_ID, Constants.DEVICE_CLIENT_ID}, audiences = { Constants.ANDROID_AUDIENCE} )
public UserData putNotes(User user, UserData data) throws OAuthRequestException, IOException, IllegalArgumentException {
if (user == null) {
throw new OAuthRequestException("Couldn't authenticate");
}
if(data == null){
return new UserData("null", "No data received", System.currentTimeMillis());
}
try{
// Add the data to the datastore
System.out.println("Adding notes: \"" + data.getNotes() + "\"");
//UserDatabase.putUser(data);
UserDatabase.putUserIntoDatastore(data);
} catch (Exception e){
e.printStackTrace();
return new UserData(user.getEmail(), "Failed to put into database", System.currentTimeMillis());
}
return data;
}
/**
* Called by a cron job to put the contents of the write cache in the datastore.
*/
@ApiMethod(name = "persist_cache", path="persist_cache", httpMethod = HttpMethod.GET)
public UserData persistCache() throws IOException, IllegalArgumentException {
try{
// Add the data to the datastore
UserDatabase.writeCacheToDatastore();
} catch (Exception e){
e.printStackTrace();
return new UserData("Server", "Failed to persist write cache", System.currentTimeMillis());
}
return new UserData("Server", "Persisted the write cache", System.currentTimeMillis());
}
}