Пример #1
0
        public async Task <Response> QueryAsync(Type type, string payload)
        {
            if (!token.HasValue)
            {
                token.Value = await interop.LoadTokenAsync();

                EnsureAuthorization();
            }

            string url = queryMapper.FindUrlByType(type);

            if (url != null)
            {
                HttpResponseMessage response = await http.PostAsync($"/api/query/{url}", new StringContent(payload, Encoding.UTF8, "text/json"));

                if (response.StatusCode == HttpStatusCode.Unauthorized)
                {
                    ClearAuthorization();
                    UnauthorizedAccessException exception = new UnauthorizedAccessException();
                    exceptionHandler.Handle(exception);
                    throw exception;
                }

                string responseContent = await response.Content.ReadAsStringAsync();

                return(SimpleJson.SimpleJson.DeserializeObject <JsResponse>(responseContent));
            }
            else
            {
                return(await http.PostJsonAsync <Response>($"/api/query", CreateRequest(type, payload)));
            }
        }
Пример #2
0
        public async Task <Response> QueryAsync(Type type, string payload)
        {
            if (!token.HasValue)
            {
                token.Value = await interop.LoadTokenAsync();

                EnsureAuthorization();
            }

            string url = queryMapper.FindUrlByType(type);

            if (url != null)
            {
                try
                {
                    HttpResponseMessage response = await http.PostAsync($"/api/query/{url}", new StringContent(payload, Encoding.UTF8, "text/json"));

                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        string responseContent = await response.Content.ReadAsStringAsync();

                        log.Debug($"Response: '{responseContent}'.");
                        return(json.Deserialize <Response>(responseContent));
                    }
                    else if (response.StatusCode == HttpStatusCode.Unauthorized)
                    {
                        ClearAuthorization();
                        throw new UnauthorizedAccessException();
                    }
                    else if (response.StatusCode == HttpStatusCode.InternalServerError)
                    {
                        throw new InternalServerException();
                    }
                    else
                    {
                        throw Ensure.Exception.InvalidOperation($"Generic HTTP error: {response.StatusCode}.");
                    }
                }
                catch (Exception e)
                {
                    if (e is HttpRequestException)
                    {
                        e = new ServerNotRespondingException(e);
                    }

                    exceptionHandler.Handle(e);
                    throw;
                }
            }
            else
            {
                return(await http.PostJsonAsync <Response>($"/api/query", CreateRequest(type, payload)));
            }
        }