use of com.ibm.cloud.cloudant.v1.model.DeleteDocumentOptions in project cloudant-java-sdk by IBM.
the class Cloudant method deleteDocument.
/**
* Delete a document.
*
* Marks the specified document as deleted by adding a `_deleted` field with the value `true`. Documents with this
* field are not returned within requests anymore but stay in the database. You must supply the current (latest)
* revision, either by using the `rev` parameter or by using the `If-Match` header to specify the revision.
*
* @param deleteDocumentOptions the {@link DeleteDocumentOptions} containing the options for the call
* @return a {@link ServiceCall} with a result of type {@link DocumentResult}
*/
public ServiceCall<DocumentResult> deleteDocument(DeleteDocumentOptions deleteDocumentOptions) {
com.ibm.cloud.sdk.core.util.Validator.notNull(deleteDocumentOptions, "deleteDocumentOptions cannot be null");
Map<String, String> pathParamsMap = new HashMap<String, String>();
pathParamsMap.put("db", deleteDocumentOptions.db());
pathParamsMap.put("doc_id", deleteDocumentOptions.docId());
RequestBuilder builder = RequestBuilder.delete(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/{db}/{doc_id}", pathParamsMap));
Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("cloudant", "v1", "deleteDocument");
for (Entry<String, String> header : sdkHeaders.entrySet()) {
builder.header(header.getKey(), header.getValue());
}
builder.header("Accept", "application/json");
if (deleteDocumentOptions.ifMatch() != null) {
builder.header("If-Match", deleteDocumentOptions.ifMatch());
}
if (deleteDocumentOptions.batch() != null) {
builder.query("batch", String.valueOf(deleteDocumentOptions.batch()));
}
if (deleteDocumentOptions.rev() != null) {
builder.query("rev", String.valueOf(deleteDocumentOptions.rev()));
}
ResponseConverter<DocumentResult> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<DocumentResult>() {
}.getType());
return createServiceCall(builder.build(), responseConverter);
}
use of com.ibm.cloud.cloudant.v1.model.DeleteDocumentOptions in project cloudant-java-sdk by IBM.
the class CloudantTest method testDeleteDocumentWOptions.
@Test
public void testDeleteDocumentWOptions() throws Throwable {
// Schedule some responses.
String mockResponseBody = "{\"id\": \"id\", \"rev\": \"rev\", \"ok\": true, \"caused_by\": \"causedBy\", \"error\": \"error\", \"reason\": \"reason\"}";
String deleteDocumentPath = "/testString/testString";
server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
constructClientService();
// Construct an instance of the DeleteDocumentOptions model
DeleteDocumentOptions deleteDocumentOptionsModel = new DeleteDocumentOptions.Builder().db("testString").docId("testString").ifMatch("testString").batch("ok").rev("testString").build();
// Invoke operation with valid options model (positive test)
Response<DocumentResult> response = cloudantService.deleteDocument(deleteDocumentOptionsModel).execute();
assertNotNull(response);
DocumentResult responseObj = response.getResult();
assertNotNull(responseObj);
// Verify the contents of the request
RecordedRequest request = server.takeRequest();
assertNotNull(request);
assertEquals(request.getMethod(), "DELETE");
// Check query
Map<String, String> query = TestUtilities.parseQueryString(request);
assertNotNull(query);
// Get query params
assertEquals(query.get("batch"), "ok");
assertEquals(query.get("rev"), "testString");
// Check request path
String parsedPath = TestUtilities.parseReqPath(request);
assertEquals(parsedPath, deleteDocumentPath);
}
use of com.ibm.cloud.cloudant.v1.model.DeleteDocumentOptions in project cloudant-java-sdk by IBM.
the class DeleteDocumentOptionsTest method testDeleteDocumentOptions.
@Test
public void testDeleteDocumentOptions() throws Throwable {
DeleteDocumentOptions deleteDocumentOptionsModel = new DeleteDocumentOptions.Builder().db("testString").docId("testString").ifMatch("testString").batch("ok").rev("testString").build();
assertEquals(deleteDocumentOptionsModel.db(), "testString");
assertEquals(deleteDocumentOptionsModel.docId(), "testString");
assertEquals(deleteDocumentOptionsModel.ifMatch(), "testString");
assertEquals(deleteDocumentOptionsModel.batch(), "ok");
assertEquals(deleteDocumentOptionsModel.rev(), "testString");
}
use of com.ibm.cloud.cloudant.v1.model.DeleteDocumentOptions in project java-sdk by watson-developer-cloud.
the class DiscoveryServiceTest method deleteDocumentIsSuccessful.
@Test
public void deleteDocumentIsSuccessful() throws InterruptedException {
server.enqueue(jsonResponse(deleteDocResp));
DeleteDocumentOptions deleteRequest = new DeleteDocumentOptions.Builder(environmentId, collectionId, documentId).build();
discoveryService.deleteDocument(deleteRequest).execute();
RecordedRequest request = server.takeRequest();
assertEquals(DOCS2_PATH, request.getPath());
assertEquals(DELETE, request.getMethod());
}
use of com.ibm.cloud.cloudant.v1.model.DeleteDocumentOptions in project java-sdk by watson-developer-cloud.
the class DiscoveryV2Example method main.
public static void main(String[] args) throws IOException {
Authenticator authenticator = new BearerTokenAuthenticator("{bearer_token}");
Discovery service = new Discovery("2019-11-22", authenticator);
service.setServiceUrl("{url}");
// This example assumes you have a project and collection set up which can accept documents.
// Paste those
// IDs below.
String projectId = "";
String collectionId = "";
// Add a new document to our collection. Fill in the file path with the file you want to send.
InputStream file = new FileInputStream("");
AddDocumentOptions addDocumentOptions = new AddDocumentOptions.Builder().projectId(projectId).collectionId(collectionId).file(file).filename("example-file").build();
DocumentAccepted addResponse = service.addDocument(addDocumentOptions).execute().getResult();
String documentId = addResponse.getDocumentId();
// Query your collection with the new document inside.
QueryOptions queryOptions = new QueryOptions.Builder().projectId(projectId).addCollectionIds(collectionId).naturalLanguageQuery(// Feel free to replace this to query something different.
"Watson").build();
QueryResponse queryResponse = service.query(queryOptions).execute().getResult();
System.out.println(queryResponse.getMatchingResults() + " results were returned by the query!");
// See if the added document got returned by the query.
for (QueryResult result : queryResponse.getResults()) {
if (result.getDocumentId().equals(documentId)) {
System.out.println("Our new document matched the query!");
}
}
// Delete our uploaded document from the collection.
DeleteDocumentOptions deleteDocumentOptions = new DeleteDocumentOptions.Builder().projectId(projectId).collectionId(collectionId).documentId(documentId).build();
service.deleteDocument(deleteDocumentOptions).execute();
}
Aggregations