com.linkedin.restli.client.BatchGetEntityRequestBuilder Maven / Gradle / Ivy
/*
Copyright (c) 2014 LinkedIn Corp.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.linkedin.restli.client;
import com.linkedin.data.schema.PathSpec;
import com.linkedin.data.template.RecordTemplate;
import com.linkedin.restli.client.response.BatchKVResponse;
import com.linkedin.restli.common.EntityResponse;
import com.linkedin.restli.common.ResourceSpec;
import com.linkedin.restli.common.TypeSpec;
import com.linkedin.restli.internal.client.BatchEntityResponseDecoder;
import com.linkedin.restli.internal.client.RestResponseDecoder;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/**
* Builds a Request for a batch of resources. Both key and entity classes are specified.
*
* @param resource key class
* @param entity template class
*
* @author Keren Jin
*/
public class BatchGetEntityRequestBuilder extends
BatchKVRequestBuilder>
{
private final RestResponseDecoder>> _decoder;
/**
* Batches multiple requests into a single batch request.
*
* Requirements:
*
* - Base URIs of all requests must be the same
*
*
* Batching is performed along two dimensions:
*
* - entity ids are accumulated
* - fields are accumulated. If any of the batched requests has a null projection,
* then the resulting batch request will also have a null projection
*
*
* @param requests to batch
* @param type of entity template
* @return batching request
*/
public static BatchGetEntityRequest batch(List> requests)
{
return batch(requests, true);
}
/**
* Batches multiple requests into a single batch request.
*
* Requirements:
*
* - Base URIs of all requests must be the same
* - if {@code batchFields} is {@code false}, then all requests must have the same projection
*
*
* Batching is performed along one or two dimensions:
*
* - entity ids are always accumulated
* - if {@code batchFields} is {@code true}, fields are also accumulated. If any of the batched requests has a
* null projection, then the resulting batch request will also have a null projection.
*
*
*
*
* @param requests to batch
* @param batchFields whether field batching is desired
* @param type of entity template
* @return batching request
*/
public static BatchGetEntityRequest batch(List> requests,
boolean batchFields)
{
final BatchGetEntityRequest firstRequest = requests.get(0);
@SuppressWarnings("deprecation")
final ResourceSpec firstResourceSpec = firstRequest.getResourceSpec();
final Map batchQueryParams =
getReadOnlyQueryParameters(BatchGetRequestUtil.getBatchQueryParam(requests, batchFields));
return new BatchGetEntityRequest(firstRequest.getHeaders(),
firstRequest.getCookies(),
firstRequest.getResponseDecoder(),
batchQueryParams,
firstRequest.getQueryParamClasses(),
firstResourceSpec,
firstRequest.getBaseUriTemplate(),
firstRequest.getPathKeys(),
firstRequest.getRequestOptions());
}
public BatchGetEntityRequestBuilder(String baseUriTemplate,
RestResponseDecoder>> decoder,
ResourceSpec resourceSpec,
RestliRequestOptions requestOptions)
{
super(baseUriTemplate, resourceSpec, requestOptions);
_decoder = decoder;
}
@SuppressWarnings("unchecked")
public BatchGetEntityRequestBuilder(String baseUriTemplate,
ResourceSpec resourceSpec,
RestliRequestOptions requestOptions)
{
this(baseUriTemplate,
new BatchEntityResponseDecoder(
(TypeSpec) resourceSpec.getValueType(),
(TypeSpec) resourceSpec.getKeyType(),
resourceSpec.getKeyParts(),
resourceSpec.getComplexKeyType()),
resourceSpec,
requestOptions);
}
@SuppressWarnings("unchecked")
public BatchGetEntityRequestBuilder ids(K... ids)
{
return ids(Arrays.asList(ids));
}
public BatchGetEntityRequestBuilder ids(Collection ids)
{
addKeys(ids);
return this;
}
@Override
public BatchGetEntityRequestBuilder setParam(String key, Object value)
{
super.setParam(key, value);
return this;
}
@Override
public BatchGetEntityRequestBuilder setReqParam(String key, Object value)
{
super.setReqParam(key, value);
return this;
}
@Override
public BatchGetEntityRequestBuilder addParam(String key, Object value)
{
super.addParam(key, value);
return this;
}
@Override
public BatchGetEntityRequestBuilder addReqParam(String key, Object value)
{
super.addReqParam(key, value);
return this;
}
@Override
public BatchGetEntityRequestBuilder setHeader(String key, String value)
{
super.setHeader(key, value);
return this;
}
@Override
public BatchGetEntityRequestBuilder setHeaders(Map headers)
{
super.setHeaders(headers);
return this;
}
@Override
public BatchGetEntityRequestBuilder addHeader(String name, String value)
{
super.addHeader(name, value);
return this;
}
@Override
public BatchGetEntityRequestBuilder pathKey(String name, Object value)
{
super.pathKey(name, value);
return this;
}
/**
* Builds a GET request for this resource batch.
*
* @return a read request for the resource batch
*/
@Override
public BatchGetEntityRequest build()
{
ensureBatchKeys();
return new BatchGetEntityRequest(buildReadOnlyHeaders(),
buildReadOnlyCookies(),
_decoder,
buildReadOnlyQueryParameters(),
getQueryParamClasses(),
_resourceSpec,
getBaseUriTemplate(),
buildReadOnlyPathKeys(),
getRequestOptions());
}
public BatchGetEntityRequestBuilder fields(PathSpec... fieldPaths)
{
addFields(fieldPaths);
return this;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy