de.SweetCode.SteamAPI.method.SteamMethodVersion Maven / Gradle / Ivy
Show all versions of SteamAPI Show documentation
package de.SweetCode.SteamAPI.method;
import de.SweetCode.SteamAPI.SteamHTTPMethod;
import de.SweetCode.SteamAPI.SteamHost;
import de.SweetCode.SteamAPI.SteamVersion;
import de.SweetCode.SteamAPI.SteamVisibility;
import de.SweetCode.SteamAPI.exceptions.SteamDependencyException;
import de.SweetCode.SteamAPI.exceptions.SteamMissingInputException;
import de.SweetCode.SteamAPI.method.input.Input;
import de.SweetCode.SteamAPI.method.option.Option;
import java.util.*;
/**
*
* A {@link SteamMethod} can have various versions supporting different {@link Option Options}, {@link SteamVersion Steam Versions}
* and/or {@link SteamHost Steam Hosts}.
*
*/
public class SteamMethodVersion {
private final SteamHTTPMethod httpMethod;
private final List hosts;
private final SteamVersion version;
private final SteamVisibility visibility;
private final Map options = new TreeMap<>();
/**
*
* Creates a new SteamMethodVersion instance.
*
*
* @param method The supported HTTP request method.
* @param hosts All supported hosts.
* @param version The version the options belong to.
* @param visibility The visibility of the method.
*/
public SteamMethodVersion(SteamHTTPMethod method, List hosts, SteamVersion version, SteamVisibility visibility) {
//@TODO Verify version
assert !(method == null);
assert !(hosts == null);
assert !(hosts.isEmpty());
assert !(version == null);
assert !(visibility == null);
this.httpMethod = method;
this.hosts = hosts;
this.version = version;
this.visibility = visibility;
}
/**
*
* The supported HTTP request method.
*
*
* @return the supported HTTP request method, never null.
*/
public SteamHTTPMethod getMethod() {
return this.httpMethod;
}
/**
*
* All supported hosts.
*
*
* @return a list of supported hosts.
*/
public List getHosts() {
return this.hosts;
}
/**
*
* The version these options belong to.
*
*
* @return the version, never null.
*/
public SteamVersion getVersion() {
return this.version;
}
/**
*
* Gives the visibility of the method.
*
*
* @return The visibility of the method, never null.
*/
public SteamVisibility getVisibility() {
return this.visibility;
}
/**
*
* Gives the option related to the key.
*
*
* @param key The key of the option.
*
* @return the related option, never null.
*/
public Option get(String key) {
//@TODO Verify input
assert !(key == null);
assert this.options.containsKey(key);
return this.options.get(key);
}
/**
*
* Adds a option if the option does not exist yet.
*
*
* @param option The option to add.
*/
public void add(Option option) {
//@TODO Verify input
assert !(option == null);
assert !(this.options.containsKey(option.getKey()));
this.options.put(option.getKey(), option);
}
/**
*
* Verifies if the input fits these SteamMethodVersion.
*
*
* @param steamMethod The SteamMethod calling the verify function.
* @param host The host which is supposed to receive the provided input.
* @param input The provided input.
*
* @return True, if the input fits, otherwise it throws exception.
*/
public boolean verify(SteamMethod steamMethod, SteamHost host, Input input) {
//@TODO Verify input
assert !(steamMethod == null);
assert !(input == null);
Map data = input.getValues();
for(Map.Entry e : this.options.entrySet()) {
String key = e.getKey();
Option option = e.getValue();
//--- If the option is required & DOES NOT exist -> onError
if(
(option.isRequired() || (host == SteamHost.PARTNER && option.isPartnerRequired())) &&
!(data.containsKey(key))
) {
throw new SteamMissingInputException(steamMethod, option);
}
//--- If the key does exist in the input & the value is of the wrong type -> onError
if(data.containsKey(key) && !(option.getOptionType().check(data.get(key)))) {
throw new IllegalArgumentException(String.format(
"The method %s expected a %s for the key %s. The value %s doesn't fit the OptionType.",
steamMethod.getName(),
option.getOptionType().getName(),
option.getKey(),
String.valueOf(data.get(key))
));
}
//--- Check for dependencies
if(data.containsKey(key)) {
option.getDependencies().forEach(d -> {
//--- If the input data doesn't contain the required dependecy
if(!(data.containsKey(d))) {
throw new SteamDependencyException(steamMethod, option, d);
}
});
}
}
return true;
}
/**
*
* Creates a new instance of {@link Builder}.
*
*
* @return the builder instance.
*/
public static Builder create() {
return new Builder();
}
/**
*
* A builder pattern class to buildRequest easier SteamMethodVersion.
*
*/
public static class Builder {
private SteamHTTPMethod method = null;
private List hosts = null;
private SteamVersion version = null;
private SteamVisibility visibility = null;
private List