Skip to content

Commit 3290f91

Browse files
committed
Updated Activity related methods to use better error handling.
1 parent 784a3e7 commit 3290f91

File tree

1 file changed

+52
-66
lines changed

1 file changed

+52
-66
lines changed

src/main/java/com/aaroncoplan/todoist/Todoist.java

Lines changed: 52 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -240,79 +240,65 @@ public void deleteComment(long id) throws TodoistException {
240240
if(response.getStatus() != HTTP_OK_NO_CONTENT) throw new TodoistException(response.getStatus());
241241
}
242242

243-
public List<Activity> getActivityForProject(long id) {
243+
public List<Activity> getActivityForProject(long id) throws TodoistException {
244244
return getActivityForProject(id, ActivityType.ALL);
245245
}
246246

247-
public List<Activity> getActivityForProject(long id, ActivityType... types) {
248-
try {
249-
int limit = 30;
250-
int offset = 0;
251-
int count;
252-
253-
List<String> activityTypes = Arrays.stream(types)
254-
.flatMap(ActivityType::getStream)
255-
.collect(Collectors.toList());
256-
257-
List<Activity> activityList = new ArrayList<>();
258-
do {
259-
HttpResponse<String> response = Unirest.post("https://todoist.com/API/v8/activity/get")
260-
.header("Content-Type", "application/json")
261-
.body(JsonAdapters.writeActivityRequest(new ActivityRequest(limit, offset, activityTypes, null, null, true, id, true)))
262-
.asString();
263-
if (response.getStatus() != HTTP_OK) {
264-
throw new Exception("HTTP STATUS " + response.getStatus());
265-
}
266-
267-
ActivityResponse activityResponse = JsonAdapters.extractActivityResponse(response.getBody());
268-
activityList.addAll(activityResponse.events);
269-
270-
count = activityResponse.count;
271-
offset += limit;
272-
} while(offset < count);
273-
274-
return activityList;
275-
} catch (Exception e) {
276-
e.printStackTrace();
277-
return null;
278-
}
247+
public List<Activity> getActivityForProject(long id, ActivityType... types) throws TodoistException {
248+
int limit = 30;
249+
int offset = 0;
250+
int count;
251+
252+
List<String> activityTypes = Arrays.stream(types)
253+
.flatMap(ActivityType::getStream)
254+
.collect(Collectors.toList());
255+
256+
List<Activity> activityList = new ArrayList<>();
257+
do {
258+
HttpResponse<String> response = Unirest.post("https://todoist.com/API/v8/activity/get")
259+
.header("Content-Type", "application/json")
260+
.body(JsonAdapters.writeActivityRequest(new ActivityRequest(limit, offset, activityTypes, null, null, true, id, true)))
261+
.asString();
262+
if (response.getStatus() != HTTP_OK) throw new TodoistException(response.getStatus());
263+
264+
ActivityResponse activityResponse = extract(JsonAdapters::extractActivityResponse, response);
265+
activityList.addAll(activityResponse.events);
266+
267+
count = activityResponse.count;
268+
offset += limit;
269+
} while(offset < count);
270+
271+
return activityList;
279272
}
280273

281-
public List<Activity> getActivityForTask(long id) {
274+
public List<Activity> getActivityForTask(long id) throws TodoistException {
282275
return getActivityForTask(id, ActivityType.ALL);
283276
}
284277

285-
public List<Activity> getActivityForTask(long id, ActivityType... types) {
286-
try {
287-
int limit = 30;
288-
int offset = 0;
289-
int count;
290-
291-
List<String> activityTypes = Arrays.stream(types)
292-
.flatMap(ActivityType::getStream)
293-
.collect(Collectors.toList());
294-
295-
List<Activity> activityList = new ArrayList<>();
296-
do {
297-
HttpResponse<String> response = Unirest.post("https://todoist.com/API/v8/activity/get")
298-
.header("Content-Type", "application/json")
299-
.body(JsonAdapters.writeActivityRequest(new ActivityRequest(limit, offset, activityTypes, id, true, true, null, null)))
300-
.asString();
301-
if (response.getStatus() != HTTP_OK) {
302-
throw new Exception("HTTP STATUS " + response.getStatus());
303-
}
304-
305-
ActivityResponse activityResponse = JsonAdapters.extractActivityResponse(response.getBody());
306-
activityList.addAll(activityResponse.events);
307-
308-
count = activityResponse.count;
309-
offset += limit;
310-
} while(offset < count);
311-
312-
return activityList;
313-
} catch (Exception e) {
314-
e.printStackTrace();
315-
return null;
316-
}
278+
public List<Activity> getActivityForTask(long id, ActivityType... types) throws TodoistException {
279+
int limit = 30;
280+
int offset = 0;
281+
int count;
282+
283+
List<String> activityTypes = Arrays.stream(types)
284+
.flatMap(ActivityType::getStream)
285+
.collect(Collectors.toList());
286+
287+
List<Activity> activityList = new ArrayList<>();
288+
do {
289+
HttpResponse<String> response = Unirest.post("https://todoist.com/API/v8/activity/get")
290+
.header("Content-Type", "application/json")
291+
.body(JsonAdapters.writeActivityRequest(new ActivityRequest(limit, offset, activityTypes, id, true, true, null, null)))
292+
.asString();
293+
if (response.getStatus() != HTTP_OK) throw new TodoistException(response.getStatus());
294+
295+
ActivityResponse activityResponse = extract(JsonAdapters::extractActivityResponse, response);
296+
activityList.addAll(activityResponse.events);
297+
298+
count = activityResponse.count;
299+
offset += limit;
300+
} while(offset < count);
301+
302+
return activityList;
317303
}
318304
}

0 commit comments

Comments
 (0)