From ecf95296d53ce1d2a2c5e1fe40b0137849e2d00b Mon Sep 17 00:00:00 2001 From: tahmid-saj Date: Thu, 13 Jun 2024 16:20:19 -0400 Subject: [PATCH] moving requests --- .../api-requests/calories-burned.requests.js | 45 ++++--------------- .../nutrient-predictor.requests.js | 45 ++++--------------- 2 files changed, 18 insertions(+), 72 deletions(-) diff --git a/src/utils/api-requests/calories-burned.requests.js b/src/utils/api-requests/calories-burned.requests.js index 0bc5b2b..cdcdd69 100644 --- a/src/utils/api-requests/calories-burned.requests.js +++ b/src/utils/api-requests/calories-burned.requests.js @@ -6,50 +6,23 @@ import { errorOnGetSearchActivity, // calories burned api requests -// helper functions -async function processSearchedActivity(trackedDayInfo, activityResults) { - console.log(trackedDayInfo) - - return activityResults.map((activityResult) => { - return { - activity: String(activityResult.name), - searchedActivity: String(trackedDayInfo.activity), - dateTracked: String(trackedDayInfo.dateTracked), - caloriesBurnedPerHour: Number(activityResult.calories_per_hour), - durationMinutes: Number(activityResult.duration_minutes), - totalCaloriesBurned: Number(activityResult.total_calories) - } - }) -} - // searching activity export async function getSearchActivity(trackedDayInfo) { try { - let url = `${process.env.REACT_APP_API_NINJAS_CALORIES_BURNED_URL}${trackedDayInfo.activity}` - - if (trackedDayInfo.weightPounds !== "") { - url = url + `&weight=${trackedDayInfo.weightPounds}` - } - if (trackedDayInfo.durationMinutes !== "") { - url = url + `&duration=${trackedDayInfo.durationMinutes}` - } - - const resActivityResults = await fetch(`${url}`, { - method: "GET", + console.log(`${process.env.REACT_APP_API_URL_CALORIES_BURNED_SEARCH_ACTIVITY}`) + const response = await fetch(`${process.env.REACT_APP_API_URL_CALORIES_BURNED_SEARCH_ACTIVITY}`, { + method: "POST", headers: { - "X-Api-Key": `${process.env.REACT_APP_API_NINJAS_KEY}` - } + "Content-Type": "application/json" + }, + body: JSON.stringify(trackedDayInfo) }) + const resJSON = await response.json() - const resJSON = await resActivityResults.json() - const res = await processSearchedActivity(trackedDayInfo, resJSON) - return res + return resJSON.searchedActivities } catch (error) { + console.log(error) errorOnGetSearchActivity() - - if (error) { - return console.error("Request failed: ", error) - } } } diff --git a/src/utils/api-requests/nutrient-predictor.requests.js b/src/utils/api-requests/nutrient-predictor.requests.js index baa571c..d170ccb 100644 --- a/src/utils/api-requests/nutrient-predictor.requests.js +++ b/src/utils/api-requests/nutrient-predictor.requests.js @@ -1,29 +1,5 @@ import { errorOnGetNutrientPredictions } from "../errors/nutrient-predictor.errors"; -// helper functions -const processNutrientPredictions = async (nutrientPredictions) => { - return nutrientPredictions.map((nutrientPrediction) => { - return { - name: nutrientPrediction.name, - servingSizeG: nutrientPrediction.serving_size_g, - calories: nutrientPrediction.calories, - macronutrients: { - carbohydratesTotalG: nutrientPrediction.carbohydrates_total_g, - proteinG: nutrientPrediction.protein_g, - fatTotalG: nutrientPrediction.fat_total_g, - fatSaturatedG: nutrientPrediction.fat_saturated_g, - }, - micronutrients: { - sodiumMG: nutrientPrediction.sodium_mg, - potassiumMG: nutrientPrediction.potassium_mg, - cholesterolMg: nutrientPrediction.cholesterol_mg, - fiberG: nutrientPrediction.fiber_g, - sugarG: nutrientPrediction.sugar_g - } - } - }) -} - // nutrient predictor api requests export const getMealPredictions = async (imagePath) => { @@ -49,22 +25,19 @@ export const getMealPredictions = async (imagePath) => { export const getNutrientPredictions = async (mealDescription) => { try { - const resNutrientPredictions = await fetch(`${process.env.REACT_APP_API_NINJAS_NUTRIENT_PREDICTOR_URL}${mealDescription}`, { - method: "GET", + console.log(`${process.env.REACT_APP_API_URL_NUTRIENT_PREDICTOR_PREDICT_NUTRIENTS}`) + const response = await fetch(`${process.env.REACT_APP_API_URL_NUTRIENT_PREDICTOR_PREDICT_NUTRIENTS}`, { + method: "POST", headers: { - "X-Api-Key": `${process.env.REACT_APP_API_NINJAS_KEY}` - } + "Content-Type": "text/plain" + }, + body: String(mealDescription) }) + const resJSON = await response.json() - const resJSON = await resNutrientPredictions.json() - const res = await processNutrientPredictions(resJSON) - console.log(res) - return res + return resJSON.predictionResults } catch (error) { + console.log(error) errorOnGetNutrientPredictions() - - if (error) { - return console.error("Request failed: ", error) - } } } \ No newline at end of file