Search in sources :

Example 96 with Builder

use of okhttp3.HttpUrl.Builder in project java-sdk by watson-developer-cloud.

the class DiscoveryServiceTest method addDocumentFromInputStreamIsSuccessful.

@Test
public void addDocumentFromInputStreamIsSuccessful() throws InterruptedException {
    server.enqueue(jsonResponse(createDocResp));
    String myDocumentJson = "{\"field\":\"value\"}";
    JsonObject myMetadata = new JsonObject();
    myMetadata.add("foo", new JsonPrimitive("bar"));
    InputStream documentStream = new ByteArrayInputStream(myDocumentJson.getBytes());
    AddDocumentOptions.Builder builder = new AddDocumentOptions.Builder(environmentId, collectionId);
    builder.file(documentStream);
    builder.filename("test_file");
    builder.metadata(myMetadata.toString());
    DocumentAccepted response = discoveryService.addDocument(builder.build()).execute();
    RecordedRequest request = server.takeRequest();
    assertEquals(DOCS1_PATH, request.getPath());
    assertEquals(POST, request.getMethod());
    assertEquals(createDocResp, response);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) DocumentAccepted(com.ibm.watson.developer_cloud.discovery.v1.model.DocumentAccepted) JsonPrimitive(com.google.gson.JsonPrimitive) ByteArrayInputStream(java.io.ByteArrayInputStream) AddDocumentOptions(com.ibm.watson.developer_cloud.discovery.v1.model.AddDocumentOptions) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JsonObject(com.google.gson.JsonObject) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 97 with Builder

use of okhttp3.HttpUrl.Builder in project java-sdk by watson-developer-cloud.

the class DiscoveryServiceTest method createTrainingExampleIsSuccessful.

@Test
public void createTrainingExampleIsSuccessful() throws InterruptedException {
    server.enqueue(jsonResponse(createTrainingExampleResp));
    CreateTrainingExampleOptions.Builder builder = new CreateTrainingExampleOptions.Builder(environmentId, collectionId, queryId);
    builder.documentId(documentId);
    builder.relevance(0);
    TrainingExample response = discoveryService.createTrainingExample(builder.build()).execute();
    RecordedRequest request = server.takeRequest();
    assertEquals(TRAINING2_PATH, request.getPath());
    assertEquals(POST, request.getMethod());
    assertEquals(createTrainingExampleResp, response);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) CreateTrainingExampleOptions(com.ibm.watson.developer_cloud.discovery.v1.model.CreateTrainingExampleOptions) TrainingExample(com.ibm.watson.developer_cloud.discovery.v1.model.TrainingExample) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 98 with Builder

use of okhttp3.HttpUrl.Builder in project java-sdk by watson-developer-cloud.

the class DiscoveryServiceTest method federatedQueryNoticesIsSuccessful.

@Test
public void federatedQueryNoticesIsSuccessful() throws InterruptedException {
    server.enqueue(jsonResponse(queryNoticesResp));
    FederatedQueryNoticesOptions.Builder builder = new FederatedQueryNoticesOptions.Builder(environmentId, new ArrayList<>(Arrays.asList(collectionId)));
    QueryNoticesResponse response = discoveryService.federatedQueryNotices(builder.build()).execute();
    RecordedRequest request = server.takeRequest();
    assertEquals(Q4_PATH, request.getPath());
    assertEquals(GET, request.getMethod());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) QueryNoticesResponse(com.ibm.watson.developer_cloud.discovery.v1.model.QueryNoticesResponse) FederatedQueryNoticesOptions(com.ibm.watson.developer_cloud.discovery.v1.model.FederatedQueryNoticesOptions) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 99 with Builder

use of okhttp3.HttpUrl.Builder in project java-sdk by watson-developer-cloud.

the class DiscoveryServiceTest method addDocumentFromInputStreamWithFileNameAndMediaTypeIsSuccessful.

