use of com.eclipsesource.json.JsonArray in project box-java-sdk by box.
the class BoxCollaboration method getPendingCollaborations.
/**
* Gets all pending collaboration invites for the current user.
*
* @param api the API connection to use.
* @return a collection of pending collaboration infos.
*/
public static Collection<Info> getPendingCollaborations(BoxAPIConnection api) {
URL url = PENDING_COLLABORATIONS_URL.build(api.getBaseURL());
BoxAPIRequest request = new BoxAPIRequest(api, url, "GET");
BoxJSONResponse response = (BoxJSONResponse) request.send();
JsonObject responseJSON = Json.parse(response.getJSON()).asObject();
int entriesCount = responseJSON.get("total_count").asInt();
Collection<BoxCollaboration.Info> collaborations = new ArrayList<>(entriesCount);
JsonArray entries = responseJSON.get("entries").asArray();
for (JsonValue entry : entries) {
JsonObject entryObject = entry.asObject();
BoxCollaboration collaboration = new BoxCollaboration(api, entryObject.get("id").asString());
BoxCollaboration.Info info = collaboration.new Info(entryObject);
collaborations.add(info);
}
return collaborations;
}
use of com.eclipsesource.json.JsonArray in project box-java-sdk by box.
the class BatchAPIRequest method prepareRequest.
/**
* @param requests list of api requests that has to be executed in batch.
* @deprecated As of 2.39.0, BatchAPI Request will no longer be supported.
* <p>
* Prepare a batch api request using list of individual reuests.
*/
@Deprecated
protected void prepareRequest(List<BoxAPIRequest> requests) {
JsonObject body = new JsonObject();
JsonArray requestsJSONArray = new JsonArray();
for (BoxAPIRequest request : requests) {
JsonObject batchRequest = new JsonObject();
batchRequest.add("method", request.getMethod());
batchRequest.add("relative_url", request.getUrl().toString().substring(this.api.getBaseURL().length() - 1));
// If the actual request has a JSON body then add it to vatch request
if (request instanceof BoxJSONRequest) {
BoxJSONRequest jsonRequest = (BoxJSONRequest) request;
batchRequest.add("body", jsonRequest.getBodyAsJsonValue());
}
// Add any headers that are in the request, except Authorization
if (request.getHeaders() != null) {
JsonObject batchRequestHeaders = new JsonObject();
for (RequestHeader header : request.getHeaders()) {
if (header.getKey() != null && !header.getKey().isEmpty() && !HttpHeaders.AUTHORIZATION.equals(header.getKey())) {
batchRequestHeaders.add(header.getKey(), header.getValue());
}
}
batchRequest.add("headers", batchRequestHeaders);
}
// Add the request to array
requestsJSONArray.add(batchRequest);
}
// Add the requests array to body
body.add("requests", requestsJSONArray);
super.setBody(body);
}
use of com.eclipsesource.json.JsonArray in project box-java-sdk by box.
the class BoxCollection method getItemsRange.
/**
* Retrieves a specific range of items in this collection.
*
* @param offset the index of the first item to retrieve.
* @param limit the maximum number of items to retrieve after the offset.
* @param fields the fields to retrieve.
* @return a partial collection containing the specified range of items.
*/
public PartialCollection<BoxItem.Info> getItemsRange(long offset, long limit, String... fields) {
QueryStringBuilder builder = new QueryStringBuilder().appendParam("offset", offset).appendParam("limit", limit);
if (fields.length > 0) {
builder.appendParam("fields", fields);
}
URL url = GET_COLLECTION_ITEMS_URL.buildWithQuery(getAPI().getBaseURL(), builder.toString(), getID());
BoxAPIRequest request = new BoxAPIRequest(this.getAPI(), url, "GET");
BoxJSONResponse response = (BoxJSONResponse) request.send();
JsonObject responseJSON = Json.parse(response.getJSON()).asObject();
String totalCountString = responseJSON.get("total_count").toString();
long fullSize = Double.valueOf(totalCountString).longValue();
PartialCollection<BoxItem.Info> items = new PartialCollection<>(offset, limit, fullSize);
JsonArray entries = responseJSON.get("entries").asArray();
for (JsonValue entry : entries) {
BoxItem.Info entryInfo = (BoxItem.Info) BoxResource.parseInfo(this.getAPI(), entry.asObject());
if (entryInfo != null) {
items.add(entryInfo);
}
}
return items;
}
use of com.eclipsesource.json.JsonArray in project box-java-sdk by box.
the class BoxFileUploadSession method getCommitBody.
/*
* Creates the JSON body for the commit request.
*/
private String getCommitBody(List<BoxFileUploadSessionPart> parts, Map<String, String> attributes) {
JsonObject jsonObject = new JsonObject();
JsonArray array = new JsonArray();
for (BoxFileUploadSessionPart part : parts) {
JsonObject partObj = new JsonObject();
partObj.add("part_id", part.getPartId());
partObj.add("offset", part.getOffset());
partObj.add("size", part.getSize());
array.add(partObj);
}
jsonObject.add("parts", array);
if (attributes != null) {
JsonObject attrObj = new JsonObject();
for (String key : attributes.keySet()) {
attrObj.add(key, attributes.get(key));
}
jsonObject.add("attributes", attrObj);
}
return jsonObject.toString();
}
use of com.eclipsesource.json.JsonArray in project box-java-sdk by box.
the class BoxFileUploadSession method getFile.
/*
* Creates the file isntance from the JSON body of the response.
*/
private BoxFile.Info getFile(BoxJSONResponse response) {
JsonObject jsonObject = Json.parse(response.getJSON()).asObject();
JsonArray array = (JsonArray) jsonObject.get("entries");
JsonObject fileObj = (JsonObject) array.get(0);
BoxFile file = new BoxFile(this.getAPI(), fileObj.get("id").asString());
return file.new Info(fileObj);
}
Aggregations