Search in sources :

Example 1 with CreateSessionOptions

use of com.ibm.watson.assistant.v2.model.CreateSessionOptions in project java-sdk by watson-developer-cloud.

the class AssistantTest method testCreateSessionWOptions.

// Test the createSession operation with a valid options model parameter
@Test
public void testCreateSessionWOptions() throws Throwable {
    // Register a mock response
    String mockResponseBody = "{\"session_id\": \"sessionId\"}";
    String createSessionPath = "/v2/assistants/testString/sessions";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(201).setBody(mockResponseBody));
    // Construct an instance of the CreateSessionOptions model
    CreateSessionOptions createSessionOptionsModel = new CreateSessionOptions.Builder().assistantId("testString").build();
    // Invoke createSession() with a valid options model and verify the result
    Response<SessionResponse> response = assistantService.createSession(createSessionOptionsModel).execute();
    assertNotNull(response);
    SessionResponse responseObj = response.getResult();
    assertNotNull(responseObj);
    // Verify the contents of the request sent to the mock server
    RecordedRequest request = server.takeRequest();
    assertNotNull(request);
    assertEquals(request.getMethod(), "POST");
    // Verify request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, createSessionPath);
    // Verify query params
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNotNull(query);
    assertEquals(query.get("version"), "testString");
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) CreateSessionOptions(com.ibm.watson.assistant.v2.model.CreateSessionOptions) SessionResponse(com.ibm.watson.assistant.v2.model.SessionResponse) Test(org.testng.annotations.Test)

Example 2 with CreateSessionOptions

use of com.ibm.watson.assistant.v2.model.CreateSessionOptions in project java-sdk by watson-developer-cloud.

the class Assistant method createSession.

/**
 * Create a session.
 *
 * <p>Create a new session. A session is used to send user input to a skill and receive responses.
 * It also maintains the state of the conversation. A session persists until it is deleted, or
 * until it times out because of inactivity. (For more information, see the
 * [documentation](https://cloud.ibm.com/docs/assistant?topic=assistant-assistant-settings).
 *
 * @param createSessionOptions the {@link CreateSessionOptions} containing the options for the
 *     call
 * @return a {@link ServiceCall} with a result of type {@link SessionResponse}
 */
public ServiceCall<SessionResponse> createSession(CreateSessionOptions createSessionOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(createSessionOptions, "createSessionOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("assistant_id", createSessionOptions.assistantId());
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v2/assistants/{assistant_id}/sessions", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("conversation", "v2", "createSession");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    builder.query("version", String.valueOf(this.version));
    ResponseConverter<SessionResponse> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<SessionResponse>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) SessionResponse(com.ibm.watson.assistant.v2.model.SessionResponse)

Aggregations

SessionResponse (com.ibm.watson.assistant.v2.model.SessionResponse)2 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)1 CreateSessionOptions (com.ibm.watson.assistant.v2.model.CreateSessionOptions)1 HashMap (java.util.HashMap)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1 Test (org.testng.annotations.Test)1