Search in sources :

Example 6 with ServiceResponseException

use of com.ibm.cloud.sdk.core.service.exception.ServiceResponseException 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)

Aggregations

ServiceResponseException (com.ibm.cloud.sdk.core.service.exception.ServiceResponseException)6 Test (org.testng.annotations.Test)3 TestCloudantService (com.ibm.cloud.cloudant.internal.TestCloudantService)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 MockResponse (okhttp3.mockwebserver.MockResponse)2 MockWebServer (okhttp3.mockwebserver.MockWebServer)2 JsonObject (com.google.gson.JsonObject)1 Cloudant (com.ibm.cloud.cloudant.v1.Cloudant)1 Document (com.ibm.cloud.cloudant.v1.model.Document)1 DocumentResult (com.ibm.cloud.cloudant.v1.model.DocumentResult)1 Ok (com.ibm.cloud.cloudant.v1.model.Ok)1 PostDocumentOptions (com.ibm.cloud.cloudant.v1.model.PostDocumentOptions)1 PutDatabaseOptions (com.ibm.cloud.cloudant.v1.model.PutDatabaseOptions)1 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)1 TokenServerResponse (com.ibm.cloud.sdk.core.security.TokenServerResponse)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1