Exemple #1
0
 private static HttpResponseMessage Get(NutritionixUri uri, HttpClient client)
 {
     try
     {
         return(client.GetAsync(uri.ToString()).Result);
     }
     catch
     {
         string error = string.Format("An error occurred sending a request to the Nutritionix API. Uri: {0}", uri);
         throw new NutritionixException(error);
     }
 }
Exemple #2
0
        private TResult Get <TResult>(NutritionixUri uri) where TResult : new()
        {
            using (var client = CreateHttpClient())
            {
                HttpResponseMessage response = Get(uri, client);
                if (response.IsSuccessStatusCode)
                {
                    return(ReadResponse <TResult>(response));
                }

                throw CreateExceptionFromResponse(response);
            }
        }
Exemple #3
0
 private static HttpResponseMessage Post(NutritionixUri uri, HttpClient client, string json)
 {
     try
     {
         client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
         HttpContent content = new StringContent(json, new UTF8Encoding(), "application/json");
         return(client.PostAsync(uri.ToString(), content).Result);
     }
     catch
     {
         string error = string.Format("An error occurred sending a request to the Nutritionix API. Uri: {0}", uri);
         throw new NutritionixException(error);
     }
 }
Exemple #4
0
        private TResult Post <TRequest, TResult>(NutritionixUri uri, TRequest request) where TResult : new()
        {
            using (var client = CreateHttpClient())
            {
                string json = JsonConvert.SerializeObject(request, new JsonSerializerSettings {
                    NullValueHandling = NullValueHandling.Ignore
                });
                HttpResponseMessage response = Post(uri, client, json);
                if (response.IsSuccessStatusCode)
                {
                    return(ReadResponse <TResult>(response));
                }

                throw CreateExceptionFromResponse(response);
            }
        }