Ejemplo n.º 1
0
        public Task <ResponseWrapper <SentimentResponse> > DocumentSentimentAsync(AnalysisCell cell)
        {
            var input = new SentimentRequest {
                text = cell.Text
            };

            return(PostRequest <SentimentResponse, SentimentRequest>(input, "/v1/sentiment", cell));
        }
 public ResponseWrapper(T resp, AnalysisCell input)
 {
     Response = resp;
     Input    = input;
 }
Ejemplo n.º 3
0
        private async Task <ResponseWrapper <TOutput> > Request <TOutput, TInput>(TInput obj, string path, AnalysisCell cell) where TOutput : class
        {
            try
            {
                using (var handler = new HttpClientHandler {
                    Credentials = new NetworkCredential(Username, Password)
                })
                    using (var client = new HttpClient(handler))
                    {
                        client.BaseAddress = new Uri(BaseUri);
                        var         reqContent = JsonHelper.JsonSerialize(obj);
                        HttpContent content    = new StringContent(reqContent, Encoding.UTF8);
                        content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                        var resp = await client.PostAsync(path, content);

                        //test response code
                        if (resp.StatusCode != HttpStatusCode.OK)
                        {
                            var wrapper2 = new ResponseWrapper <TOutput>(null, cell);

                            GetHeaderValues <TOutput, TInput>(resp, wrapper2);
                            wrapper2.ErrorMessage = (int)resp.StatusCode + " (" + resp.ReasonPhrase + ")";

                            Trace.WriteLine("FAILED: " + (int)resp.StatusCode);

                            return(wrapper2);
                        }

                        resp.EnsureSuccessStatusCode();
                        var response = await resp.Content.ReadAsStringAsync();

                        var des = JsonHelper.JsonDeserialize <TOutput>(response);

                        var wrapper = new ResponseWrapper <TOutput>(des, cell);
                        GetHeaderValues <TOutput, TInput>(resp, wrapper);

                        return(wrapper);
                    }
            }
            catch (AggregateException aex)
            {
                var resp = new ResponseWrapper <TOutput>(null, cell);
                aex.Flatten().Handle(ex => // Note that we still need to call Flatten
                {
                    if (ex is HttpRequestException)
                    {
                        resp.ErrorMessage = ex.Message;
                        return(true);                         // This exception is "handled"
                    }
                    if (ex is IndexOutOfRangeException)
                    {
                        resp.ErrorMessage = ex.Message;

                        Trace.WriteLine("Index out of range");
                        return(true);                      // This exception is "handled"
                    }
                    return(false);                         // All other exceptions will get rethrown
                });
                return(resp);
            }
            catch (HttpRequestException ex)
            {
                return(new ResponseWrapper <TOutput>(null, cell)
                {
                    ErrorMessage = ex.Message
                });
            }
        }
Ejemplo n.º 4
0
 public Task <ResponseWrapper <TOutput> > PostRequest <TOutput, TInput>(TInput obj, string path, AnalysisCell cell) where TOutput : class
 {
     return(Request <TOutput, TInput>(obj, path, cell));
 }
Ejemplo n.º 5
0
        public Task <ResponseWrapper <SentimentEntityResponse[]> > EntitySentimentAsync(AnalysisCell cell)
        {
            var input = new SentimentRequest {
                text = cell.Text, level = "entity"
            };

            return(PostRequest <SentimentEntityResponse[], SentimentRequest>(input, "/v1/sentiment", cell));
        }