Skip to content

Commit e553756

Browse files
committed
Updated Label related methods to use better error handling.
1 parent d2ecac2 commit e553756

File tree

1 file changed

+28
-61
lines changed

1 file changed

+28
-61
lines changed

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

Lines changed: 28 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -137,78 +137,45 @@ public void deleteTask(long id) throws TodoistException {
137137
if(response.getStatus() != HTTP_OK_NO_CONTENT) throw new TodoistException(response.getStatus());
138138
}
139139

140-
public List<Label> getAllLabels() {
141-
try {
142-
HttpResponse<String> response = Unirest.get(URL_BASE + "/labels")
143-
.asString();
144-
if(response.getStatus() != HTTP_OK) {
145-
throw new Exception("HTTP STATUS " + response.getStatus());
146-
}
147-
return JsonAdapters.extractLabelList(response.getBody());
148-
} catch (Exception e) {
149-
e.printStackTrace();
150-
return null;
151-
}
140+
public List<Label> getAllLabels() throws TodoistException {
141+
HttpResponse<String> response = Unirest.get(URL_BASE + "/labels")
142+
.asString();
143+
if(response.getStatus() != HTTP_OK) throw new TodoistException(response.getStatus());
144+
return extract(JsonAdapters::extractLabelList, response);
152145
}
153146

154-
public Label createNewLabel(String name) {
147+
public Label createNewLabel(String name) throws TodoistException {
155148
return createNewLabel(name, null);
156149
}
157150

158-
public Label createNewLabel(String name, Integer order) {
159-
try {
160-
HttpResponse<String> response = Unirest.post(URL_BASE + "/labels")
161-
.header("Content-Type", "application/json")
162-
.body(JsonAdapters.writeLabelRequest(new LabelRequest(name, order)))
163-
.asString();
164-
if(response.getStatus() != HTTP_OK) {
165-
throw new Exception("HTTP STATUS " + response.getStatus());
166-
}
167-
return JsonAdapters.extractLabel(response.getBody());
168-
} catch (Exception e) {
169-
e.printStackTrace();
170-
return null;
171-
}
151+
public Label createNewLabel(String name, Integer order) throws TodoistException {
152+
HttpResponse<String> response = Unirest.post(URL_BASE + "/labels")
153+
.header("Content-Type", "application/json")
154+
.body(JsonAdapters.writeLabelRequest(new LabelRequest(name, order)))
155+
.asString();
156+
if(response.getStatus() != HTTP_OK) throw new TodoistException(response.getStatus());
157+
return extract(JsonAdapters::extractLabel, response);
172158
}
173159

174-
public Label getLabel(long id) {
175-
try {
176-
HttpResponse<String> response = Unirest.get(URL_BASE + "/labels/" + id)
177-
.asString();
178-
if(response.getStatus() != HTTP_OK) {
179-
throw new Exception("HTTP STATUS " + response.getStatus());
180-
}
181-
return JsonAdapters.extractLabel(response.getBody());
182-
} catch (Exception e) {
183-
e.printStackTrace();
184-
return null;
185-
}
160+
public Label getLabel(long id) throws TodoistException {
161+
HttpResponse<String> response = Unirest.get(URL_BASE + "/labels/" + id)
162+
.asString();
163+
if(response.getStatus() != HTTP_OK) throw new TodoistException(response.getStatus());
164+
return extract(JsonAdapters::extractLabel, response);
186165
}
187166

188-
public void updateLabel(long id, String name, Integer order) {
189-
try {
190-
HttpResponse<String> response = Unirest.post(URL_BASE + "/labels/" + id)
191-
.header("Content-Type", "application/json")
192-
.body(JsonAdapters.writeLabelRequest(new LabelRequest(name, order)))
193-
.asString();
194-
if(response.getStatus() != HTTP_OK_NO_CONTENT) {
195-
throw new Exception("HTTP STATUS " + response.getStatus());
196-
}
197-
} catch (Exception e) {
198-
e.printStackTrace();
199-
}
167+
public void updateLabel(long id, String name, Integer order) throws TodoistException {
168+
HttpResponse<String> response = Unirest.post(URL_BASE + "/labels/" + id)
169+
.header("Content-Type", "application/json")
170+
.body(JsonAdapters.writeLabelRequest(new LabelRequest(name, order)))
171+
.asString();
172+
if(response.getStatus() != HTTP_OK_NO_CONTENT) throw new TodoistException(response.getStatus());
200173
}
201174

202-
public void deleteLabel(long id) {
203-
try {
204-
HttpResponse<String> response = Unirest.delete(URL_BASE + "/labels/" + id)
205-
.asString();
206-
if(response.getStatus() != HTTP_OK_NO_CONTENT) {
207-
throw new Exception("HTTP STATUS " + response.getStatus());
208-
}
209-
} catch (Exception e) {
210-
e.printStackTrace();
211-
}
175+
public void deleteLabel(long id) throws TodoistException {
176+
HttpResponse<String> response = Unirest.delete(URL_BASE + "/labels/" + id)
177+
.asString();
178+
if(response.getStatus() != HTTP_OK_NO_CONTENT) throw new TodoistException(response.getStatus());
212179
}
213180

214181
public List<Comment> getAllCommentsForProject(long id) {

0 commit comments

Comments
 (0)