Refactorings
This commit is contained in:
		| @@ -3,11 +3,11 @@ | ||||
| JPushover | ||||
| ================ | ||||
|  | ||||
| A zero-dependency convenient class for sending messages to [Pushover][1] in Java. | ||||
| A minimal (15 KB), zero-dependency convenient class for sending messages to [Pushover][1] in Java. | ||||
|  | ||||
| Requires Java 11. | ||||
|  | ||||
| Support [Messages API][3] and [Glances API][4]. | ||||
| Supports [Messages API][3] and [Glances API][4]. | ||||
|  | ||||
| Usage | ||||
| ------------------ | ||||
|   | ||||
| @@ -6,9 +6,9 @@ import java.util.Objects; | ||||
| import java.util.TreeMap; | ||||
|  | ||||
| import de.svenkubiak.jpushover.enums.Param; | ||||
| import de.svenkubiak.jpushover.enums.Url; | ||||
| import de.svenkubiak.jpushover.http.PushoverRequest; | ||||
| import de.svenkubiak.jpushover.http.PushoverResponse; | ||||
| import de.svenkubiak.jpushover.utils.Urls; | ||||
| import de.svenkubiak.jpushover.utils.Validate; | ||||
|  | ||||
| /** | ||||
| @@ -17,7 +17,6 @@ import de.svenkubiak.jpushover.utils.Validate; | ||||
|  * | ||||
|  */ | ||||
| public class Glance { | ||||
|     private static final String GLANCE_URL = Urls.getGlanceUrl(); | ||||
|     private String token; | ||||
|     private String user; | ||||
|     private String device; | ||||
| @@ -137,6 +136,6 @@ public class Glance { | ||||
|         body.put(Param.PERCENT.toString(), String.valueOf(this.percent)); | ||||
|  | ||||
|          | ||||
|         return new PushoverRequest().push(GLANCE_URL, body, this.proxyHost, this.proxyPort); | ||||
|         return new PushoverRequest().push(Url.GLANCES.toString(), body, this.proxyHost, this.proxyPort); | ||||
|     } | ||||
| } | ||||
| @@ -8,9 +8,9 @@ import java.util.TreeMap; | ||||
| 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.http.PushoverRequest; | ||||
| import de.svenkubiak.jpushover.http.PushoverResponse; | ||||
| import de.svenkubiak.jpushover.utils.Urls; | ||||
| import de.svenkubiak.jpushover.utils.Validate; | ||||
|  | ||||
| /** | ||||
| @@ -19,8 +19,6 @@ import de.svenkubiak.jpushover.utils.Validate; | ||||
|  * | ||||
|  */ | ||||
| public class Message { | ||||
|     private static final String MESSAGE_URL = Urls.getMessageUrl(); | ||||
|     private static final String VALIDATION_URL = Urls.getValidationUrl(); | ||||
|     private Priority priority = Priority.NORMAL; | ||||
|     private Sound sound = Sound.PUSHOVER; | ||||
|     private String token; | ||||
| @@ -270,7 +268,7 @@ public class Message { | ||||
|         body.put(Param.TOKEN.toString(), this.token); | ||||
|         body.put(Param.USER.toString(), this.user); | ||||
|  | ||||
|         var pushoverResponse = new PushoverRequest().push(VALIDATION_URL, body, this.proxyHost, this.proxyPort); | ||||
|         var pushoverResponse = new PushoverRequest().push(Url.VALIDATE.toString(), body, this.proxyHost, this.proxyPort); | ||||
|          | ||||
|         var valid = false; | ||||
|         if (pushoverResponse.getHttpStatus() == 200) { | ||||
| @@ -354,7 +352,7 @@ public class Message { | ||||
|             body.put(Param.EXPIRE.toString(), String.valueOf(this.expire));  | ||||
|         } | ||||
|          | ||||
|         return new PushoverRequest().push(MESSAGE_URL, body, this.proxyHost, this.proxyPort); | ||||
|         return new PushoverRequest().push(Url.MESSAGES.toString(), body, this.proxyHost, this.proxyPort); | ||||
|     } | ||||
|  | ||||
|     public Priority getPriority() { | ||||
|   | ||||
							
								
								
									
										23
									
								
								src/main/java/de/svenkubiak/jpushover/enums/Url.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								src/main/java/de/svenkubiak/jpushover/enums/Url.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | ||||
| package de.svenkubiak.jpushover.enums; | ||||
|  | ||||
| /** | ||||
|  * | ||||
|  * @author svenkubiak | ||||
|  * | ||||
|  */ | ||||
| public enum Url { | ||||
|     GLANCES("https://api.pushover.net/1/glances.json"), | ||||
|     MESSAGES("https://api.pushover.net/1/messages.json"), | ||||
|     VALIDATE("https://api.pushover.net/1/users/validate.json"); | ||||
|  | ||||
|     private final String value; | ||||
|  | ||||
|     Url (String value) { | ||||
|         this.value = value; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public String toString() { | ||||
|         return this.value; | ||||
|     } | ||||
| } | ||||
| @@ -1,34 +0,0 @@ | ||||
| package de.svenkubiak.jpushover.utils; | ||||
|  | ||||
| /** | ||||
|  *  | ||||
|  * @author svenkubiak | ||||
|  * | ||||
|  */ | ||||
| public class Urls { | ||||
|     public static String getGlanceUrl() { | ||||
|         if (isTest()) { | ||||
|             return "http://127.0.0.1:8080/1/glances.json"; | ||||
|         } | ||||
|         return "https://api.pushover.net/1/glances.json"; | ||||
|     } | ||||
|      | ||||
|     public static String getMessageUrl() { | ||||
|         if (isTest()) { | ||||
|             return "http://127.0.0.1:8080/1/messages.json"; | ||||
|         } | ||||
|         return "https://api.pushover.net/1/messages.json"; | ||||
|     } | ||||
|      | ||||
|     public static String getValidationUrl() { | ||||
|         if (isTest()) { | ||||
|             return "http://127.0.0.1:8080/1/users/validate.json"; | ||||
|         } | ||||
|         return "https://api.pushover.net/1/users/validate.json"; | ||||
|     } | ||||
|      | ||||
|     private static boolean isTest() { | ||||
|         String mode = System.getProperty("mode"); | ||||
|         return ("test").equals(mode); | ||||
|     } | ||||
| } | ||||
| @@ -22,6 +22,7 @@ import jpushover.MockServer; | ||||
|  * | ||||
|  */ | ||||
| public class GlanceTest { | ||||
|     private static final String URL = "http://127.0.0.1:8080/1/glances.json"; | ||||
|     private static final String APPLICATION_JSON = "application/json; charset=utf-8"; | ||||
|     private static final String CONTENT_TYPE = "Content-Type"; | ||||
|      | ||||
| @@ -31,7 +32,7 @@ public class GlanceTest { | ||||
|      | ||||
|     @Test() | ||||
|     public void testTokenRequired() throws IOException, InterruptedException { | ||||
|         stubFor(post(urlEqualTo("/1/glances.json")) | ||||
|         stubFor(post(urlEqualTo(URL)) | ||||
|                 .willReturn(aResponse() | ||||
|                     .withStatus(400) | ||||
|                     .withHeader(CONTENT_TYPE, APPLICATION_JSON))); | ||||
| @@ -43,7 +44,7 @@ public class GlanceTest { | ||||
|      | ||||
|     @Test() | ||||
|     public void testUserRequired() throws IOException, InterruptedException { | ||||
|         stubFor(post(urlEqualTo("/1/glances.json")) | ||||
|         stubFor(post(urlEqualTo(URL)) | ||||
|                 .willReturn(aResponse() | ||||
|                     .withStatus(400) | ||||
|                     .withHeader(CONTENT_TYPE, APPLICATION_JSON))); | ||||
| @@ -55,7 +56,7 @@ public class GlanceTest { | ||||
|      | ||||
|     @Test() | ||||
|     public void testPushWithoutContent() throws IOException, InterruptedException { | ||||
|         stubFor(post(urlEqualTo("/1/glances.json")) | ||||
|         stubFor(post(urlEqualTo(URL)) | ||||
|                 .willReturn(aResponse() | ||||
|                     .withStatus(400) | ||||
|                     .withHeader(CONTENT_TYPE, APPLICATION_JSON))); | ||||
| @@ -66,7 +67,7 @@ public class GlanceTest { | ||||
|      | ||||
|     @Test | ||||
|     public void testPush() throws IOException, InterruptedException { | ||||
|         stubFor(post(urlEqualTo("/1/glances.json")) | ||||
|         stubFor(post(urlEqualTo(URL)) | ||||
|                 .willReturn(aResponse() | ||||
|                     .withStatus(200) | ||||
|                     .withHeader(CONTENT_TYPE, APPLICATION_JSON))); | ||||
|   | ||||
| @@ -27,6 +27,7 @@ import jpushover.MockServer; | ||||
|  * | ||||
|  */ | ||||
| public class MessageTest { | ||||
|     private static final String URL = "https://api.pushover.net/1/messages.json"; | ||||
|     private static final String APPLICATION_JSON = "application/json; charset=utf-8"; | ||||
|     private static final String CONTENT_TYPE = "Content-Type"; | ||||
|      | ||||
| @@ -36,7 +37,7 @@ public class MessageTest { | ||||
|      | ||||
|     @Test() | ||||
|     public void testTokenRequired() throws IOException, InterruptedException { | ||||
|         stubFor(post(urlEqualTo("/1/messages.json")) | ||||
|         stubFor(post(urlEqualTo(URL)) | ||||
|                 .willReturn(aResponse() | ||||
|                     .withStatus(400) | ||||
|                     .withHeader(CONTENT_TYPE, APPLICATION_JSON))); | ||||
| @@ -48,7 +49,7 @@ public class MessageTest { | ||||
|      | ||||
|     @Test() | ||||
|     public void testUserRequired() throws IOException, InterruptedException { | ||||
|         stubFor(post(urlEqualTo("/1/messages.json")) | ||||
|         stubFor(post(urlEqualTo(URL)) | ||||
|                 .willReturn(aResponse() | ||||
|                     .withStatus(400) | ||||
|                     .withHeader(CONTENT_TYPE, APPLICATION_JSON))); | ||||
| @@ -60,7 +61,7 @@ public class MessageTest { | ||||
|      | ||||
|     @Test() | ||||
|     public void testMessageRequired() throws IOException, InterruptedException { | ||||
|         stubFor(post(urlEqualTo("/1/messages.json")) | ||||
|         stubFor(post(urlEqualTo(URL)) | ||||
|                 .willReturn(aResponse() | ||||
|                     .withStatus(400) | ||||
|                     .withHeader(CONTENT_TYPE, APPLICATION_JSON))); | ||||
| @@ -72,7 +73,7 @@ public class MessageTest { | ||||
|      | ||||
|     @Test() | ||||
|     public void testPushWithoutContent() throws IOException, InterruptedException { | ||||
|         stubFor(post(urlEqualTo("/1/messages.json")) | ||||
|         stubFor(post(urlEqualTo(URL)) | ||||
|                 .willReturn(aResponse() | ||||
|                     .withStatus(400) | ||||
|                     .withHeader(CONTENT_TYPE, APPLICATION_JSON))); | ||||
| @@ -83,7 +84,7 @@ public class MessageTest { | ||||
|      | ||||
|     @Test | ||||
|     public void testPush() throws IOException, InterruptedException { | ||||
|         stubFor(post(urlEqualTo("/1/messages.json")) | ||||
|         stubFor(post(urlEqualTo(URL)) | ||||
|                 .willReturn(aResponse() | ||||
|                     .withStatus(200) | ||||
|                     .withHeader(CONTENT_TYPE, APPLICATION_JSON))); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Sven Kubiak
					Sven Kubiak