Search in sources :

Example 36 with Cloudant

use of com.ibm.cloud.cloudant.v1.Cloudant in project cloudant-java-sdk by IBM.

the class Cloudant method getDesignDocument.

/**
 * Retrieve a design document.
 *
 * Returns design document with the specified `doc_id` from the specified database. Unless you request a specific
 * revision, the current revision of the design document is always returned.
 *
 * @param getDesignDocumentOptions the {@link GetDesignDocumentOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link DesignDocument}
 */
public ServiceCall<DesignDocument> getDesignDocument(GetDesignDocumentOptions getDesignDocumentOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(getDesignDocumentOptions, "getDesignDocumentOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("db", getDesignDocumentOptions.db());
    pathParamsMap.put("ddoc", getDesignDocumentOptions.ddoc());
    RequestBuilder builder = RequestBuilder.get(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/{db}/_design/{ddoc}", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("cloudant", "v1", "getDesignDocument");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    if (getDesignDocumentOptions.ifNoneMatch() != null) {
        builder.header("If-None-Match", getDesignDocumentOptions.ifNoneMatch());
    }
    if (getDesignDocumentOptions.attachments() != null) {
        builder.query("attachments", String.valueOf(getDesignDocumentOptions.attachments()));
    }
    if (getDesignDocumentOptions.attEncodingInfo() != null) {
        builder.query("att_encoding_info", String.valueOf(getDesignDocumentOptions.attEncodingInfo()));
    }
    if (getDesignDocumentOptions.conflicts() != null) {
        builder.query("conflicts", String.valueOf(getDesignDocumentOptions.conflicts()));
    }
    if (getDesignDocumentOptions.deletedConflicts() != null) {
        builder.query("deleted_conflicts", String.valueOf(getDesignDocumentOptions.deletedConflicts()));
    }
    if (getDesignDocumentOptions.latest() != null) {
        builder.query("latest", String.valueOf(getDesignDocumentOptions.latest()));
    }
    if (getDesignDocumentOptions.localSeq() != null) {
        builder.query("local_seq", String.valueOf(getDesignDocumentOptions.localSeq()));
    }
    if (getDesignDocumentOptions.meta() != null) {
        builder.query("meta", String.valueOf(getDesignDocumentOptions.meta()));
    }
    if (getDesignDocumentOptions.rev() != null) {
        builder.query("rev", String.valueOf(getDesignDocumentOptions.rev()));
    }
    if (getDesignDocumentOptions.revs() != null) {
        builder.query("revs", String.valueOf(getDesignDocumentOptions.revs()));
    }
    if (getDesignDocumentOptions.revsInfo() != null) {
        builder.query("revs_info", String.valueOf(getDesignDocumentOptions.revsInfo()));
    }
    ResponseConverter<DesignDocument> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<DesignDocument>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) DesignDocument(com.ibm.cloud.cloudant.v1.model.DesignDocument)

Example 37 with Cloudant

use of com.ibm.cloud.cloudant.v1.Cloudant in project cloudant-java-sdk by IBM.

the class Cloudant method postAllDocsQueries.

/**
 * Multi-query the list of all documents in a database.
 *
 * Runs multiple queries using the primary index (all document IDs). Returns a JSON object that contains a list of
 * result objects, one for each query, with a structure equivalent to that of a single `_all_docs` request. This
 * enables you to request multiple queries in a single request, in place of multiple `POST /{db}/_all_docs` requests.
 *
 * @param postAllDocsQueriesOptions the {@link PostAllDocsQueriesOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link AllDocsQueriesResult}
 */
public ServiceCall<AllDocsQueriesResult> postAllDocsQueries(PostAllDocsQueriesOptions postAllDocsQueriesOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(postAllDocsQueriesOptions, "postAllDocsQueriesOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("db", postAllDocsQueriesOptions.db());
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/{db}/_all_docs/queries", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("cloudant", "v1", "postAllDocsQueries");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    final JsonObject contentJson = new JsonObject();
    contentJson.add("queries", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(postAllDocsQueriesOptions.queries()));
    builder.bodyJson(contentJson);
    ResponseConverter<AllDocsQueriesResult> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<AllDocsQueriesResult>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) JsonObject(com.google.gson.JsonObject) AllDocsQueriesResult(com.ibm.cloud.cloudant.v1.model.AllDocsQueriesResult)

Example 38 with Cloudant

use of com.ibm.cloud.cloudant.v1.Cloudant in project knative-eventing-java-app by IBM.

the class CloudEventStoreCloudant method getEvents.

@Override
public List<CloudEvent<?, ?>> getEvents() {
    try {
        List<CloudEvent<?, ?>> events = new ArrayList<>();
        PostAllDocsOptions docsOptions = new PostAllDocsOptions.Builder().db(this.dbName).includeDocs(true).build();
        AllDocsResult allDocResults = this.client.postAllDocs(docsOptions).execute().getResult();
        for (DocsResultRow docResult : allDocResults.getRows()) {
            Document document = docResult.getDoc();
            @SuppressWarnings("rawtypes") CloudEventImpl evt = this.gson.fromJson(document.toString(), CloudEventImpl.class);
            events.add(evt);
        }
        return events;
    } catch (NotFoundException e) {
        logger.warn("Unable to retrieve all documents from Cloudant", e);
        return Collections.emptyList();
    }
}
Also used : DocsResultRow(com.ibm.cloud.cloudant.v1.model.DocsResultRow) ArrayList(java.util.ArrayList) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) AllDocsResult(com.ibm.cloud.cloudant.v1.model.AllDocsResult) PostAllDocsOptions(com.ibm.cloud.cloudant.v1.model.PostAllDocsOptions) Document(com.ibm.cloud.cloudant.v1.model.Document) CloudEvent(io.cloudevents.CloudEvent) CloudEventImpl(io.cloudevents.v02.CloudEventImpl)

Example 39 with Cloudant

use of com.ibm.cloud.cloudant.v1.Cloudant in project knative-eventing-java-app by IBM.

the class CloudEventStoreCloudant method removeAllEvents.

@Override
public void removeAllEvents() throws Exception {
    try {
        PostAllDocsOptions docsOptions = new PostAllDocsOptions.Builder().db(this.dbName).includeDocs(true).build();
        AllDocsResult allDocResults = this.client.postAllDocs(docsOptions).execute().getResult();
        for (DocsResultRow docResult : allDocResults.getRows()) {
            Document document = docResult.getDoc();
            DeleteDocumentOptions deleteDocumentOptions = new DeleteDocumentOptions.Builder().db(this.dbName).docId(document.getId()).rev(document.getRev()).build();
            DocumentResult deleteDocumentResponse = client.deleteDocument(deleteDocumentOptions).execute().getResult();
            if (!deleteDocumentResponse.isOk()) {
                logger.info("Could not delete a document.");
            }
        }
    } catch (NotFoundException e) {
        String errMsg = "Unable to retrieve all documents from Cloudant";
        logger.error(errMsg, e);
        throw new Exception(errMsg, e);
    }
}
Also used : DocsResultRow(com.ibm.cloud.cloudant.v1.model.DocsResultRow) DocumentResult(com.ibm.cloud.cloudant.v1.model.DocumentResult) GsonBuilder(com.google.gson.GsonBuilder) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) AllDocsResult(com.ibm.cloud.cloudant.v1.model.AllDocsResult) PostAllDocsOptions(com.ibm.cloud.cloudant.v1.model.PostAllDocsOptions) Document(com.ibm.cloud.cloudant.v1.model.Document) DeleteDocumentOptions(com.ibm.cloud.cloudant.v1.model.DeleteDocumentOptions) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException)

