From 68a352b354259f4751aa53129bd1dd90890e026f Mon Sep 17 00:00:00 2001 From: BaaaZen Date: Sat, 15 Oct 2016 00:25:20 +0200 Subject: [PATCH] Add deletePin --- .../java/nl/palolem/timeline/Timeline.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/main/java/nl/palolem/timeline/Timeline.java b/src/main/java/nl/palolem/timeline/Timeline.java index e14fd89..00fb33e 100644 --- a/src/main/java/nl/palolem/timeline/Timeline.java +++ b/src/main/java/nl/palolem/timeline/Timeline.java @@ -51,6 +51,35 @@ public static void sendPin(String token, Pin pin) throws IOException, PebbleExce } } + public static void deletePin(String token, Pin pin) throws IOException, PebbleException { + String url = String.format("%s/%s", PINS_URL, pin.getId()); + + HttpURLConnection connection = null; + + try { + connection = (HttpURLConnection) new URL(url).openConnection(); + connection.setDoOutput(true); + connection.setRequestMethod("DELETE"); + connection.setRequestProperty("Content-Type", "application/json"); + connection.setRequestProperty("X-User-Token", token); + + OutputStream os = connection.getOutputStream(); + mapper.writer().writeValue(os, pin); + os.flush(); + + int statusCode = connection.getResponseCode(); + if (statusCode != 200) { + InputStream is = connection.getErrorStream(); + Response response = mapper.readValue(is, Response.class); + throw new PebbleException(String.format("Error deleting pin: %s", response.getSummary()), response); + } + } finally { + if (connection != null) { + connection.disconnect(); + } + } + } + public static String serialize(Pin pin) throws JsonProcessingException { return mapper.writeValueAsString(pin); }