Skip to content

Commit d2ecac2

Browse files
committed
Updated Task related methods to use better error handling.
1 parent 3cb03da commit d2ecac2

File tree

1 file changed

+45
-92
lines changed

1 file changed

+45
-92
lines changed

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

Lines changed: 45 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -72,116 +72,69 @@ public void deleteProject(long id) throws TodoistException {
7272
if(response.getStatus() != HTTP_OK_NO_CONTENT) throw new TodoistException(response.getStatus());
7373
}
7474

75-
public List<Task> getActiveTasks() {
76-
try {
77-
HttpResponse<String> response = Unirest.get(URL_BASE + "/tasks")
78-
.asString();
79-
if(response.getStatus() != HTTP_OK) {
80-
throw new Exception("HTTP STATUS " + response.getStatus());
81-
}
82-
83-
return JsonAdapters.extractTaskList(response.getBody());
84-
} catch (Exception e) {
85-
e.printStackTrace();
86-
return null;
87-
}
75+
public List<Task> getActiveTasks() throws TodoistException {
76+
HttpResponse<String> response = Unirest.get(URL_BASE + "/tasks")
77+
.asString();
78+
if(response.getStatus() != HTTP_OK) throw new TodoistException(response.getStatus());
79+
return extract(JsonAdapters::extractTaskList, response);
8880
}
8981

90-
public List<Task> getActiveTasks(Long projectId, Long labelId, String filter, String lang) {
91-
try {
92-
Map<String, Object> params = new HashMap<>();
93-
if(projectId != null) params.put("project_id", projectId);
94-
if(labelId != null) params.put("label_id", labelId);
95-
if(filter != null) params.put("filter", filter);
96-
if(lang != null) params.put("lang", lang);
97-
98-
HttpResponse<String> response = Unirest.get(URL_BASE + "/tasks")
99-
.queryString(params)
100-
.asString();
101-
if(response.getStatus() != HTTP_OK) {
102-
throw new Exception("HTTP STATUS " + response.getStatus());
103-
}
104-
return JsonAdapters.extractTaskList(response.getBody());
105-
} catch (Exception e) {
106-
e.printStackTrace();
107-
return null;
108-
}
82+
public List<Task> getActiveTasks(Long projectId, Long labelId, String filter, String lang) throws TodoistException {
83+
Map<String, Object> params = new HashMap<>();
84+
if(projectId != null) params.put("project_id", projectId);
85+
if(labelId != null) params.put("label_id", labelId);
86+
if(filter != null) params.put("filter", filter);
87+
if(lang != null) params.put("lang", lang);
88+
89+
HttpResponse<String> response = Unirest.get(URL_BASE + "/tasks")
90+
.queryString(params)
91+
.asString();
92+
if(response.getStatus() != HTTP_OK) throw new TodoistException(response.getStatus());
93+
return extract(JsonAdapters::extractTaskList, response);
10994
}
11095

111-
private Task createNewTask(TaskRequest taskRequest) {
112-
try {
113-
HttpResponse<String> response = Unirest.post(URL_BASE + "/tasks")
114-
.header("Content-Type", "application/json")
115-
.body(JsonAdapters.writeTaskRequest(taskRequest))
116-
.asString();
117-
if(response.getStatus() != HTTP_OK) {
118-
throw new Exception("HTTP STATUS " + response.getStatus());
119-
}
120-
return JsonAdapters.extractTask(response.getBody());
121-
} catch (Exception e) {
122-
e.printStackTrace();
123-
return null;
124-
}
96+
private Task createNewTask(TaskRequest taskRequest) throws TodoistException {
97+
HttpResponse<String> response = Unirest.post(URL_BASE + "/tasks")
98+
.header("Content-Type", "application/json")
99+
.body(JsonAdapters.writeTaskRequest(taskRequest))
100+
.asString();
101+
if(response.getStatus() != HTTP_OK) throw new TodoistException(response.getStatus());
102+
return extract(JsonAdapters::extractTask, response);
125103
}
126104

127-
public Task createNewTask(String content) {
105+
public Task createNewTask(String content) throws TodoistException {
128106
return createNewTask(new TaskRequest(content, null, null, null, null, null, null, null, null));
129107
}
130108

131-
public Task createNewTask(String content, Long projectId, Integer order, List<Long> labelIds, Integer priority, String dueString, String dueDate, String dueDateTime, String dueLang) {
109+
public Task createNewTask(String content, Long projectId, Integer order, List<Long> labelIds, Integer priority, String dueString, String dueDate, String dueDateTime, String dueLang) throws TodoistException {
132110
return createNewTask(new TaskRequest(content, projectId, order, labelIds, priority, dueString, dueDate, dueDateTime, dueLang));
133111
}
134112

135-
public Task getActiveTask(long id) {
136-
try {
137-
HttpResponse<String> response = Unirest.get(URL_BASE + "/tasks/" + id)
138-
.asString();
139-
if(response.getStatus() != HTTP_OK) {
140-
throw new Exception("HTTP STATUS " + response.getStatus());
141-
}
142-
return JsonAdapters.extractTask(response.getBody());
143-
} catch (Exception e) {
144-
e.printStackTrace();
145-
return null;
146-
}
113+
public Task getActiveTask(long id) throws TodoistException {
114+
HttpResponse<String> response = Unirest.get(URL_BASE + "/tasks/" + id)
115+
.asString();
116+
if(response.getStatus() != HTTP_OK) throw new TodoistException(response.getStatus());
117+
return extract(JsonAdapters::extractTask, response);
147118
}
148119

149-
public void updateTask(long id, String content, Long projectId, List<Long> labelIds, Integer priority, String dueString, String dueDate, String dueDateTime, String dueLang) {
150-
try {
151-
HttpResponse<String> response = Unirest.post(URL_BASE + "/tasks/" + id)
152-
.header("Content-Type", "application/json")
153-
.body(JsonAdapters.writeTaskRequest(new TaskRequest(content, projectId, null, labelIds, priority, dueString, dueDate, dueDateTime, dueLang)))
154-
.asString();
155-
if(response.getStatus() != HTTP_OK_NO_CONTENT) {
156-
throw new Exception("HTTP STATUS " + response.getStatus());
157-
}
158-
} catch (Exception e) {
159-
e.printStackTrace();
160-
}
120+
public void updateTask(long id, String content, Long projectId, List<Long> labelIds, Integer priority, String dueString, String dueDate, String dueDateTime, String dueLang) throws TodoistException {
121+
HttpResponse<String> response = Unirest.post(URL_BASE + "/tasks/" + id)
122+
.header("Content-Type", "application/json")
123+
.body(JsonAdapters.writeTaskRequest(new TaskRequest(content, projectId, null, labelIds, priority, dueString, dueDate, dueDateTime, dueLang)))
124+
.asString();
125+
if(response.getStatus() != HTTP_OK_NO_CONTENT) throw new TodoistException(response.getStatus());
161126
}
162127

163-
public void closeTask(long id) {
164-
try {
165-
HttpResponse<String> response = Unirest.post(URL_BASE + "/tasks/" + id + "/close")
166-
.asString();
167-
if(response.getStatus() != HTTP_OK_NO_CONTENT) {
168-
throw new Exception("HTTP STATUS " + response.getStatus());
169-
}
170-
} catch (Exception e) {
171-
e.printStackTrace();
172-
}
128+
public void closeTask(long id) throws TodoistException {
129+
HttpResponse<String> response = Unirest.post(URL_BASE + "/tasks/" + id + "/close")
130+
.asString();
131+
if(response.getStatus() != HTTP_OK_NO_CONTENT) throw new TodoistException(response.getStatus());
173132
}
174133

175-
public void deleteTask(long id) {
176-
try {
177-
HttpResponse<String> response = Unirest.delete(URL_BASE + "/tasks/" + id)
178-
.asString();
179-
if(response.getStatus() != HTTP_OK_NO_CONTENT) {
180-
throw new Exception("HTTP STATUS " + response.getStatus());
181-
}
182-
} catch (Exception e) {
183-
e.printStackTrace();
184-
}
134+
public void deleteTask(long id) throws TodoistException {
135+
HttpResponse<String> response = Unirest.delete(URL_BASE + "/tasks/" + id)
136+
.asString();
137+
if(response.getStatus() != HTTP_OK_NO_CONTENT) throw new TodoistException(response.getStatus());
185138
}
186139

187140
public List<Label> getAllLabels() {

0 commit comments

Comments
 (0)