Example 40 with Cloudant

use of com.ibm.cloud.cloudant.v1.Cloudant in project knative-eventing-java-app by IBM.

the class CloudEventStoreCloudant method getNumEvents.

@Override
public long getNumEvents() {
    try {
        PostAllDocsOptions docsOptions = new PostAllDocsOptions.Builder().db(this.dbName).build();
        AllDocsResult allDocResults = this.client.postAllDocs(docsOptions).execute().getResult();
        return allDocResults.getTotalRows();
    } catch (Exception e) {
        logger.warn("Unable to retrieve all documents from Cloudant", e);
        return -1;
    }
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) AllDocsResult(com.ibm.cloud.cloudant.v1.model.AllDocsResult) PostAllDocsOptions(com.ibm.cloud.cloudant.v1.model.PostAllDocsOptions) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException)

Aggregations

RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)70 HashMap (java.util.HashMap)51 JsonObject (com.google.gson.JsonObject)23 DocumentResult (com.ibm.cloud.cloudant.v1.model.DocumentResult)17 Ok (com.ibm.cloud.cloudant.v1.model.Ok)11 Document (com.ibm.cloud.cloudant.v1.model.Document)9 List (java.util.List)8 AllDocsResult (com.ibm.cloud.cloudant.v1.model.AllDocsResult)6 NotFoundException (com.ibm.cloud.sdk.core.service.exception.NotFoundException)6 ArrayList (java.util.ArrayList)6 Cloudant (com.ibm.cloud.cloudant.v1.Cloudant)5 SecurityObject (com.ibm.cloud.cloudant.v1.model.SecurityObject)5 Test (org.testng.annotations.Test)5 DesignDocument (com.ibm.cloud.cloudant.v1.model.DesignDocument)3 GetDocumentOptions (com.ibm.cloud.cloudant.v1.model.GetDocumentOptions)3 PostAllDocsOptions (com.ibm.cloud.cloudant.v1.model.PostAllDocsOptions)3 PostDocumentOptions (com.ibm.cloud.cloudant.v1.model.PostDocumentOptions)3 ReplicationDocument (com.ibm.cloud.cloudant.v1.model.ReplicationDocument)3 MockResponse (okhttp3.mockwebserver.MockResponse)3 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)3