168 lines
4.9 KiB
Java
Raw Normal View History

package de.svenkubiak.jpushover.apis;
import java.util.NavigableMap;
import java.util.Objects;
import java.util.TreeMap;
2020-07-27 09:22:49 +02:00
import java.util.concurrent.Future;
import de.svenkubiak.jpushover.enums.Param;
2020-03-06 08:35:42 +01:00
import de.svenkubiak.jpushover.enums.Url;
2021-06-20 12:01:46 +02:00
import de.svenkubiak.jpushover.exceptions.JPushoverException;
import de.svenkubiak.jpushover.http.PushoverRequest;
import de.svenkubiak.jpushover.http.PushoverResponse;
2020-07-27 09:22:49 +02:00
import de.svenkubiak.jpushover.services.AsyncExecutor;
import de.svenkubiak.jpushover.services.AsyncService;
import de.svenkubiak.jpushover.utils.Validate;
/**
*
* @author svenkubiak
*
*/
2020-07-27 09:22:49 +02:00
public class Glance implements API {
2020-07-26 19:06:36 +02:00
private NavigableMap<String, String> body = new TreeMap<>();
2020-03-05 14:51:53 +01:00
private String proxyHost;
private int proxyPort;
2020-07-27 13:35:49 +02:00
public Glance() {
}
2020-07-26 19:06:36 +02:00
/**
* Your application's API token
* (required)
*
* @param token The pushover API token
* @return Glance instance
*/
2020-07-27 13:35:49 +02:00
public Glance withToken(String token) {
Objects.requireNonNull(token, "token can not be null");
2020-07-26 19:06:36 +02:00
body.put(Param.TOKEN.toString(), token);
return this;
}
2020-07-26 19:06:36 +02:00
/**
* The user/group key (not e-mail address) of your user (or you),
* viewable when logged into the @see <a href="https://pushover.net/login">pushover dashboard</a>
* (required)
*
* @param user The username
* @return Glance instance
*/
2020-07-27 13:35:49 +02:00
public Glance withUser(String user) {
Objects.requireNonNull(user, "user can not be null");
2020-07-26 19:06:36 +02:00
body.put(Param.USER.toString(), user);
return this;
}
2020-07-26 19:06:36 +02:00
/**
* Your user's device name to send the message directly to that device,
* rather than all of the user's devices
* (optional)
*
* @param device The device name
* @return Glance instance
*/
2020-07-27 13:35:49 +02:00
public Glance withDevice(String device) {
Objects.requireNonNull(device, "device can not be null");
2020-07-26 19:06:36 +02:00
body.put(Param.DEVICE.toString(), device);
return this;
}
/**
* A description of the data being shown, such as "Widgets Sold"
*
* @param title the title to use
* @return Glance instance
*/
2020-07-27 13:35:49 +02:00
public Glance withTitle(String title) {
Objects.requireNonNull(title, "title can not be null");
Validate.checkArgument(title.length() <= 100, "Title must not exceed a length of 100 characters");
2020-07-26 19:06:36 +02:00
body.put(Param.TITLE.toString(), title);
return this;
}
/**
* The main line of data, used on most screens
*
* @param text the text to use
* @return Glance instance
*/
2020-07-27 13:35:49 +02:00
public Glance withText(String text) {
Objects.requireNonNull(text, "text can not be null");
Validate.checkArgument(text.length() <= 100, "Text must not exceed a length of 100 characters");
2020-07-26 19:06:36 +02:00
body.put(Param.TEXT.toString(), text);
return this;
}
/**
* A second line of data
*
* @param subtext the subtext to use
* @return Glance instance
*/
2020-07-27 13:35:49 +02:00
public Glance withSubtext(String subtext) {
Objects.requireNonNull(subtext, "subtext can not be null");
2020-03-05 14:51:53 +01:00
Validate.checkArgument(subtext.length() <= 100, "Subtext must not exceed a length of 100 characters");
2020-07-26 19:06:36 +02:00
body.put(Param.SUBTEXT.toString(), subtext);
return this;
}
/**
* Shown on smaller screens; useful for simple counts
*
* @param count the count to use
* @return Glance instance
*/
2020-07-27 13:35:49 +02:00
public Glance withCount(int count) {
2020-07-26 19:06:36 +02:00
body.put(Param.COUNT.toString(), String.valueOf(count));
return this;
}
/**
* Shown on some screens as a progress bar/circle
*
* @param percent the percent to use
* @return GLance instance
*/
2020-07-27 13:35:49 +02:00
public Glance withPercent(int percent) {
2020-07-26 19:06:36 +02:00
body.put(Param.PERCENT.toString(), String.valueOf(percent));
return this;
}
/**
* Sends a glance to pushover
*
* @return PushoverResponse instance
2021-06-20 12:01:46 +02:00
*
* @throws JPushoverException
*/
2020-07-27 09:22:49 +02:00
@Override
2021-06-20 12:01:46 +02:00
public PushoverResponse push() throws JPushoverException {
2020-07-26 19:06:36 +02:00
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");
2020-03-06 08:35:42 +01:00
return new PushoverRequest().push(Url.GLANCES.toString(), body, this.proxyHost, this.proxyPort);
}
2020-07-27 09:22:49 +02:00
/**
* Sends a glance to pushover asynchronously
*
* @return PushoverResponse instance
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
2021-06-20 12:01:46 +02:00
public Future<PushoverResponse> pushAsync() {
2020-07-27 09:22:49 +02:00
return AsyncService.getInstance().execute(new AsyncExecutor(this));
}
2020-07-27 13:35:49 +02:00
public String getValue(String param) {
Objects.requireNonNull(param, "param can not be null");
return body.get(param);
}
}