コード例 #1
0
ファイル: APICall.cs プロジェクト: ChaskyH/WpfApp
        public async Task <NutritionsObject> GetNutritions(String query)
        {
            var json = new JObject();

            json["query"] = query;

            HttpResponseMessage response = await client.PostAsync("natural/nutrients", new StringContent(json.ToString(), Encoding.UTF8, "application/json"));

            if (response.IsSuccessStatusCode)
            {
                var result = await response.Content.ReadAsStringAsync();

                try
                {
                    JObject obj = JsonConvert.DeserializeObject(result) as JObject;

                    var j = obj["foods"].ToList().FirstOrDefault();
                    return(NutritionsObject.FromJson(j.ToString()));
                }
                catch (Exception)
                {
                    return(null);
                }
            }

            return(null);
        }
コード例 #2
0
ファイル: NutritionsObject.cs プロジェクト: ChaskyH/WpfApp
 public static string ToJson(this NutritionsObject self) => JsonConvert.SerializeObject(self, NutritionAPI.Converter.Settings);
コード例 #3
0
ファイル: APICall.cs プロジェクト: ChaskyH/WpfApp
        public async Task <Meal> GetMealAsync(String query)
        {
            Meal meal = new Meal();

            var json = new JObject();

            json["query"] = query;

            HttpResponseMessage response = await client.PostAsync("natural/nutrients", new StringContent(json.ToString(), Encoding.UTF8, "application/json"));

            if (response.IsSuccessStatusCode)
            {
                var result = await response.Content.ReadAsStringAsync();

                try
                {
                    JObject obj = JsonConvert.DeserializeObject(result) as JObject;

                    foreach (var food in  obj["foods"].ToList())
                    {
                        var no = NutritionsObject.FromJson(food.ToString());
                        if (no != null)
                        {
                            meal.Foods.Add(new FoodNutritions()
                            {
                                FoodName          = no.FoodName,
                                ServingQuantity   = (int)no.ServingQty,
                                ServingUnit       = no.ServingUnit,
                                ServingWeight     = no.ServingWeightGrams.ToString(),
                                Calories          = no.NfCalories,
                                TotalFat          = no.NfTotalFat,
                                SaturatedFat      = no.NfSaturatedFat,
                                Cholestrol        = no.NfCholesterol,
                                Sodium            = no.NfSodium,
                                TotalCarbohydrate = no.NfTotalCarbohydrate,
                                DietaryFiber      = no.NfDietaryFiber,
                                Sugars            = no.NfSugars,
                                Protien           = no.NfProtein,
                                Potassium         = no.NfPotassium,
                                Photo             = no.Photo.Highres
                            });

                            meal.Date = food["consumed_at"].ToObject <DateTime>();

                            switch (int.Parse(food["meal_type"].ToString()))
                            {
                            case 1: meal.Type = "Breakfast"; break;

                            case 2: meal.Type = "Morning Snack"; break;

                            case 3: meal.Type = "Lunch"; break;

                            case 4: meal.Type = "Afternoon Snack"; break;

                            case 5: meal.Type = "Dinner"; break;

                            case 6: meal.Type = "Evening Snack"; break;
                            }
                            ;
                        }
                    }



                    return(meal);
                }
                catch (Exception)
                {
                    return(meal);
                }
            }

            return(meal);
        }