use of com.ibm.cloud.cloudant.v1.model.GetDocumentOptions in project cloudant-java-sdk by IBM.
the class Cloudant method getDocument.
/**
* Retrieve a document.
*
* Returns document with the specified `doc_id` from the specified database. Unless you request a specific revision,
* the latest revision of the document is always returned.
*
* @param getDocumentOptions the {@link GetDocumentOptions} containing the options for the call
* @return a {@link ServiceCall} with a result of type {@link Document}
*/
public ServiceCall<Document> getDocument(GetDocumentOptions getDocumentOptions) {
com.ibm.cloud.sdk.core.util.Validator.notNull(getDocumentOptions, "getDocumentOptions cannot be null");
Map<String, String> pathParamsMap = new HashMap<String, String>();
pathParamsMap.put("db", getDocumentOptions.db());
pathParamsMap.put("doc_id", getDocumentOptions.docId());
RequestBuilder builder = RequestBuilder.get(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/{db}/{doc_id}", pathParamsMap));
Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("cloudant", "v1", "getDocument");
for (Entry<String, String> header : sdkHeaders.entrySet()) {
builder.header(header.getKey(), header.getValue());
}
builder.header("Accept", "application/json");
if (getDocumentOptions.ifNoneMatch() != null) {
builder.header("If-None-Match", getDocumentOptions.ifNoneMatch());
}
if (getDocumentOptions.attachments() != null) {
builder.query("attachments", String.valueOf(getDocumentOptions.attachments()));
}
if (getDocumentOptions.attEncodingInfo() != null) {
builder.query("att_encoding_info", String.valueOf(getDocumentOptions.attEncodingInfo()));
}
if (getDocumentOptions.conflicts() != null) {
builder.query("conflicts", String.valueOf(getDocumentOptions.conflicts()));
}
if (getDocumentOptions.deletedConflicts() != null) {
builder.query("deleted_conflicts", String.valueOf(getDocumentOptions.deletedConflicts()));
}
if (getDocumentOptions.latest() != null) {
builder.query("latest", String.valueOf(getDocumentOptions.latest()));
}
if (getDocumentOptions.localSeq() != null) {
builder.query("local_seq", String.valueOf(getDocumentOptions.localSeq()));
}
if (getDocumentOptions.meta() != null) {
builder.query("meta", String.valueOf(getDocumentOptions.meta()));
}
if (getDocumentOptions.rev() != null) {
builder.query("rev", String.valueOf(getDocumentOptions.rev()));
}
if (getDocumentOptions.revs() != null) {
builder.query("revs", String.valueOf(getDocumentOptions.revs()));
}
if (getDocumentOptions.revsInfo() != null) {
builder.query("revs_info", String.valueOf(getDocumentOptions.revsInfo()));
}
ResponseConverter<Document> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<Document>() {
}.getType());
return createServiceCall(builder.build(), responseConverter);
}
use of com.ibm.cloud.cloudant.v1.model.GetDocumentOptions in project cloudant-java-sdk by IBM.
the class GetInfoFromExistingDatabase method main.
public static void main(String[] args) {
// 1. Create a Cloudant client with "EXAMPLES" service name ===========
Cloudant client = Cloudant.newInstance("EXAMPLES");
// 2. Get server information ==========================================
ServerInformation serverInformation = client.getServerInformation().execute().getResult();
System.out.println("Server Version: " + serverInformation.getVersion());
// 3. Get database information for "animaldb" =========================
String dbName = "animaldb";
GetDatabaseInformationOptions dbInformationOptions = new GetDatabaseInformationOptions.Builder(dbName).build();
DatabaseInformation dbInformationResponse = client.getDatabaseInformation(dbInformationOptions).execute().getResult();
// 4. Show document count in database =================================
Long documentCount = dbInformationResponse.getDocCount();
System.out.println("Document count in \"" + dbInformationResponse.getDbName() + "\" database is " + documentCount + ".");
// 5. Get zebra document out of the database by document id ===========
GetDocumentOptions getDocOptions = new GetDocumentOptions.Builder().db(dbName).docId("zebra").build();
Document documentAboutZebra = client.getDocument(getDocOptions).execute().getResult();
System.out.println("Document retrieved from database:\n" + documentAboutZebra);
}
use of com.ibm.cloud.cloudant.v1.model.GetDocumentOptions in project cloudant-java-sdk by IBM.
the class UpdateDoc method main.
public static void main(String[] args) {
// 1. Create a client with `CLOUDANT` default service name ============
Cloudant client = Cloudant.newInstance();
// 2. Update the document =============================================
// Set the options to get the document out of the database if it exists
String exampleDbName = "orders";
GetDocumentOptions documentInfoOptions = new GetDocumentOptions.Builder().db(exampleDbName).docId("example").build();
try {
// Try to get the document if it previously existed in the database
Document document = client.getDocument(documentInfoOptions).execute().getResult();
// Note: for response byte stream use:
/*
InputStream documentAsByteStream =
client.getDocumentAsStream(documentInfoOptions)
.execute()
.getResult();
*/
// Add Bob Smith's address to the document
document.put("address", "19 Front Street, Darlington, DL5 1TY");
// Remove the joined property from document object
document.removeProperty("joined");
// Update the document in the database
PostDocumentOptions updateDocumentOptions = new PostDocumentOptions.Builder().db(exampleDbName).document(document).build();
// ================================================================
// Note: for request byte stream use:
/*
PostDocumentOptions updateDocumentOptions =
new PostDocumentOptions.Builder()
.db(exampleDbName)
.contentType("application/json")
.body(documentAsByteStream)
.build();
*/
// ================================================================
DocumentResult updateDocumentResponse = client.postDocument(updateDocumentOptions).execute().getResult();
// ====================================================================
// Note: updating the document can also be done with the "putDocument"
// method. docId and rev are required for an UPDATE operation,
// but rev can be provided in the document object too:
/*
PutDocumentOptions updateDocumentOptions =
new PutDocumentOptions.Builder()
.db(exampleDbName)
.docId(document.getId()) // docId is a required parameter
.rev(document.getRev())
.document(document) // rev in the document object CAN replace above `rev` parameter
.build();
DocumentResult updateDocumentResponse = client
.putDocument(updateDocumentOptions)
.execute()
.getResult();
*/
// ====================================================================
// Keeping track of the latest revision number of the document object
// is necessary for further UPDATE/DELETE operations:
document.setRev(updateDocumentResponse.getRev());
System.out.println("You have updated the document:\n" + document);
} catch (NotFoundException nfe) {
System.out.println("Cannot update document because " + "either \"orders\" database or the \"example\" " + "document was not found.");
}
}
use of com.ibm.cloud.cloudant.v1.model.GetDocumentOptions in project cloudant-java-sdk by IBM.
the class CloudantTest method testGetDocumentAsRelatedWOptions.
@Test
public void testGetDocumentAsRelatedWOptions() throws Throwable {
// Schedule some responses.
String mockResponseBody = "This is a mock binary response.";
String getDocumentAsRelatedPath = "/testString/testString";
server.enqueue(new MockResponse().setHeader("Content-type", "multipart/related").setResponseCode(200).setBody(mockResponseBody));
constructClientService();
// Construct an instance of the GetDocumentOptions model
GetDocumentOptions getDocumentOptionsModel = new GetDocumentOptions.Builder().db("testString").docId("testString").ifNoneMatch("testString").attachments(false).attEncodingInfo(false).conflicts(false).deletedConflicts(false).latest(false).localSeq(false).meta(false).rev("testString").revs(false).revsInfo(false).build();
// Invoke operation with valid options model (positive test)
Response<InputStream> response = cloudantService.getDocumentAsRelated(getDocumentOptionsModel).execute();
assertNotNull(response);
InputStream responseObj = response.getResult();
assertNotNull(responseObj);
// Verify the contents of the request
RecordedRequest request = server.takeRequest();
assertNotNull(request);
assertEquals(request.getMethod(), "GET");
// Check query
Map<String, String> query = TestUtilities.parseQueryString(request);
assertNotNull(query);
// Get query params
assertEquals(Boolean.valueOf(query.get("attachments")), Boolean.valueOf(false));
assertEquals(Boolean.valueOf(query.get("att_encoding_info")), Boolean.valueOf(false));
assertEquals(Boolean.valueOf(query.get("conflicts")), Boolean.valueOf(false));
assertEquals(Boolean.valueOf(query.get("deleted_conflicts")), Boolean.valueOf(false));
assertEquals(Boolean.valueOf(query.get("latest")), Boolean.valueOf(false));
assertEquals(Boolean.valueOf(query.get("local_seq")), Boolean.valueOf(false));
assertEquals(Boolean.valueOf(query.get("meta")), Boolean.valueOf(false));
assertEquals(query.get("rev"), "testString");
assertEquals(Boolean.valueOf(query.get("revs")), Boolean.valueOf(false));
assertEquals(Boolean.valueOf(query.get("revs_info")), Boolean.valueOf(false));
// Check request path
String parsedPath = TestUtilities.parseReqPath(request);
assertEquals(parsedPath, getDocumentAsRelatedPath);
}
use of com.ibm.cloud.cloudant.v1.model.GetDocumentOptions in project cloudant-java-sdk by IBM.
the class CloudantTest method testGetDocumentAsStreamWOptions.
@Test
public void testGetDocumentAsStreamWOptions() throws Throwable {
// Schedule some responses.
String mockResponseBody = "{\"foo\": \"this is a mock response for JSON streaming\"}";
String getDocumentAsStreamPath = "/testString/testString";
server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
constructClientService();
// Construct an instance of the GetDocumentOptions model
GetDocumentOptions getDocumentOptionsModel = new GetDocumentOptions.Builder().db("testString").docId("testString").ifNoneMatch("testString").attachments(false).attEncodingInfo(false).conflicts(false).deletedConflicts(false).latest(false).localSeq(false).meta(false).rev("testString").revs(false).revsInfo(false).build();
// Invoke operation with valid options model (positive test)
Response<InputStream> response = cloudantService.getDocumentAsStream(getDocumentOptionsModel).execute();
assertNotNull(response);
InputStream responseObj = response.getResult();
assertNotNull(responseObj);
// Verify the contents of the request
RecordedRequest request = server.takeRequest();
assertNotNull(request);
assertEquals(request.getMethod(), "GET");
// Check query
Map<String, String> query = TestUtilities.parseQueryString(request);
assertNotNull(query);
// Get query params
assertEquals(Boolean.valueOf(query.get("attachments")), Boolean.valueOf(false));
assertEquals(Boolean.valueOf(query.get("att_encoding_info")), Boolean.valueOf(false));
assertEquals(Boolean.valueOf(query.get("conflicts")), Boolean.valueOf(false));
assertEquals(Boolean.valueOf(query.get("deleted_conflicts")), Boolean.valueOf(false));
assertEquals(Boolean.valueOf(query.get("latest")), Boolean.valueOf(false));
assertEquals(Boolean.valueOf(query.get("local_seq")), Boolean.valueOf(false));
assertEquals(Boolean.valueOf(query.get("meta")), Boolean.valueOf(false));
assertEquals(query.get("rev"), "testString");
assertEquals(Boolean.valueOf(query.get("revs")), Boolean.valueOf(false));
assertEquals(Boolean.valueOf(query.get("revs_info")), Boolean.valueOf(false));
// Check request path
String parsedPath = TestUtilities.parseReqPath(request);
assertEquals(parsedPath, getDocumentAsStreamPath);
// Verify streamed JSON response
java.util.Scanner s = new java.util.Scanner(responseObj).useDelimiter("\\A");
String streamedResponseBody = s.hasNext() ? s.next() : "";
assertEquals(streamedResponseBody, mockResponseBody);
}
Aggregations