/// <summary>
        /// Create a session.
        ///
        /// 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.
        /// </summary>
        /// <param name="callback">The callback function that is invoked when the operation completes.</param>
        /// <param name="assistantId">Unique identifier of the assistant. To find the assistant ID in the Watson
        /// Assistant user interface, open the assistant settings and click **API Details**. For information about
        /// creating assistants, see the
        /// [documentation](https://cloud.ibm.com/docs/services/assistant?topic=assistant-assistant-add#assistant-add-task).
        ///
        /// **Note:** Currently, the v2 API does not support creating assistants.</param>
        /// <returns><see cref="SessionResponse" />SessionResponse</returns>
        public bool CreateSession(Callback <SessionResponse> callback, string assistantId)
        {
            if (callback == null)
            {
                throw new ArgumentNullException("`callback` is required for `CreateSession`");
            }
            if (string.IsNullOrEmpty(assistantId))
            {
                throw new ArgumentNullException("`assistantId` is required for `CreateSession`");
            }

            RequestObject <SessionResponse> req = new RequestObject <SessionResponse>
            {
                Callback               = callback,
                HttpMethod             = UnityWebRequest.kHttpVerbPOST,
                DisableSslVerification = true
            };

            foreach (KeyValuePair <string, string> kvp in customRequestHeaders)
            {
                req.Headers.Add(kvp.Key, kvp.Value);
            }

            ClearCustomRequestHeaders();

            foreach (KeyValuePair <string, string> kvp in Common.GetSdkHeaders("conversation", "V2", "CreateSession"))
            {
                Log.Debug("Headers: ", kvp.Key + " : " + kvp.Value);
                req.Headers.Add(kvp.Key, kvp.Value);
            }

            req.Parameters["version"] = VersionDate;

            req.OnResponse = OnCreateSessionResponse;

            RESTConnector connector = RESTConnector.GetConnector(Authenticator, string.Format("/v2/assistants/{0}/sessions", assistantId), GetServiceUrl());

            if (connector == null)
            {
                return(false);
            }

            Log.Debug("URI: ", string.Format("/v2/assistants/{0}/sessions", assistantId));
            Log.Debug("Req: ", req.ToString());
            bool success = connector.Send(req);

            return(success);
        }
예제 #2
0
        public static async Task <string> callApiAsync(RequestObject requestObject)
        {
            string requestString = requestObject.ToString();

            using (HttpClient client = new HttpClient())
            {
                using (HttpResponseMessage response = await client.GetAsync(requestString))
                {
                    using (HttpContent content = response.Content)
                    {
                        string data = await content.ReadAsStringAsync();

                        System.IO.File.WriteAllText("output.json", data);
                        return(data);
                    }
                }
            }
        }