IanTDuncan / MealTime

Project for CSC 480
0 stars 0 forks source link

API - Spoonacular - GenerateShopplingList Class #157

Closed BeepDroid closed 6 months ago

BeepDroid commented 6 months ago

API - Spoonacular - GenerateShopplingList Class


Description:


This class stores the logic for users to add the contents of their chosen meal plan to their shopping list via generation based on the contents of their meal plan and takes the time-frame of the meal plan into consideration.

Steps to Reproduce:


1.Connect to base URL using Retrofit. 2.Call the MealPlanningService interface. 3.Format your strings for logging in, including username, apiKey, hash, and the method we described in the interface. 4.Call on the service then input the "onResponse" method to capture the response from the API.

Expected vs. Actual Behavior:


Expected behavior: Connection to the API and generation of a shoppling list based on the data the user generated in their meal plan. Actual behavior: Connection successful, but gneration action failed as there was no user with a meal plan attached. Will be resolved upon logic being added to button functionality.

Code Snippets:



package com.example.mealtime1;
import com.spoonacular.client.model.AddToMealPlanRequest;
import com.spoonacular.client.model.GenerateShoppingList200Response;
import com.spoonacular.client.model.GenerateShoppingListRequest;

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class GenerateShoppingList {
    public static void main(String[] args) {
        // Define your API base URL
        String baseUrl = "https://api.spoonacular.com/";

        // Create Retrofit instance
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        // Create your Retrofit service interface
        MealPlanningService service = retrofit.create(MealPlanningService.class);

        // Set your API key
        String apiKey = "faadc412663942a8909197924745241d";

        String username = "dsky"; // The username
        String startDate = "2020-06-01"; // The start date in the format yyyy-mm-dd
        String endDate = "2020-06-07"; // The end date in the format yyyy-mm-dd
        String hash = "hash_example"; // The private hash for the username
        GenerateShoppingListRequest generateShoppingListRequest = new GenerateShoppingListRequest();

        // Make API call using Retrofit
        Call<GenerateShoppingList200Response> call = service.generateShoppingList(username, startDate, endDate, hash, generateShoppingListRequest, apiKey);
        call.enqueue(new Callback<GenerateShoppingList200Response>() {
            @Override
            public void onResponse(Call<GenerateShoppingList200Response> call, Response<GenerateShoppingList200Response> response) {
                if (response.isSuccessful()) {
                    GenerateShoppingList200Response result = response.body();
                    System.out.println(result);
                } else {
                    System.err.println("Failed to generate shopping list: " + response.errorBody().toString());
                }
            }

            @Override
            public void onFailure(Call<GenerateShoppingList200Response> call, Throwable t) {
                System.err.println("Exception occurred while calling MealPlanningApi#generateShoppingList: " + t.getMessage());
            }
        });

    }
}

Environment Details:


Android Studio, Retrofit2, Spoonacular API, Windows OS.

Current Status:


Completed