use of com.ibm.cloud.cloudant.v1.model.PutDatabaseOptions in project cloudant-java-sdk by IBM.
the class Cloudant method putDatabase.
/**
* Create a database.
*
* @param putDatabaseOptions the {@link PutDatabaseOptions} containing the options for the call
* @return a {@link ServiceCall} with a result of type {@link Ok}
*/
public ServiceCall<Ok> putDatabase(PutDatabaseOptions putDatabaseOptions) {
com.ibm.cloud.sdk.core.util.Validator.notNull(putDatabaseOptions, "putDatabaseOptions cannot be null");
Map<String, String> pathParamsMap = new HashMap<String, String>();
pathParamsMap.put("db", putDatabaseOptions.db());
RequestBuilder builder = RequestBuilder.put(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/{db}", pathParamsMap));
Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("cloudant", "v1", "putDatabase");
for (Entry<String, String> header : sdkHeaders.entrySet()) {
builder.header(header.getKey(), header.getValue());
}
builder.header("Accept", "application/json");
if (putDatabaseOptions.partitioned() != null) {
builder.query("partitioned", String.valueOf(putDatabaseOptions.partitioned()));
}
if (putDatabaseOptions.q() != null) {
builder.query("q", String.valueOf(putDatabaseOptions.q()));
}
ResponseConverter<Ok> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<Ok>() {
}.getType());
return createServiceCall(builder.build(), responseConverter);
}
use of com.ibm.cloud.cloudant.v1.model.PutDatabaseOptions in project cloudant-java-sdk by IBM.
the class PutDatabaseOptionsTest method testPutDatabaseOptions.
@Test
public void testPutDatabaseOptions() throws Throwable {
PutDatabaseOptions putDatabaseOptionsModel = new PutDatabaseOptions.Builder().db("testString").partitioned(false).q(Long.valueOf("26")).build();
assertEquals(putDatabaseOptionsModel.db(), "testString");
assertEquals(putDatabaseOptionsModel.partitioned(), Boolean.valueOf(false));
assertEquals(putDatabaseOptionsModel.q(), Long.valueOf("26"));
}
use of com.ibm.cloud.cloudant.v1.model.PutDatabaseOptions 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);
}
use of com.ibm.cloud.cloudant.v1.model.PutDatabaseOptions in project cloudant-java-sdk by IBM.
the class CloudantTest method testPutDatabaseWOptions.
@Test
public void testPutDatabaseWOptions() throws Throwable {
// Schedule some responses.
String mockResponseBody = "{\"ok\": true}";
String putDatabasePath = "/testString";
server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(201).setBody(mockResponseBody));
constructClientService();
// Construct an instance of the PutDatabaseOptions model
PutDatabaseOptions putDatabaseOptionsModel = new PutDatabaseOptions.Builder().db("testString").partitioned(false).q(Long.valueOf("26")).build();
// Invoke operation with valid options model (positive test)
Response<Ok> response = cloudantService.putDatabase(putDatabaseOptionsModel).execute();
assertNotNull(response);
Ok responseObj = response.getResult();
assertNotNull(responseObj);
// Verify the contents of the request
RecordedRequest request = server.takeRequest();
assertNotNull(request);
assertEquals(request.getMethod(), "PUT");
// Check query
Map<String, String> query = TestUtilities.parseQueryString(request);
assertNotNull(query);
// Get query params
assertEquals(Boolean.valueOf(query.get("partitioned")), Boolean.valueOf(false));
assertEquals(Long.valueOf(query.get("q")), Long.valueOf("26"));
// Check request path
String parsedPath = TestUtilities.parseReqPath(request);
assertEquals(parsedPath, putDatabasePath);
}
Aggregations