IanTDuncan / MealTime

Project for CSC 480
0 stars 0 forks source link

API - Spoonacular - AddToShoppingList Class #156

Closed BeepDroid closed 3 months ago

BeepDroid commented 3 months ago

API - Spoonacular - AddToShoppingList Class

Description:


The logic class for adding items the user chooses from their respective meal plans to the shopping list, using documentation provided by Spoonacular and the Meal Service interface.

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: Successful connection to the API and access to the shopping list method call. Actual behavior: Connection successful, but adding action failed due to their being no shopping list present. 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 retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class AddToShoppingList {
    public static void main(String[] args) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.spoonacular.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

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

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

        // Prepare request data
        String username = "dsky"; // The username
        String hash = "hash_example"; // The private hash for the username
        AddToMealPlanRequest addToMealPlanRequest = new AddToMealPlanRequest();

        // Make API call using Retrofit
        Call<GenerateShoppingList200Response> call = service.addToShoppingList(username, hash, addToMealPlanRequest, 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 add to shopping list: " + response.errorBody().toString());
                }
            }

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

Environment Details:


Android Studio, Retrofit2, Spoonacular API, Windows OS.

Current Status:


Completed