Search in sources :

Example 41 with RequestBuilder

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);
    }
}
Also used : TokenServerResponse(com.ibm.cloud.sdk.core.security.TokenServerResponse) Response(okhttp3.Response) Cookie(okhttp3.Cookie) RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) ServiceResponseException(com.ibm.cloud.sdk.core.service.exception.ServiceResponseException) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException)

Example 42 with RequestBuilder

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);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) DocumentShardInfo(com.ibm.cloud.cloudant.v1.model.DocumentShardInfo)

Example 43 with RequestBuilder

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);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) JsonObject(com.google.gson.JsonObject) SearchAnalyzeResult(com.ibm.cloud.cloudant.v1.model.SearchAnalyzeResult)

Example 44 with RequestBuilder

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);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap)

Example 45 with RequestBuilder

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);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) GeoResult(com.ibm.cloud.cloudant.v1.model.GeoResult)

Aggregations

RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)422 HashMap (java.util.HashMap)324 JsonObject (com.google.gson.JsonObject)111 MultipartBody (okhttp3.MultipartBody)30 InputStream (java.io.InputStream)27 DocumentResult (com.ibm.cloud.cloudant.v1.model.DocumentResult)12 Ok (com.ibm.cloud.cloudant.v1.model.Ok)8 MetricResponse (com.ibm.watson.discovery.v1.model.MetricResponse)4 DeleteModelResults (com.ibm.watson.natural_language_understanding.v1.model.DeleteModelResults)4 Collection (com.ibm.watson.visual_recognition.v4.model.Collection)4 DesignDocument (com.ibm.cloud.cloudant.v1.model.DesignDocument)3 ReplicationDocument (com.ibm.cloud.cloudant.v1.model.ReplicationDocument)3 TagsSubscriptionList (com.ibm.cloud.eventnotifications.event_notifications.v1.model.TagsSubscriptionList)3 Counterexample (com.ibm.watson.assistant.v1.model.Counterexample)3 DialogNode (com.ibm.watson.assistant.v1.model.DialogNode)3 Entity (com.ibm.watson.assistant.v1.model.Entity)3 Intent (com.ibm.watson.assistant.v1.model.Intent)3 Value (com.ibm.watson.assistant.v1.model.Value)3 DeleteCredentials (com.ibm.watson.discovery.v1.model.DeleteCredentials)3 TokenDictStatusResponse (com.ibm.watson.discovery.v1.model.TokenDictStatusResponse)3