forked from dtinit/data-transfer-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RememberTheMilkService.java
165 lines (145 loc) · 6.32 KB
/
RememberTheMilkService.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Copyright 2018 The Data Transfer Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dataportabilityproject.transfer.rememberthemilk.tasks;
import static com.google.common.base.Preconditions.checkState;
import com.fasterxml.jackson.xml.XmlMapper;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.repackaged.com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import java.io.IOException;
import java.net.URL;
import java.util.Map;
import org.dataportabilityproject.transfer.rememberthemilk.model.tasks.GetListResponse;
import org.dataportabilityproject.transfer.rememberthemilk.model.tasks.GetListsResponse;
import org.dataportabilityproject.transfer.rememberthemilk.model.tasks.ListAddResponse;
import org.dataportabilityproject.transfer.rememberthemilk.model.tasks.ListInfo;
import org.dataportabilityproject.transfer.rememberthemilk.model.tasks.RememberTheMilkResponse;
import org.dataportabilityproject.transfer.rememberthemilk.model.tasks.TaskUpdateResponse;
import org.dataportabilityproject.transfer.rememberthemilk.model.tasks.TaskSeries;
import org.dataportabilityproject.transfer.rememberthemilk.model.tasks.TimelineCreateResponse;
class RememberTheMilkService {
private static final String BASE_URL = "https://api.rememberthemilk.com/services/rest/";
private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
private RememberTheMilkSignatureGenerator signatureGenerator;
private XmlMapper xmlMapper = new XmlMapper();
RememberTheMilkService(RememberTheMilkSignatureGenerator signatureGenerator) {
this.signatureGenerator = signatureGenerator;
}
public String createTimeline() throws IOException {
TimelineCreateResponse timelineCreateResponse =
makeRequest(
ImmutableMap.of("method", RememberTheMilkMethods.TIMELINES_CREATE.getMethodName()),
TimelineCreateResponse.class);
checkState(!Strings.isNullOrEmpty(timelineCreateResponse.timeline));
return timelineCreateResponse.timeline;
}
public ListInfo createTaskList(String name, String timeline) throws IOException {
Map<String, String> params =
ImmutableMap.of(
"method",
RememberTheMilkMethods.LISTS_ADD.getMethodName(),
"timeline",
timeline,
"name",
("Copy of: " + name));
ListAddResponse response = makeRequest(params, ListAddResponse.class);
checkState(response.list != null, "Added list is null");
checkState(response.list.id != 0, "Added list has id of zero");
return response.list;
}
public TaskSeries createTask(String name, String timeline, String listId) throws IOException {
Map<String, String> params =
ImmutableMap.of(
"method",
RememberTheMilkMethods.TASKS_ADD.getMethodName(),
"timeline",
timeline,
"name",
name,
"list_id",
listId);
TaskUpdateResponse taskUpdateResponse = makeRequest(params, TaskUpdateResponse.class);
return taskUpdateResponse.list.taskseries.get(0);
}
public void completeTask(String timeline, String listId, int seriesId, int taskId)
throws IOException {
// NB: The RTM API does not support setting an arbitrary completion time, so this method can
// only mark a task as having been completed.
Map<String, String> params =
ImmutableMap.of(
"method",
RememberTheMilkMethods.TASKS_COMPLETE.getMethodName(),
"timeline",
timeline,
"list_id",
listId,
"taskseries_id",
String.valueOf(seriesId),
"task_id",
String.valueOf(taskId)
);
makeRequest(params, TaskUpdateResponse.class);
}
public GetListResponse getList(String listId) throws IOException {
Map<String, String> params =
ImmutableMap.of(
"method", RememberTheMilkMethods.TASKS_GET_LIST.getMethodName(), "list_id", listId);
return makeRequest(params, GetListResponse.class);
}
public GetListsResponse getLists() throws IOException {
return makeRequest(
ImmutableMap.of("method", RememberTheMilkMethods.LISTS_GET_LIST.getMethodName()),
GetListsResponse.class);
}
private <T extends RememberTheMilkResponse> T makeRequest(
Map<String, String> parameters, Class<T> dataClass) throws IOException {
URL signedUrl = signatureGenerator.getSignature(BASE_URL, parameters);
HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory();
HttpRequest getRequest = requestFactory.buildGetRequest(new GenericUrl(signedUrl));
HttpResponse response = getRequest.execute();
int statusCode = response.getStatusCode();
if (statusCode != 200) {
throw new IOException(
"Bad status code: " + statusCode + " error: " + response.getStatusMessage());
}
T parsedResponse = xmlMapper.readValue(response.getContent(), dataClass);
if (parsedResponse.error != null) {
throw new IOException(
"Error making call to " + signedUrl + " error: " + parsedResponse.error);
}
return parsedResponse;
}
private enum RememberTheMilkMethods {
LISTS_GET_LIST("rtm.lists.getList"),
LISTS_ADD("rtm.lists.add"),
TASKS_ADD("rtm.tasks.add"),
TASKS_COMPLETE("rtm.tasks.complete"),
TASKS_GET_LIST("rtm.tasks.getList"),
TIMELINES_CREATE("rtm.timelines.create");
private final String methodName;
RememberTheMilkMethods(String methodName) {
this.methodName = methodName;
}
String getMethodName() {
return methodName;
}
}
}