Open kleinmantara opened 2 years ago
Code example for the PushoverRestClient class with the dependencies to httpmime.
private static final ContentType plainUTF = ContentType.create("text/plain", MIME.UTF8_CHARSET); public Status pushMessage(PushoverMessage msg) throws PushoverException { final MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addTextBody("token", msg.getApiToken(), plainUTF); builder.addTextBody("user", msg.getUserId(), plainUTF); builder.addTextBody("message", msg.getMessage(), plainUTF); addBodyIfNotNull(builder, "title", msg.getTitle()); addBodyIfNotNull(builder, "url", msg.getUrl()); addBodyIfNotNull(builder, "url_title", msg.getTitle()); addBodyIfNotNull(builder, "device", msg.getDevice()); addBodyIfNotNull(builder, "timestamp", "" + msg.getTimestamp()); addBodyIfNotNull(builder, "sound", msg.getSound()); addBodyIfNotNull(builder, "html", msg.isHtml() ? "1" : "0"); if (!MessagePriority.NORMAL.equals(msg.getPriority())) { addBodyIfNotNull(builder, "priority", "" + msg.getPriority()); } if (msg.getFile != null) { builder.addBinaryBody("attachment", msg.getFile().getStream(), msg.getFile().getContentType(), msg.getFile().getName()); } HttpEntity multipart = builder.build(); HttpPost post = new HttpPost(PUSH_MESSAGE_URL); post.setEntity(multipart); try (CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpResponse response = httpClient.execute(post); return PushoverResponseFactory.createStatus(response); } catch (Exception e) { throw new PushoverException(e.getMessage(), e.getCause()); } } private void addBodyIfNotNull(MultipartEntityBuilder builder, String name, String value) { builder.addTextBody(name, value, plainUTF); }
Code example for the PushoverRestClient class with the dependencies to httpmime.