brunobrandes / api-ai-csharp

A C# wrapper for the api.ai
Apache License 2.0
18 stars 7 forks source link

HttpResponseException #2

Closed SkyQuant closed 7 years ago

SkyQuant commented 7 years ago

I have an exception An unhandled exception of type 'System.Web.Http.HttpResponseException' occurred in mscorlib.dll. Additional information: Processing of the HTTP request resulted in an exception. Please see the HTTP response returned by the 'Response' property of this exception for details. on this code:

        private static async void AiRun()
        {
            var container = new Container();
            container.RegisterSingleton<IServiceProvider>(container);
            container.Register<IApiAiAppServiceFactory, ApiAiAppServiceFactory>();
            container.Register<IHttpClientFactory, HttpClientFactory>();
            var apiAiAppServiceFactory = container.GetInstance<IApiAiAppServiceFactory>();
            var queryAppService = apiAiAppServiceFactory.CreateQueryAppService("https://api.api.ai/v1", "my_token");

            var queryRequest = new QueryRequest
            {
                Query = new string[] { "Hello, I want a pizza" },
                Lang = Api.Ai.Domain.Enum.Language.English
            };
            var queryResponseGetq = await queryAppService.GetQueryAsync(queryRequest);
            var queryResponsePost = await queryAppService.PostQueryAsync(queryRequest);
        }
brunobrandes commented 7 years ago

Hello @SkyQuant Use try catch will capture stack trace.


var container = new Container();

container.RegisterSingleton<IServiceProvider>(container);
container.Register<IApiAiAppServiceFactory, ApiAiAppServiceFactory>();
container.Register<IHttpClientFactory, HttpClientFactory>();

var apiAiAppServiceFactory = container.GetInstance<IApiAiAppServiceFactory>();
var queryAppService = apiAiAppServiceFactory.CreateQueryAppService("https://api.api.ai/v1", 
"my_token");

var queryRequest = new QueryRequest
{
    Query = new string[] { "Hello, I want a pizza" },
    Lang = Api.Ai.Domain.Enum.Language.English
};

try
{
    var queryResponsePost = await queryAppService.PostQueryAsync(queryRequest);
}
catch (HttpResponseException ex)
{
    var description = ex.StackTrace;
}

You are using api.ai developer access token in my_token parameter?

SkyQuant commented 7 years ago

There is no such exception if method PostQueryAsync is synchronous. But it is in async console app.

        static void Main(string[] args)
        {
            AiRun().GetAwaiter().GetResult();
         // AiRun().Wait();
            Console.ReadKey();
        }

        private static async Task AiRun()
        {
            var container = new Container();

            container.RegisterSingleton<IServiceProvider>(container);
            container.Register<IApiAiAppServiceFactory, ApiAiAppServiceFactory>();
            container.Register<IHttpClientFactory, HttpClientFactory>();

            var apiAiAppServiceFactory = container.GetInstance<IApiAiAppServiceFactory>();
            var queryAppService = apiAiAppServiceFactory.CreateQueryAppService("https://api.api.ai/v1",
            "My Developer Or Client access token");

            var queryRequest = new QueryRequest
            {
                Query = new string[] { "Hello, I want a pizza" },
                Lang = Api.Ai.Domain.Enum.Language.English
            };

            try
            {
                var queryResponsePost = await queryAppService.PostQueryAsync(queryRequest);
            }
            catch (HttpResponseException ex)
            {
                var description = ex.StackTrace;
                Debugger.Break();
            }
        }
brunobrandes commented 7 years ago

Hi @SkyQuant I just publish a new nuget package version (1.0.2) with ApiAiException object to better describe api.ai response error.

I identified that my example does not contains the sessionId field and he is required.

Update your Api.Ai.Csharp package and add sessionId property in the QueryRequest object. 😉

var queryRequest = new QueryRequest
{
    SessionId = "1",
    Query = new string[] { "Hello, I want a pizza" },
    Lang = Api.Ai.Domain.Enum.Language.English
};