Skip to content

Commit 784a3e7

Browse files
committed
Updated Comment related methods to use better error handling.
1 parent e553756 commit 784a3e7

File tree

1 file changed

+38
-78
lines changed

1 file changed

+38
-78
lines changed

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

Lines changed: 38 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -178,106 +178,66 @@ public void deleteLabel(long id) throws TodoistException {
178178
if(response.getStatus() != HTTP_OK_NO_CONTENT) throw new TodoistException(response.getStatus());
179179
}
180180

181-
public List<Comment> getAllCommentsForProject(long id) {
182-
try {
183-
HttpResponse<String> response = Unirest.get(URL_BASE + "/comments")
184-
.queryString("project_id", id)
185-
.asString();
186-
if(response.getStatus() != HTTP_OK) {
187-
throw new Exception("HTTP STATUS " + response.getStatus());
188-
}
189-
return JsonAdapters.extractCommentList(response.getBody());
190-
} catch (Exception e) {
191-
e.printStackTrace();
192-
return null;
193-
}
181+
public List<Comment> getAllCommentsForProject(long id) throws TodoistException {
182+
HttpResponse<String> response = Unirest.get(URL_BASE + "/comments")
183+
.queryString("project_id", id)
184+
.asString();
185+
if(response.getStatus() != HTTP_OK) throw new TodoistException(response.getStatus());
186+
return extract(JsonAdapters::extractCommentList, response);
194187
}
195188

196-
public List<Comment> getAllCommentsForTask(long id) {
197-
try {
198-
HttpResponse<String> response = Unirest.get(URL_BASE + "/comments")
199-
.queryString("task_id", id)
200-
.asString();
201-
if(response.getStatus() != HTTP_OK) {
202-
throw new Exception("HTTP STATUS " + response.getStatus());
203-
}
204-
return JsonAdapters.extractCommentList(response.getBody());
205-
} catch (Exception e) {
206-
e.printStackTrace();
207-
return null;
208-
}
189+
public List<Comment> getAllCommentsForTask(long id) throws TodoistException {
190+
HttpResponse<String> response = Unirest.get(URL_BASE + "/comments")
191+
.queryString("task_id", id)
192+
.asString();
193+
if(response.getStatus() != HTTP_OK) throw new TodoistException(response.getStatus());
194+
return extract(JsonAdapters::extractCommentList, response);
209195
}
210196

211-
private Comment createNewComment(Long taskId, Long projectId, String content, AttachmentRequest attachmentRequest) {
212-
try {
213-
HttpResponse<String> response = Unirest.post(URL_BASE + "/comments")
214-
.header("Content-Type", "application/json")
215-
.body(JsonAdapters.writeCommentRequest(new CommentRequest(taskId, projectId, content, attachmentRequest)))
216-
.asString();
217-
if(response.getStatus() != HTTP_OK) {
218-
throw new Exception("HTTP STATUS " + response.getStatus());
219-
}
220-
return JsonAdapters.extractComment(response.getBody());
221-
} catch (Exception e) {
222-
e.printStackTrace();
223-
return null;
224-
}
197+
private Comment createNewComment(Long taskId, Long projectId, String content, AttachmentRequest attachmentRequest) throws TodoistException {
198+
HttpResponse<String> response = Unirest.post(URL_BASE + "/comments")
199+
.header("Content-Type", "application/json")
200+
.body(JsonAdapters.writeCommentRequest(new CommentRequest(taskId, projectId, content, attachmentRequest)))
201+
.asString();
202+
if(response.getStatus() != HTTP_OK) throw new TodoistException(response.getStatus());
203+
return extract(JsonAdapters::extractComment, response);
225204
}
226205

227-
public Comment createNewCommentForTask(long id, String content) {
206+
public Comment createNewCommentForTask(long id, String content) throws TodoistException {
228207
return createNewComment(id, null, content, null);
229208
}
230209

231-
public Comment createNewCommentForTask(long id, String content, AttachmentRequest attachmentRequest) {
210+
public Comment createNewCommentForTask(long id, String content, AttachmentRequest attachmentRequest) throws TodoistException {
232211
return createNewComment(id, null, content, attachmentRequest);
233212
}
234213

235-
public Comment createNewCommentForProject(long id, String content) {
214+
public Comment createNewCommentForProject(long id, String content) throws TodoistException {
236215
return createNewComment(null, id, content, null);
237216
}
238217

239-
public Comment createNewCommentForProject(long id, String content, AttachmentRequest attachmentRequest) {
218+
public Comment createNewCommentForProject(long id, String content, AttachmentRequest attachmentRequest) throws TodoistException {
240219
return createNewComment(null, id, content, attachmentRequest);
241220
}
242221

243-
public Comment getComment(long id) {
244-
try {
245-
HttpResponse<String> response = Unirest.get(URL_BASE + "/comments/" + id)
246-
.asString();
247-
if(response.getStatus() != HTTP_OK) {
248-
throw new Exception("HTTP STATUS " + response.getStatus());
249-
}
250-
return JsonAdapters.extractComment(response.getBody());
251-
} catch (Exception e) {
252-
e.printStackTrace();
253-
return null;
254-
}
222+
public Comment getComment(long id) throws TodoistException {
223+
HttpResponse<String> response = Unirest.get(URL_BASE + "/comments/" + id)
224+
.asString();
225+
if(response.getStatus() != HTTP_OK) throw new TodoistException(response.getStatus());
226+
return extract(JsonAdapters::extractComment, response);
255227
}
256228

257-
public void updateComment(long id, String content) {
258-
try {
259-
HttpResponse<String> response = Unirest.post(URL_BASE + "/comments/" + id)
260-
.header("Content-Type", "application/json")
261-
.body(JsonAdapters.writeCommentRequest(new CommentRequest(null, null, content, null)))
262-
.asString();
263-
if(response.getStatus() != HTTP_OK_NO_CONTENT) {
264-
throw new Exception("HTTP STATUS " + response.getStatus());
265-
}
266-
} catch (Exception e) {
267-
e.printStackTrace();
268-
}
229+
public void updateComment(long id, String content) throws TodoistException {
230+
HttpResponse<String> response = Unirest.post(URL_BASE + "/comments/" + id)
231+
.header("Content-Type", "application/json")
232+
.body(JsonAdapters.writeCommentRequest(new CommentRequest(null, null, content, null)))
233+
.asString();
234+
if(response.getStatus() != HTTP_OK_NO_CONTENT) throw new TodoistException(response.getStatus());
269235
}
270236

271-
public void deleteComment(long id) {
272-
try {
273-
HttpResponse<String> response = Unirest.delete(URL_BASE + "/comments/" + id)
274-
.asString();
275-
if(response.getStatus() != HTTP_OK_NO_CONTENT) {
276-
throw new Exception("HTTP STATUS " + response.getStatus());
277-
}
278-
} catch (Exception e) {
279-
e.printStackTrace();
280-
}
237+
public void deleteComment(long id) throws TodoistException {
238+
HttpResponse<String> response = Unirest.delete(URL_BASE + "/comments/" + id)
239+
.asString();
240+
if(response.getStatus() != HTTP_OK_NO_CONTENT) throw new TodoistException(response.getStatus());
281241
}
282242

283243
public List<Activity> getActivityForProject(long id) {

0 commit comments

Comments
 (0)