com.files.models.Request Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of files-sdk Show documentation
Show all versions of files-sdk Show documentation
The Files.com Java client library provides convenient access to the Files.com API from JVM based applications.
package com.files.models;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.files.FilesClient;
import com.files.FilesConfig;
import com.files.ListIterator;
import com.files.net.HttpMethods.RequestMethods;
import com.files.util.FilesInputStream;
import com.files.util.ModelUtils;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.Getter;
import lombok.Setter;
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Request {
private HashMap options;
private ObjectMapper objectMapper = JsonMapper
.builder()
.disable(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS)
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.defaultDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX"))
.build();
public Request() {
this(null, null);
}
public Request(HashMap parameters) {
this(parameters, null);
}
public Request(HashMap parameters, HashMap options) {
this.options = options;
try {
ObjectReader objectReader = objectMapper.readerForUpdating(this);
objectReader.readValue(objectMapper.writeValueAsString(parameters));
} catch (JsonProcessingException e) {
// TODO: error generation on constructor
}
}
/**
* Request ID
*/
@Getter
@Setter
@JsonProperty("id")
public Long id;
/**
* Folder path This must be slash-delimited, but it must neither start nor end with a slash. Maximum of 5000 characters.
*/
@Getter
@Setter
@JsonProperty("path")
public String path;
/**
* Source filename, if applicable
*/
@Getter
@Setter
@JsonProperty("source")
public String source;
/**
* Destination filename
*/
@Getter
@Setter
@JsonProperty("destination")
public String destination;
/**
* ID of automation that created request
*/
@Getter
@Setter
@JsonProperty("automation_id")
public String automationId;
/**
* User making the request (if applicable)
*/
@Getter
@Setter
@JsonProperty("user_display_name")
public String userDisplayName;
/**
* A list of user IDs to request the file from. If sent as a string, it should be comma-delimited.
*/
@Getter
@Setter
@JsonProperty("user_ids")
public String userIds;
/**
* A list of group IDs to request the file from. If sent as a string, it should be comma-delimited.
*/
@Getter
@Setter
@JsonProperty("group_ids")
public String groupIds;
/**
*/
public void delete(HashMap parameters) {
delete(parameters);
}
public void destroy(HashMap parameters) {
delete(parameters);
}
public void save() throws IOException {
HashMap parameters = ModelUtils.toParameterMap(objectMapper.writeValueAsString(this));
if (parameters.containsKey("id") && parameters.get("id") != null) {
throw new UnsupportedOperationException("The Request Object doesn't support updates.");
} else {
Request.create(parameters, this.options);
}
}
/**
* Parameters:
* cursor - string - Used for pagination. When a list request has more records available, cursors are provided in the response headers `X-Files-Cursor-Next` and `X-Files-Cursor-Prev`. Send one of those cursor value here to resume an existing list from the next available record. Note: many of our SDKs have iterator methods that will automatically handle cursor-based pagination.
* per_page - int64 - Number of records to show per page. (Max: 10,000, 1,000 or less is recommended).
* sort_by - object - If set, sort records by the specified field in either `asc` or `desc` direction (e.g. `sort_by[destination]=desc`). Valid fields are `destination`.
* mine - boolean - Only show requests of the current user? (Defaults to true if current user is not a site admin.)
* path - string - Path to show requests for. If omitted, shows all paths. Send `/` to represent the root directory.
*/
public static ListIterator list() throws RuntimeException {
return list(null, null);
}
public static ListIterator list(HashMap parameters) throws RuntimeException {
return list(parameters, null);
}
public static ListIterator list(HashMap parameters, HashMap options) throws RuntimeException {
parameters = parameters != null ? parameters : new HashMap();
options = options != null ? options : new HashMap();
if (parameters.containsKey("cursor") && !(parameters.get("cursor") instanceof String)) {
throw new IllegalArgumentException("Bad parameter: cursor must be of type String parameters[\"cursor\"]");
}
if (parameters.containsKey("per_page") && !(parameters.get("per_page") instanceof Long)) {
throw new IllegalArgumentException("Bad parameter: per_page must be of type Long parameters[\"per_page\"]");
}
if (parameters.containsKey("sort_by") && !(parameters.get("sort_by") instanceof Map)) {
throw new IllegalArgumentException("Bad parameter: sort_by must be of type Map parameters[\"sort_by\"]");
}
if (parameters.containsKey("mine") && !(parameters.get("mine") instanceof Boolean)) {
throw new IllegalArgumentException("Bad parameter: mine must be of type Boolean parameters[\"mine\"]");
}
if (parameters.containsKey("path") && !(parameters.get("path") instanceof String)) {
throw new IllegalArgumentException("Bad parameter: path must be of type String parameters[\"path\"]");
}
String url = String.format("%s%s/requests", FilesConfig.getInstance().getApiRoot(), FilesConfig.getInstance().getApiBase());
TypeReference> typeReference = new TypeReference>() {};
return FilesClient.requestList(url, RequestMethods.GET, typeReference, parameters, options);
}
public static ListIterator all() throws RuntimeException {
return all(null, null);
}
public static ListIterator all(HashMap parameters, HashMap options) throws RuntimeException {
return list(parameters, options);
}
/**
* Parameters:
* cursor - string - Used for pagination. When a list request has more records available, cursors are provided in the response headers `X-Files-Cursor-Next` and `X-Files-Cursor-Prev`. Send one of those cursor value here to resume an existing list from the next available record. Note: many of our SDKs have iterator methods that will automatically handle cursor-based pagination.
* per_page - int64 - Number of records to show per page. (Max: 10,000, 1,000 or less is recommended).
* sort_by - object - If set, sort records by the specified field in either `asc` or `desc` direction (e.g. `sort_by[destination]=desc`). Valid fields are `destination`.
* mine - boolean - Only show requests of the current user? (Defaults to true if current user is not a site admin.)
* path (required) - string - Path to show requests for. If omitted, shows all paths. Send `/` to represent the root directory.
*/
public static Request getFolder() throws RuntimeException {
return getFolder(null, null, null);
}
public static Request getFolder(String path, HashMap parameters) throws RuntimeException {
return getFolder(path, parameters, null);
}
public static Request getFolder(HashMap parameters, HashMap options) throws RuntimeException {
return getFolder(null, parameters, options);
}
public static Request getFolder(String path, HashMap parameters, HashMap options) throws RuntimeException {
parameters = parameters != null ? parameters : new HashMap();
options = options != null ? options : new HashMap();
if (path == null && parameters.containsKey("path") && parameters.get("path") != null) {
path = (String) parameters.get("path");
}
if (path == null) {
throw new NullPointerException("Argument or Parameter missing: path parameters[\"path\"]");
}
if (parameters.containsKey("cursor") && !(parameters.get("cursor") instanceof String)) {
throw new IllegalArgumentException("Bad parameter: cursor must be of type String parameters[\"cursor\"]");
}
if (parameters.containsKey("per_page") && !(parameters.get("per_page") instanceof Long)) {
throw new IllegalArgumentException("Bad parameter: per_page must be of type Long parameters[\"per_page\"]");
}
if (parameters.containsKey("sort_by") && !(parameters.get("sort_by") instanceof Map)) {
throw new IllegalArgumentException("Bad parameter: sort_by must be of type Map parameters[\"sort_by\"]");
}
if (parameters.containsKey("mine") && !(parameters.get("mine") instanceof Boolean)) {
throw new IllegalArgumentException("Bad parameter: mine must be of type Boolean parameters[\"mine\"]");
}
if (!(path instanceof String)) {
throw new IllegalArgumentException("Bad parameter: path must be of type String parameters[\"path\"]");
}
String urlParts[] = {FilesConfig.getInstance().getApiRoot(), FilesConfig.getInstance().getApiBase(), path};
for (int i = 2; i < urlParts.length; i++) {
try {
urlParts[i] = new URI(null, null, urlParts[i], null).getRawPath();
} catch (URISyntaxException ex) {
// NOOP
}
}
String url = String.format("%s%s/requests/folders/%s", urlParts);
TypeReference typeReference = new TypeReference() {};
return FilesClient.requestItem(url, RequestMethods.GET, typeReference, parameters, options);
}
/**
* Parameters:
* path (required) - string - Folder path on which to request the file.
* destination (required) - string - Destination filename (without extension) to request.
* user_ids - string - A list of user IDs to request the file from. If sent as a string, it should be comma-delimited.
* group_ids - string - A list of group IDs to request the file from. If sent as a string, it should be comma-delimited.
*/
public static Request create() throws RuntimeException {
return create(null, null);
}
public static Request create(HashMap parameters) throws RuntimeException {
return create(parameters, null);
}
public static Request create(HashMap parameters, HashMap options) throws RuntimeException {
parameters = parameters != null ? parameters : new HashMap();
options = options != null ? options : new HashMap();
if (!parameters.containsKey("path") || parameters.get("path") == null) {
throw new NullPointerException("Parameter missing: path parameters[\"path\"]");
}
if (!parameters.containsKey("destination") || parameters.get("destination") == null) {
throw new NullPointerException("Parameter missing: destination parameters[\"destination\"]");
}
if (parameters.containsKey("path") && !(parameters.get("path") instanceof String)) {
throw new IllegalArgumentException("Bad parameter: path must be of type String parameters[\"path\"]");
}
if (parameters.containsKey("destination") && !(parameters.get("destination") instanceof String)) {
throw new IllegalArgumentException("Bad parameter: destination must be of type String parameters[\"destination\"]");
}
if (parameters.containsKey("user_ids") && !(parameters.get("user_ids") instanceof String)) {
throw new IllegalArgumentException("Bad parameter: user_ids must be of type String parameters[\"user_ids\"]");
}
if (parameters.containsKey("group_ids") && !(parameters.get("group_ids") instanceof String)) {
throw new IllegalArgumentException("Bad parameter: group_ids must be of type String parameters[\"group_ids\"]");
}
String url = String.format("%s%s/requests", FilesConfig.getInstance().getApiRoot(), FilesConfig.getInstance().getApiBase());
TypeReference typeReference = new TypeReference() {};
return FilesClient.requestItem(url, RequestMethods.POST, typeReference, parameters, options);
}
/**
*/
public static void delete() throws RuntimeException {
delete(null, null, null);
}
public static void delete(Long id, HashMap parameters) throws RuntimeException {
delete(id, parameters, null);
}
public static void delete(HashMap parameters, HashMap options) throws RuntimeException {
delete(null, parameters, options);
}
public static void delete(Long id, HashMap parameters, HashMap options) throws RuntimeException {
parameters = parameters != null ? parameters : new HashMap();
options = options != null ? options : new HashMap();
if (id == null && parameters.containsKey("id") && parameters.get("id") != null) {
id = (Long) parameters.get("id");
}
if (id == null) {
throw new NullPointerException("Argument or Parameter missing: id parameters[\"id\"]");
}
if (!(id instanceof Long)) {
throw new IllegalArgumentException("Bad parameter: id must be of type Long parameters[\"id\"]");
}
String urlParts[] = {FilesConfig.getInstance().getApiRoot(), FilesConfig.getInstance().getApiBase(), String.valueOf(id)};
for (int i = 2; i < urlParts.length; i++) {
try {
urlParts[i] = new URI(null, null, urlParts[i], null).getRawPath();
} catch (URISyntaxException ex) {
// NOOP
}
}
String url = String.format("%s%s/requests/%s", urlParts);
FilesClient.apiRequest(url, RequestMethods.DELETE, parameters, options);
}
public static void destroy() throws RuntimeException {
destroy(null, null, null);
}
public static void destroy(Long id, HashMap parameters, HashMap options) throws RuntimeException {
delete(id, parameters, options);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy