Search in sources :

Example 41 with Cloudant

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

the class SBApplicationConfig method cloudant.

/**
 * Enables Spring to automatically create <code>Cloudant</code> instances
 * configured to connect to the bound Cloudant client.
 *
 * @return A fully-configured <code>Cloudant</code> instance.
 */
@ConditionalOnMissingBean
@Bean
public Cloudant cloudant() {
    // Create the authenticator.
    Authenticator authenticator = new BasicAuthenticator.Builder().username(this.cloudantUsername).password(this.cloudantPassword).build();
    // Create the client
    Cloudant client = new Cloudant("", authenticator);
    client.setServiceUrl(cloudantUrl);
    return client;
}
Also used : BasicAuthenticator(com.ibm.cloud.sdk.core.security.BasicAuthenticator) Cloudant(com.ibm.cloud.cloudant.v1.Cloudant) BasicAuthenticator(com.ibm.cloud.sdk.core.security.BasicAuthenticator) Authenticator(com.ibm.cloud.sdk.core.security.Authenticator) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 42 with Cloudant

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

the class CloudEventStoreCloudant method addEvent.

@Override
public void addEvent(CloudEvent<?, ?> event) throws Exception {
    // Convert event into document object
    Document document = new Document();
    // https://github.com/cloudant/java-cloudant/blob/master/MIGRATION.md
    document.setProperties(this.gson.fromJson(this.gson.toJson(event), Map.class));
    // Post document and get response
    PostDocumentOptions postDocumentOptions = new PostDocumentOptions.Builder().db(this.dbName).document(document).build();
    DocumentResult response = this.client.postDocument(postDocumentOptions).execute().getResult();
    // Check for errors
    String error = response.getError();
    if (error != null) {
        logger.error("Error adding event to Cloudant: " + error);
        throw new Exception(error);
    }
}
Also used : DocumentResult(com.ibm.cloud.cloudant.v1.model.DocumentResult) Document(com.ibm.cloud.cloudant.v1.model.Document) Map(java.util.Map) PostDocumentOptions(com.ibm.cloud.cloudant.v1.model.PostDocumentOptions) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException)

Example 43 with Cloudant

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

the class PutCloudantSecurityConfigurationOptionsTest method testPutCloudantSecurityConfigurationOptions.

@Test
public void testPutCloudantSecurityConfigurationOptions() throws Throwable {
    SecurityObject securityObjectModel = new SecurityObject.Builder().names(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).roles(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).build();
    assertEquals(securityObjectModel.names(), new java.util.ArrayList<String>(java.util.Arrays.asList("testString")));
    assertEquals(securityObjectModel.roles(), new java.util.ArrayList<String>(java.util.Arrays.asList("testString")));
    PutCloudantSecurityConfigurationOptions putCloudantSecurityConfigurationOptionsModel = new PutCloudantSecurityConfigurationOptions.Builder().db("testString").cloudant(new java.util.HashMap<String, List<String>>() {

        {
            put("foo", new java.util.ArrayList<String>(java.util.Arrays.asList("_reader")));
        }
    }).admins(securityObjectModel).members(securityObjectModel).couchdbAuthOnly(true).build();
    assertEquals(putCloudantSecurityConfigurationOptionsModel.db(), "testString");
    assertEquals(putCloudantSecurityConfigurationOptionsModel.cloudant(), new java.util.HashMap<String, List<String>>() {

        {
            put("foo", new java.util.ArrayList<String>(java.util.Arrays.asList("_reader")));
        }
    });
    assertEquals(putCloudantSecurityConfigurationOptionsModel.admins(), securityObjectModel);
    assertEquals(putCloudantSecurityConfigurationOptionsModel.members(), securityObjectModel);
    assertEquals(putCloudantSecurityConfigurationOptionsModel.couchdbAuthOnly(), Boolean.valueOf(true));
}
Also used : PutCloudantSecurityConfigurationOptions(com.ibm.cloud.cloudant.v1.model.PutCloudantSecurityConfigurationOptions) SecurityObject(com.ibm.cloud.cloudant.v1.model.SecurityObject) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Example 44 with Cloudant

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

the class CreateDbAndDoc method main.

public static void main(String[] args) {
    // 1. Create a client with `CLOUDANT` default service name ============
    Cloudant client = Cloudant.newInstance();
    // 2. Create a database ===============================================
    // Create a database object with "orders" id
    String exampleDbName = "orders";
    PutDatabaseOptions putDbOptions = new PutDatabaseOptions.Builder().db(exampleDbName).build();
    // Try to create database if it doesn't exist
    try {
        Ok putDatabaseResult = client.putDatabase(putDbOptions).execute().getResult();
        if (putDatabaseResult.isOk()) {
            System.out.println("\"" + exampleDbName + "\" database created.");
        }
    } catch (ServiceResponseException sre) {
        if (sre.getStatusCode() == 412)
            System.out.println("Cannot create \"" + exampleDbName + "\" database, it already exists.");
    }
    // 3. Create a document ===============================================
    // Create a document object with "example" id
    String exampleDocId = "example";
    Document exampleDocument = new Document();
    // Setting id for the document is optional when "postDocument" method is used for CREATE.
    // When id is not provided the server will generate one for your document.
    exampleDocument.setId(exampleDocId);
    // Add "name" and "joined" fields to the document
    exampleDocument.put("name", "Bob Smith");
    exampleDocument.put("joined", "2019-01-24T10:42:59.000Z");
    // Save the document in the database with "postDocument" method
    PostDocumentOptions createDocumentOptions = new PostDocumentOptions.Builder().db(exampleDbName).document(exampleDocument).build();
    DocumentResult createDocumentResponse = client.postDocument(createDocumentOptions).execute().getResult();
    // ====================================================================
    // Note: saving the document can also be done with the "putDocument"
    // method. In this case `docId` is required for a CREATE operation:
    /*
        PutDocumentOptions createDocumentOptions =
            new PutDocumentOptions.Builder()
                .db(exampleDbName)
                .docId(exampleDocId)
                .document(exampleDocument)
                .build();
        DocumentResult createDocumentResponse = client
            .putDocument(createDocumentOptions)
            .execute()
            .getResult();
        */
    // ====================================================================
    // Keeping track of the revision number of the document object
    // is necessary for further UPDATE/DELETE operations:
    exampleDocument.setRev(createDocumentResponse.getRev());
    System.out.println("You have created the document:\n" + exampleDocument);
}
Also used : PutDatabaseOptions(com.ibm.cloud.cloudant.v1.model.PutDatabaseOptions) DocumentResult(com.ibm.cloud.cloudant.v1.model.DocumentResult) ServiceResponseException(com.ibm.cloud.sdk.core.service.exception.ServiceResponseException) Cloudant(com.ibm.cloud.cloudant.v1.Cloudant) Ok(com.ibm.cloud.cloudant.v1.model.Ok) Document(com.ibm.cloud.cloudant.v1.model.Document) PostDocumentOptions(com.ibm.cloud.cloudant.v1.model.PostDocumentOptions)

Example 45 with Cloudant

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

the class DeleteDoc method main.

public static void main(String[] args) {
    // 1. Create a client with `CLOUDANT` default service name ============
    Cloudant client = Cloudant.newInstance();
    // 2. Delete the document =============================================
    // Set the options to get the document out of the database if it exists
    String exampleDbName = "orders";
    String exampleDocId = "example";
    GetDocumentOptions documentInfoOptions = new GetDocumentOptions.Builder().db(exampleDbName).docId(exampleDocId).build();
    try {
        // Try to get the document if it previously existed in the database
        Document document = client.getDocument(documentInfoOptions).execute().getResult();
        // Delete the document from the database
        DeleteDocumentOptions deleteDocumentOptions = new DeleteDocumentOptions.Builder().db(exampleDbName).docId(// docId is required for DELETE
        exampleDocId).rev(// rev is required for DELETE
        document.getRev()).build();
        DocumentResult deleteDocumentResponse = client.deleteDocument(deleteDocumentOptions).execute().getResult();
        if (deleteDocumentResponse.isOk()) {
            System.out.println("You have deleted the document.");
        }
    } catch (NotFoundException nfe) {
        System.out.println("Cannot delete document because " + "either \"orders\" database or the \"example\" " + "document was not found.");
    }
}
Also used : DocumentResult(com.ibm.cloud.cloudant.v1.model.DocumentResult) GetDocumentOptions(com.ibm.cloud.cloudant.v1.model.GetDocumentOptions) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) Cloudant(com.ibm.cloud.cloudant.v1.Cloudant) Document(com.ibm.cloud.cloudant.v1.model.Document) DeleteDocumentOptions(com.ibm.cloud.cloudant.v1.model.DeleteDocumentOptions)

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