Пример #1
0
        public static ONErrorHandler GetErrorCodeHTTP(HttpStatusCode _StatusCode, ApiMethodTypes _Method)
        {
            switch (_StatusCode)
            {
            case HttpStatusCode.PreconditionFailed:
                return(new ONErrorHandler(ErrorCode.PRECONDITION_FAILED));

            case HttpStatusCode.NotFound:
                return(new ONErrorHandler(ErrorCode.NOT_FOUND));

            case HttpStatusCode.Conflict:
                return(new ONErrorHandler(ErrorCode.CONFLICT));

            default:
                return(new ONErrorHandler(ErrorCode.HTTP_DEFAULT));
            }
        }
Пример #2
0
        internal async Task <T> SendAsync <T>(String uri, ApiMethodTypes methodType = ApiMethodTypes.MethodGet, Object body = null)
        {
            string responseString = "";

            try
            {
                using (var c = new HttpClient())
                {
                    var client = new HttpClient();

                    client.Timeout = new TimeSpan(0, 0, 30);

                    //Adicionando a chave da API no header da chamada, para permitir a busca por 25 animais
                    client.DefaultRequestHeaders.Add("x-api-key", HttpUtils.XAPIKEY_HEADER);

                    HttpContent content = new StringContent(SerializeToJson(body), Encoding.UTF8, HttpUtils.CONTENT_TYPE);

                    HttpResponseMessage response = null;
                    switch (methodType)
                    {
                    case ApiMethodTypes.MethodPost:
                        response = await client.PostAsync(uri, content).ConfigureAwait(false);

                        break;

                    case ApiMethodTypes.MethodGet:
                        response = await client.GetAsync(uri).ConfigureAwait(false);

                        break;

                    case ApiMethodTypes.MethodPut:
                        response = await client.PutAsync(uri, content).ConfigureAwait(false);

                        break;

                    case ApiMethodTypes.MethodDelete:
                        response = await client.DeleteAsync(uri);

                        return((T)(object)response.IsSuccessStatusCode);

                    default:
                        return(default(T));
                    }

                    if (response.IsSuccessStatusCode)
                    {
                        responseString = response.Content.ReadAsStringAsync().Result;
                        return(DeserializeFromJson <T>(responseString));
                    }
                    else
                    {
                        if (response.StatusCode == System.Net.HttpStatusCode.InternalServerError)
                        {
                            ErrorModel err = DeserializeFromJson <ErrorModel>(
                                response.Content.ReadAsStringAsync().Result);
                            if (err != null)
                            {
                                throw new ONErrorHandler(err);
                            }
                        }
                        return(default(T));
                    }
                }
            }
            catch (JsonReaderException j)
            {
                throw new ONErrorHandler(ErrorCode.JSON_CONVERTER);
            }
            catch (HttpRequestException h)
            {
                throw new ONErrorHandler(ErrorCode.HTTP_EXCEPTION);
            }
            catch (TaskCanceledException t)
            {
                throw new ONErrorHandler(ErrorCode.HTTP_EXCEPTION);
            }
            catch (ONErrorHandler e)
            {
                throw e;
            }
            catch (Exception ex)
            {
                throw new ONErrorHandler(ex);
            }
        }