@Test
public void addDocumentFromInputStreamWithFileNameAndMediaTypeIsSuccessful() throws InterruptedException {
    server.enqueue(jsonResponse(createDocResp));
    String fileName = "MyFileName";
    String myDocumentJson = "{\"field\":\"value\"}";
    JsonObject myMetadata = new JsonObject();
    myMetadata.add("foo", new JsonPrimitive("bar"));
    InputStream documentStream = new ByteArrayInputStream(myDocumentJson.getBytes());
    AddDocumentOptions.Builder builder = new AddDocumentOptions.Builder(environmentId, collectionId);
    builder.file(documentStream).fileContentType(HttpMediaType.APPLICATION_JSON);
    builder.filename("test_file");
    builder.metadata(myMetadata.toString());
    DocumentAccepted response = discoveryService.addDocument(builder.build()).execute();
    RecordedRequest request = server.takeRequest();
    assertEquals(DOCS1_PATH, request.getPath());
    assertEquals(POST, request.getMethod());
    assertEquals(createDocResp, response);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) DocumentAccepted(com.ibm.watson.developer_cloud.discovery.v1.model.DocumentAccepted) JsonPrimitive(com.google.gson.JsonPrimitive) ByteArrayInputStream(java.io.ByteArrayInputStream) AddDocumentOptions(com.ibm.watson.developer_cloud.discovery.v1.model.AddDocumentOptions) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JsonObject(com.google.gson.JsonObject) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest) Test(org.junit.Test)

Example 100 with Builder

use of okhttp3.HttpUrl.Builder in project java-sdk by watson-developer-cloud.

the class NaturalLanguageClassifier method createClassifier.

/**
 * Create classifier.
 *
 * Sends data to create and train a classifier and returns information about the new classifier.
 *
 * @param createClassifierOptions the {@link CreateClassifierOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link Classifier}
 */
public ServiceCall<Classifier> createClassifier(CreateClassifierOptions createClassifierOptions) {
    Validator.notNull(createClassifierOptions, "createClassifierOptions cannot be null");
    String[] pathSegments = { "v1/classifiers" };
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments));
    MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
    multipartBuilder.setType(MultipartBody.FORM);
    RequestBody trainingMetadataBody = RequestUtils.inputStreamBody(createClassifierOptions.metadata(), "application/json");
    multipartBuilder.addFormDataPart("training_metadata", createClassifierOptions.metadataFilename(), trainingMetadataBody);
    RequestBody trainingDataBody = RequestUtils.inputStreamBody(createClassifierOptions.trainingData(), "text/csv");
    multipartBuilder.addFormDataPart("training_data", createClassifierOptions.trainingDataFilename(), trainingDataBody);
    builder.body(multipartBuilder.build());
    return createServiceCall(builder.build(), ResponseConverterUtils.getObject(Classifier.class));
}
Also used : RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) MultipartBody(okhttp3.MultipartBody) RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) Classifier(com.ibm.watson.developer_cloud.natural_language_classifier.v1.model.Classifier) RequestBody(okhttp3.RequestBody)

Aggregations

Request (okhttp3.Request)206 Response (okhttp3.Response)148 OkHttpClient (okhttp3.OkHttpClient)142 IOException (java.io.IOException)111 RequestBody (okhttp3.RequestBody)81 Test (org.junit.Test)75 HttpUrl (okhttp3.HttpUrl)47 File (java.io.File)42 MockResponse (okhttp3.mockwebserver.MockResponse)42 MultipartBody (okhttp3.MultipartBody)40 Map (java.util.Map)39 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)31 Call (okhttp3.Call)29 Interceptor (okhttp3.Interceptor)29 Retrofit (retrofit2.Retrofit)29 Builder (okhttp3.OkHttpClient.Builder)26 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)25 ResponseBody (okhttp3.ResponseBody)24 HashMap (java.util.HashMap)22 FormBody (okhttp3.FormBody)21