use of com.ibm.cloud.sdk.core.http.RequestBuilder in project cloudant-java-sdk by IBM.
the class CouchDbSessionAuthenticator method requestToken.
@Override
public CouchDbSessionToken requestToken() {
JsonObject creds = new JsonObject();
creds.addProperty("username", user);
creds.addProperty("password", pass);
RequestBuilder postSessionbuilder = RequestBuilder.post(sessionUrl).bodyJson(creds);
// Apply custom headers if necessary
if (headers != null) {
for (String key : headers.names()) {
postSessionbuilder.header(key, headers.get(key));
}
}
try {
// Build the client if we need to
if (client == null) {
client = HttpClientSingleton.getInstance().configureClient(options).newBuilder().cookieJar(cookieJar).build();
}
try (Response response = client.newCall(postSessionbuilder.build()).execute()) {
if (response.isSuccessful()) {
List<Cookie> cookies = Cookie.parseAll(sessionUrl, response.headers());
for (Cookie cookie : cookies) {
if ("AuthSession".equals(cookie.name())) {
return new CouchDbSessionToken(cookie.expiresAt());
}
}
}
throw new ServiceResponseException(response.code(), response);
}
} catch (RuntimeException | IOException t) {
return new CouchDbSessionToken(t);
}
}
use of com.ibm.cloud.sdk.core.http.RequestBuilder in project cloudant-java-sdk by IBM.
the class Cloudant method getDocumentShardsInfo.
/**
* Retrieve shard information for a specific document.
*
* Retrieves information about a specific shard where a particular document is stored, along with information about
* the nodes where that shard has a replica.
*
* @param getDocumentShardsInfoOptions the {@link GetDocumentShardsInfoOptions} containing the options for the call
* @return a {@link ServiceCall} with a result of type {@link DocumentShardInfo}
*/
public ServiceCall<DocumentShardInfo> getDocumentShardsInfo(GetDocumentShardsInfoOptions getDocumentShardsInfoOptions) {
com.ibm.cloud.sdk.core.util.Validator.notNull(getDocumentShardsInfoOptions, "getDocumentShardsInfoOptions cannot be null");
Map<String, String> pathParamsMap = new HashMap<String, String>();
pathParamsMap.put("db", getDocumentShardsInfoOptions.db());
pathParamsMap.put("doc_id", getDocumentShardsInfoOptions.docId());
RequestBuilder builder = RequestBuilder.get(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/{db}/_shards/{doc_id}", pathParamsMap));
Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("cloudant", "v1", "getDocumentShardsInfo");
for (Entry<String, String> header : sdkHeaders.entrySet()) {
builder.header(header.getKey(), header.getValue());
}
builder.header("Accept", "application/json");
ResponseConverter<DocumentShardInfo> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<DocumentShardInfo>() {
}.getType());
return createServiceCall(builder.build(), responseConverter);
}
use of com.ibm.cloud.sdk.core.http.RequestBuilder in project cloudant-java-sdk by IBM.
the class Cloudant method postSearchAnalyze.
/**
* Query tokenization of sample text.
*
* Returns the results of analyzer tokenization of the provided sample text. This endpoint can be used for testing
* analyzer tokenization.
*
* @param postSearchAnalyzeOptions the {@link PostSearchAnalyzeOptions} containing the options for the call
* @return a {@link ServiceCall} with a result of type {@link SearchAnalyzeResult}
*/
public ServiceCall<SearchAnalyzeResult> postSearchAnalyze(PostSearchAnalyzeOptions postSearchAnalyzeOptions) {
com.ibm.cloud.sdk.core.util.Validator.notNull(postSearchAnalyzeOptions, "postSearchAnalyzeOptions cannot be null");
RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/_search_analyze"));
Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("cloudant", "v1", "postSearchAnalyze");
for (Entry<String, String> header : sdkHeaders.entrySet()) {
builder.header(header.getKey(), header.getValue());
}
builder.header("Accept", "application/json");
final JsonObject contentJson = new JsonObject();
contentJson.addProperty("analyzer", postSearchAnalyzeOptions.analyzer());
contentJson.addProperty("text", postSearchAnalyzeOptions.text());
builder.bodyJson(contentJson);
ResponseConverter<SearchAnalyzeResult> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<SearchAnalyzeResult>() {
}.getType());
return createServiceCall(builder.build(), responseConverter);
}
use of com.ibm.cloud.sdk.core.http.RequestBuilder in project cloudant-java-sdk by IBM.
the class Cloudant method headDatabase.
/**
* Retrieve the HTTP headers for a database.
*
* Returns the HTTP headers that contain a minimal amount of information about the specified database. Since the
* response body is empty, using the HEAD method is a lightweight way to check if the database exists or not.
*
* @param headDatabaseOptions the {@link HeadDatabaseOptions} containing the options for the call
* @return a {@link ServiceCall} with a void result
*/
public ServiceCall<Void> headDatabase(HeadDatabaseOptions headDatabaseOptions) {
com.ibm.cloud.sdk.core.util.Validator.notNull(headDatabaseOptions, "headDatabaseOptions cannot be null");
Map<String, String> pathParamsMap = new HashMap<String, String>();
pathParamsMap.put("db", headDatabaseOptions.db());
RequestBuilder builder = RequestBuilder.head(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/{db}", pathParamsMap));
Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("cloudant", "v1", "headDatabase");
for (Entry<String, String> header : sdkHeaders.entrySet()) {
builder.header(header.getKey(), header.getValue());
}
ResponseConverter<Void> responseConverter = ResponseConverterUtils.getVoid();
return createServiceCall(builder.build(), responseConverter);
}
use of com.ibm.cloud.sdk.core.http.RequestBuilder in project cloudant-java-sdk by IBM.
the class Cloudant method getGeo.
/**
* Query a geospatial index.
*
* Executes a query against the requested geospatial index from the specified design document.
*
* @param getGeoOptions the {@link GetGeoOptions} containing the options for the call
* @return a {@link ServiceCall} with a result of type {@link GeoResult}
*/
public ServiceCall<GeoResult> getGeo(GetGeoOptions getGeoOptions) {
com.ibm.cloud.sdk.core.util.Validator.notNull(getGeoOptions, "getGeoOptions cannot be null");
Map<String, String> pathParamsMap = new HashMap<String, String>();
pathParamsMap.put("db", getGeoOptions.db());
pathParamsMap.put("ddoc", getGeoOptions.ddoc());
pathParamsMap.put("index", getGeoOptions.index());
RequestBuilder builder = RequestBuilder.get(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/{db}/_design/{ddoc}/_geo/{index}", pathParamsMap));
Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("cloudant", "v1", "getGeo");
for (Entry<String, String> header : sdkHeaders.entrySet()) {
builder.header(header.getKey(), header.getValue());
}
builder.header("Accept", "application/json");
if (getGeoOptions.bbox() != null) {
builder.query("bbox", String.valueOf(getGeoOptions.bbox()));
}
if (getGeoOptions.bookmark() != null) {
builder.query("bookmark", String.valueOf(getGeoOptions.bookmark()));
}
if (getGeoOptions.format() != null) {
builder.query("format", String.valueOf(getGeoOptions.format()));
}
if (getGeoOptions.g() != null) {
builder.query("g", String.valueOf(getGeoOptions.g()));
}
if (getGeoOptions.includeDocs() != null) {
builder.query("include_docs", String.valueOf(getGeoOptions.includeDocs()));
}
if (getGeoOptions.lat() != null) {
builder.query("lat", String.valueOf(getGeoOptions.lat()));
}
if (getGeoOptions.limit() != null) {
builder.query("limit", String.valueOf(getGeoOptions.limit()));
}
if (getGeoOptions.lon() != null) {
builder.query("lon", String.valueOf(getGeoOptions.lon()));
}
if (getGeoOptions.nearest() != null) {
builder.query("nearest", String.valueOf(getGeoOptions.nearest()));
}
if (getGeoOptions.radius() != null) {
builder.query("radius", String.valueOf(getGeoOptions.radius()));
}
if (getGeoOptions.rangex() != null) {
builder.query("rangex", String.valueOf(getGeoOptions.rangex()));
}
if (getGeoOptions.rangey() != null) {
builder.query("rangey", String.valueOf(getGeoOptions.rangey()));
}
if (getGeoOptions.relation() != null) {
builder.query("relation", String.valueOf(getGeoOptions.relation()));
}
if (getGeoOptions.skip() != null) {
builder.query("skip", String.valueOf(getGeoOptions.skip()));
}
if (getGeoOptions.stale() != null) {
builder.query("stale", String.valueOf(getGeoOptions.stale()));
}
ResponseConverter<GeoResult> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<GeoResult>() {
}.getType());
return createServiceCall(builder.build(), responseConverter);
}
Aggregations