Minor refactoring

This commit is contained in:
Sven Kubiak 2021-06-20 12:01:46 +02:00
parent 105a3f26c0
commit a61fcc53c1
8 changed files with 33 additions and 39 deletions

View File

@ -1,7 +1,6 @@
package de.svenkubiak.jpushover.apis;
import java.io.IOException;
import de.svenkubiak.jpushover.exceptions.JPushoverException;
import de.svenkubiak.jpushover.http.PushoverResponse;
/**
@ -10,5 +9,5 @@ import de.svenkubiak.jpushover.http.PushoverResponse;
*
*/
public interface API {
PushoverResponse push() throws IOException, InterruptedException;
PushoverResponse push() throws JPushoverException;
}

View File

@ -1,14 +1,13 @@
package de.svenkubiak.jpushover.apis;
import java.io.IOException;
import java.util.NavigableMap;
import java.util.Objects;
import java.util.TreeMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import de.svenkubiak.jpushover.enums.Param;
import de.svenkubiak.jpushover.enums.Url;
import de.svenkubiak.jpushover.exceptions.JPushoverException;
import de.svenkubiak.jpushover.http.PushoverRequest;
import de.svenkubiak.jpushover.http.PushoverResponse;
import de.svenkubiak.jpushover.services.AsyncExecutor;
@ -140,12 +139,11 @@ public class Glance implements API {
* Sends a glance to pushover
*
* @return PushoverResponse instance
*
* @throws IOException if sending the message fails
* @throws InterruptedException if sending the message fails
*
* @throws JPushoverException
*/
@Override
public PushoverResponse push() throws IOException, InterruptedException {
public PushoverResponse push() throws JPushoverException {
Objects.requireNonNull(body.get(Param.TOKEN.toString()), "Token is required for a glance");
Objects.requireNonNull(body.get(Param.USER.toString()), "User is required for a glance");
@ -156,12 +154,9 @@ public class Glance implements API {
* Sends a glance to pushover asynchronously
*
* @return PushoverResponse instance
*
* @throws InterruptedException if sending the message fails
* @throws ExecutionException if sending the message fails
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public Future<PushoverResponse> pushAsync() throws InterruptedException, ExecutionException {
public Future<PushoverResponse> pushAsync() {
return AsyncService.getInstance().execute(new AsyncExecutor(this));
}

View File

@ -1,6 +1,5 @@
package de.svenkubiak.jpushover.apis;
import java.io.IOException;
import java.util.NavigableMap;
import java.util.Objects;
import java.util.TreeMap;
@ -11,6 +10,7 @@ import de.svenkubiak.jpushover.enums.Param;
import de.svenkubiak.jpushover.enums.Priority;
import de.svenkubiak.jpushover.enums.Sound;
import de.svenkubiak.jpushover.enums.Url;
import de.svenkubiak.jpushover.exceptions.JPushoverException;
import de.svenkubiak.jpushover.http.PushoverRequest;
import de.svenkubiak.jpushover.http.PushoverResponse;
import de.svenkubiak.jpushover.services.AsyncExecutor;
@ -247,10 +247,9 @@ public class Message implements API {
*
* @return true if token and user are valid and at least on device is on the account, false otherwise
*
* @throws IOException if validation fails
* @throws InterruptedException if validation fails
* @throws JPushoverException
*/
public boolean validate() throws IOException, InterruptedException {
public boolean validate() throws JPushoverException {
Objects.requireNonNull(body.get(Param.TOKEN.toString()), "Token is required for validation");
Objects.requireNonNull(body.get(Param.USER.toString()), "User is required for validation");
@ -272,11 +271,10 @@ public class Message implements API {
*
* @return PushoverResponse instance
*
* @throws IOException if sending the message fails
* @throws InterruptedException if sending the message fails
* @throws JPushoverException
*/
@Override
public PushoverResponse push() throws IOException, InterruptedException {
public PushoverResponse push() throws JPushoverException {
Objects.requireNonNull(body.get(Param.TOKEN.toString()), "Token is required for a message");
Objects.requireNonNull(body.get(Param.USER.toString()), "User is required for a message");
Objects.requireNonNull(body.get(Param.MESSAGE.toString()), "Message is required for a message");

View File

@ -32,7 +32,7 @@ public class OpenClient {
*
* @param email Your Pushover email address
* @param password Your Pushover password
* @param twofa Your Your current Pushover two-factor code (if enabled)
* @param twofa Your current Pushover two-factor code (if enabled)
*
* @return A PushoverResponse
* @throws JPushoverException if something went wrong with the HTTP request
@ -74,7 +74,7 @@ public class OpenClient {
.response(response.body())
.isSuccessful((response.statusCode() == 200) ? true : false);
} catch (IOException | InterruptedException e) {
throw new JPushoverException("Login failed", e);
throw new JPushoverException("Pushover Login failed", e);
}
return pushoverResponse;
@ -97,7 +97,7 @@ public class OpenClient {
* Retrieves all available messages for the given deviceId
*
* @param secret Your Pushover secret retrieved after login
* @param deviceId The deviceId to get the messages
* @param deviceId The deviceId from whom to get the messages
*
* @return A String containing raw Json with all available messages or null
* @throws JPushoverException if something went wrong with the HTTP request
@ -137,7 +137,7 @@ public class OpenClient {
* Deletes all messages after (and including) a given messagesId
*
* @param secret Your Pushover secret retrieved after login
* @param deviceId The deviceId to get the messages
* @param deviceId The deviceId whom to get the messages
* @param messageId The messagesId
*
* @return A PushoverResponse
@ -146,7 +146,7 @@ public class OpenClient {
public PushoverResponse deleteMessages(String secret, String deviceId, String messageId) throws JPushoverException {
Objects.requireNonNull(deviceId, "secret can not be null");
Objects.requireNonNull(secret, "deviceId can not be null");
Objects.requireNonNull(messageId, "deviceId can not be null");
Objects.requireNonNull(messageId, "messageId can not be null");
StringBuilder params = new StringBuilder()
.append("secret")
@ -172,7 +172,6 @@ public class OpenClient {
.httpStatus(response.statusCode())
.response(response.body())
.isSuccessful((response.statusCode() == 200) ? true : false);
} catch (IOException | InterruptedException e) {
throw new JPushoverException("Failed to delete messages", e);
}
@ -181,13 +180,13 @@ public class OpenClient {
}
/**
* Establishes a WebSocket connect which listens to new messages
* Establishes a WebSocket connection which listens to incoming messages
*
* @param secret Your Pushover secret retrieved after login
* @param deviceId The deviceId to get the messages
* @param deviceId The deviceId from whom to get the messages
* @param messageListener Your instance of a MessagesListener
*
* @return True if the connection was established successful
* @return True if the connection was established successfully
*/
public boolean listen(String secret, String deviceId, MessageListener messageListener) {
Objects.requireNonNull(secret, "secret can not be null");
@ -243,12 +242,10 @@ public class OpenClient {
PushoverResponse pushoverResponse = PushoverResponse.create().isSuccessful(false);
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() == 200) {
pushoverResponse
pushoverResponse
.httpStatus(response.statusCode())
.response(response.body())
.isSuccessful(true);
}
} catch (IOException | InterruptedException e) {
throw new JPushoverException("Failed to register new device", e);
}

View File

@ -11,4 +11,4 @@ public class JPushoverException extends Exception{
public JPushoverException(String message, Exception e) {
super(message, e);
}
}
}

View File

@ -14,6 +14,8 @@ import java.util.NavigableMap;
import java.util.Objects;
import java.util.OptionalLong;
import de.svenkubiak.jpushover.exceptions.JPushoverException;
/**
*
* @author svenkubiak
@ -21,7 +23,7 @@ import java.util.OptionalLong;
*/
public class PushoverRequest {
public PushoverResponse push(String url, NavigableMap<String, String> body, String proxyHost, int proxyPort) throws IOException, InterruptedException {
public PushoverResponse push(String url, NavigableMap<String, String> body, String proxyHost, int proxyPort) throws JPushoverException {
Objects.requireNonNull(url, "API URL can not be null");
Objects.requireNonNull(body, "body can not be null");
@ -39,7 +41,7 @@ public class PushoverRequest {
return jPushoverResponse;
}
private HttpResponse<String> getResponse(String body, String url, String proxyHost, int proxyPort) throws IOException, InterruptedException {
private HttpResponse<String> getResponse(String body, String url, String proxyHost, int proxyPort) throws JPushoverException {
var httpRequest = HttpRequest.newBuilder()
.uri(URI.create(url))
.timeout(Duration.ofSeconds(5))
@ -54,7 +56,11 @@ public class PushoverRequest {
httpClientBuilder.proxy(ProxySelector.of(new InetSocketAddress(proxyHost, proxyPort)));
}
return httpClientBuilder.build().send(httpRequest, HttpResponse.BodyHandlers.ofString());
try {
return httpClientBuilder.build().send(httpRequest, HttpResponse.BodyHandlers.ofString());
} catch (IOException | InterruptedException e) {
throw new JPushoverException("Failed to execute HTTP request", e);
}
}
private String toJson(NavigableMap<String, String> body) {

View File

@ -7,7 +7,7 @@ package de.svenkubiak.jpushover.interfaces;
*/
public interface MessageListener {
/**
* Called when a new message/new messages is available
* Called when a new message is available/new messages are available
*/
void onMessage();

View File

@ -1,6 +1,5 @@
package de.svenkubiak.jpushover.services;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
@ -25,7 +24,7 @@ public class AsyncService<T> {
return INSTANCE;
}
public Future<PushoverResponse> execute(AsyncExecutor<PushoverResponse> asyncExecutor) throws InterruptedException, ExecutionException {
public Future<PushoverResponse> execute(AsyncExecutor<PushoverResponse> asyncExecutor) {
return executorService.submit(asyncExecutor);
}