IanTDuncan / MealTime

Project for CSC 480
0 stars 0 forks source link

Spoonacular API - DeleteFromShoppingList Class #151

Closed BeepDroid closed 3 months ago

BeepDroid commented 3 months ago

Spoonacular API - DeleteFromShoppingList Class


Description:


Calls the method DeleteFromShopplistList from Spoonacular to allow a user to delete an item from their shoppling list.

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:


Code Snippets:




import com.spoonacular.MealPlanningApi;
import com.spoonacular.client.model.DeleteFromMealPlanRequest;

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

public class DeleteFromShoppingList {
    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";

        // Prepare request data
        String username = "dsky"; // The username
        Integer id = 1; // The item's id
        String hash = "hash_example"; // The private hash for the username
        DeleteFromMealPlanRequest deleteFromMealPlanRequest = new DeleteFromMealPlanRequest();

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

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

### Environment Details:
____________________________
Android Studio, Retrofit2, Spoonacular API, Windows OS.

### Current Status:
________________________
Closed