diff --git a/build.gradle b/build.gradle index 8664f41..1380be8 100644 --- a/build.gradle +++ b/build.gradle @@ -10,7 +10,7 @@ repositories { sourceCompatibility = JavaVersion.VERSION_1_8 dependencies { - implementation 'com.mashape.unirest:unirest-java:1.4.9' + implementation 'com.konghq:unirest-java:2.3.08' implementation 'com.squareup.moshi:moshi:1.8.0' testImplementation 'junit:junit:4.12' } diff --git a/src/main/java/com/aaroncoplan/todoist/Todoist.java b/src/main/java/com/aaroncoplan/todoist/Todoist.java index ea9c29c..675c903 100644 --- a/src/main/java/com/aaroncoplan/todoist/Todoist.java +++ b/src/main/java/com/aaroncoplan/todoist/Todoist.java @@ -2,9 +2,10 @@ import com.aaroncoplan.todoist.helpers.*; import com.aaroncoplan.todoist.model.*; -import com.mashape.unirest.http.HttpResponse; -import com.mashape.unirest.http.Unirest; +import kong.unirest.HttpResponse; +import kong.unirest.Unirest; +import java.io.IOException; import java.util.*; import java.util.stream.Collectors; @@ -16,441 +17,288 @@ public class Todoist { private final String URL_BASE = "https://beta.todoist.com/API/v8"; public Todoist(String token) { - Unirest.setTimeouts(20_000, 20_000); - Unirest.setDefaultHeader("Authorization", String.format("Bearer %s", token)); + Unirest.config() + .connectTimeout(20_000) + .socketTimeout(20_000) + .addDefaultHeader("Authorization", String.format("Bearer %s", token)); } - public List getAllProjects() { + public T extract(CheckedFunction extractionFunction, HttpResponse httpResponse) throws TodoistException { try { - HttpResponse response = Unirest.get(URL_BASE + "/projects") - .asString(); - if(response.getStatus() != HTTP_OK) { - throw new Exception("HTTP STATUS " + response.getStatus()); - } - return JsonAdapters.extractProjectList(response.getBody()); - } catch (Exception e) { - e.printStackTrace(); - return null; + return extractionFunction.apply(httpResponse.getBody()); + } catch (IOException e) { + throw new TodoistException("Error mapping JSON to Object"); } } - public Project getProject(long id) { - try { - HttpResponse response = Unirest.get(URL_BASE + "/projects/" + id) - .asString(); - if(response.getStatus() != HTTP_OK) { - throw new Exception("HTTP STATUS " + response.getStatus()); - } - return JsonAdapters.extractProject(response.getBody()); - } catch (Exception e) { - e.printStackTrace(); - return null; - } + public interface CheckedFunction { + R apply(P t) throws IOException; } - public Project createNewProject(String name) { - try { - HttpResponse response = Unirest.post(URL_BASE + "/projects") - .header("Content-Type", "application/json") - .body(JsonAdapters.writeProjectRequest(new ProjectRequest(name))) - .asString(); - if(response.getStatus() != HTTP_OK) { - throw new Exception("HTTP STATUS " + response.getStatus()); - } - return JsonAdapters.extractProject(response.getBody()); - } catch (Exception e) { - e.printStackTrace(); - return null; - } + public List getAllProjects() throws TodoistException { + HttpResponse response = Unirest.get(URL_BASE + "/projects") + .asString(); + if (response.getStatus() != HTTP_OK) throw new TodoistException(response.getStatus()); + return extract(JsonAdapters::extractProjectList, response); } - public void updateProject(long id, String name) { - try { - HttpResponse response = Unirest.post(URL_BASE + "/projects/" + id) - .header("Content-Type", "application/json") - .body(JsonAdapters.writeProjectRequest(new ProjectRequest(name))) - .asString(); - if(response.getStatus() != HTTP_OK_NO_CONTENT) { - throw new Exception("HTTP STATUS " + response.getStatus()); - } - } catch (Exception e) { - e.printStackTrace(); - } + public Project getProject(long id) throws TodoistException { + HttpResponse response = Unirest.get(URL_BASE + "/projects/" + id) + .asString(); + if(response.getStatus() != HTTP_OK) throw new TodoistException(response.getStatus()); + return extract(JsonAdapters::extractProject, response); } - public void deleteProject(long id) { - try { - HttpResponse response = Unirest.delete(URL_BASE + "/projects/" + id) - .asString(); - if(response.getStatus() != HTTP_OK_NO_CONTENT) { - throw new Exception("HTTP STATUS " + response.getStatus()); - } - } catch (Exception e) { - e.printStackTrace(); - } + public Project createNewProject(String name) throws TodoistException { + HttpResponse response = Unirest.post(URL_BASE + "/projects") + .header("Content-Type", "application/json") + .body(JsonAdapters.writeProjectRequest(new ProjectRequest(name))) + .asString(); + if(response.getStatus() != HTTP_OK) throw new TodoistException(response.getStatus()); + return extract(JsonAdapters::extractProject, response); } - public List getActiveTasks() { - try { - HttpResponse response = Unirest.get(URL_BASE + "/tasks") - .asString(); - if(response.getStatus() != HTTP_OK) { - throw new Exception("HTTP STATUS " + response.getStatus()); - } - - return JsonAdapters.extractTaskList(response.getBody()); - } catch (Exception e) { - e.printStackTrace(); - return null; - } + public void updateProject(long id, String name) throws TodoistException { + HttpResponse response = Unirest.post(URL_BASE + "/projects/" + id) + .header("Content-Type", "application/json") + .body(JsonAdapters.writeProjectRequest(new ProjectRequest(name))) + .asString(); + if(response.getStatus() != HTTP_OK_NO_CONTENT) throw new TodoistException(response.getStatus()); } - public List getActiveTasks(Long projectId, Long labelId, String filter, String lang) { - try { - Map params = new HashMap<>(); - if(projectId != null) params.put("project_id", projectId); - if(labelId != null) params.put("label_id", labelId); - if(filter != null) params.put("filter", filter); - if(lang != null) params.put("lang", lang); - - HttpResponse response = Unirest.get(URL_BASE + "/tasks") - .queryString(params) - .asString(); - if(response.getStatus() != HTTP_OK) { - throw new Exception("HTTP STATUS " + response.getStatus()); - } - return JsonAdapters.extractTaskList(response.getBody()); - } catch (Exception e) { - e.printStackTrace(); - return null; - } + public void deleteProject(long id) throws TodoistException { + HttpResponse response = Unirest.delete(URL_BASE + "/projects/" + id) + .asString(); + if(response.getStatus() != HTTP_OK_NO_CONTENT) throw new TodoistException(response.getStatus()); } - private Task createNewTask(TaskRequest taskRequest) { - try { - HttpResponse response = Unirest.post(URL_BASE + "/tasks") - .header("Content-Type", "application/json") - .body(JsonAdapters.writeTaskRequest(taskRequest)) - .asString(); - if(response.getStatus() != HTTP_OK) { - throw new Exception("HTTP STATUS " + response.getStatus()); - } - return JsonAdapters.extractTask(response.getBody()); - } catch (Exception e) { - e.printStackTrace(); - return null; - } + public List getActiveTasks() throws TodoistException { + HttpResponse response = Unirest.get(URL_BASE + "/tasks") + .asString(); + if(response.getStatus() != HTTP_OK) throw new TodoistException(response.getStatus()); + return extract(JsonAdapters::extractTaskList, response); } - public Task createNewTask(String content) { + public List getActiveTasks(Long projectId, Long labelId, String filter, String lang) throws TodoistException { + Map params = new HashMap<>(); + if(projectId != null) params.put("project_id", projectId); + if(labelId != null) params.put("label_id", labelId); + if(filter != null) params.put("filter", filter); + if(lang != null) params.put("lang", lang); + + HttpResponse response = Unirest.get(URL_BASE + "/tasks") + .queryString(params) + .asString(); + if(response.getStatus() != HTTP_OK) throw new TodoistException(response.getStatus()); + return extract(JsonAdapters::extractTaskList, response); + } + + private Task createNewTask(TaskRequest taskRequest) throws TodoistException { + HttpResponse response = Unirest.post(URL_BASE + "/tasks") + .header("Content-Type", "application/json") + .body(JsonAdapters.writeTaskRequest(taskRequest)) + .asString(); + if(response.getStatus() != HTTP_OK) throw new TodoistException(response.getStatus()); + return extract(JsonAdapters::extractTask, response); + } + + public Task createNewTask(String content) throws TodoistException { return createNewTask(new TaskRequest(content, null, null, null, null, null, null, null, null)); } - public Task createNewTask(String content, Long projectId, Integer order, List labelIds, Integer priority, String dueString, String dueDate, String dueDateTime, String dueLang) { + public Task createNewTask(String content, Long projectId, Integer order, List labelIds, Integer priority, String dueString, String dueDate, String dueDateTime, String dueLang) throws TodoistException { return createNewTask(new TaskRequest(content, projectId, order, labelIds, priority, dueString, dueDate, dueDateTime, dueLang)); } - public Task getActiveTask(long id) { - try { - HttpResponse response = Unirest.get(URL_BASE + "/tasks/" + id) - .asString(); - if(response.getStatus() != HTTP_OK) { - throw new Exception("HTTP STATUS " + response.getStatus()); - } - return JsonAdapters.extractTask(response.getBody()); - } catch (Exception e) { - e.printStackTrace(); - return null; - } + public Task getActiveTask(long id) throws TodoistException { + HttpResponse response = Unirest.get(URL_BASE + "/tasks/" + id) + .asString(); + if(response.getStatus() != HTTP_OK) throw new TodoistException(response.getStatus()); + return extract(JsonAdapters::extractTask, response); } - public void updateTask(long id, String content, Long projectId, List labelIds, Integer priority, String dueString, String dueDate, String dueDateTime, String dueLang) { - try { - HttpResponse response = Unirest.post(URL_BASE + "/tasks/" + id) - .header("Content-Type", "application/json") - .body(JsonAdapters.writeTaskRequest(new TaskRequest(content, projectId, null, labelIds, priority, dueString, dueDate, dueDateTime, dueLang))) - .asString(); - if(response.getStatus() != HTTP_OK_NO_CONTENT) { - throw new Exception("HTTP STATUS " + response.getStatus()); - } - } catch (Exception e) { - e.printStackTrace(); - } + public void updateTask(long id, String content, Long projectId, List labelIds, Integer priority, String dueString, String dueDate, String dueDateTime, String dueLang) throws TodoistException { + HttpResponse response = Unirest.post(URL_BASE + "/tasks/" + id) + .header("Content-Type", "application/json") + .body(JsonAdapters.writeTaskRequest(new TaskRequest(content, projectId, null, labelIds, priority, dueString, dueDate, dueDateTime, dueLang))) + .asString(); + if(response.getStatus() != HTTP_OK_NO_CONTENT) throw new TodoistException(response.getStatus()); } - public void closeTask(long id) { - try { - HttpResponse response = Unirest.post(URL_BASE + "/tasks/" + id + "/close") - .asString(); - if(response.getStatus() != HTTP_OK_NO_CONTENT) { - throw new Exception("HTTP STATUS " + response.getStatus()); - } - } catch (Exception e) { - e.printStackTrace(); - } + public void closeTask(long id) throws TodoistException { + HttpResponse response = Unirest.post(URL_BASE + "/tasks/" + id + "/close") + .asString(); + if(response.getStatus() != HTTP_OK_NO_CONTENT) throw new TodoistException(response.getStatus()); } - public void deleteTask(long id) { - try { - HttpResponse response = Unirest.delete(URL_BASE + "/tasks/" + id) - .asString(); - if(response.getStatus() != HTTP_OK_NO_CONTENT) { - throw new Exception("HTTP STATUS " + response.getStatus()); - } - } catch (Exception e) { - e.printStackTrace(); - } + public void deleteTask(long id) throws TodoistException { + HttpResponse response = Unirest.delete(URL_BASE + "/tasks/" + id) + .asString(); + if(response.getStatus() != HTTP_OK_NO_CONTENT) throw new TodoistException(response.getStatus()); } - public List