IanTDuncan / MealTime

Project for CSC 480
0 stars 0 forks source link

API - Spoonacular - RecipeSearch #160

Closed IanTDuncan closed 6 months ago

IanTDuncan commented 6 months ago

API - Spoonacular - RecipeSearch


Description:

This issue details the code that returns recipe information when fed a recipe ID owned by Spoonacular. This will be primarily used for the recipes generated by the Meal Planner.


Steps To Reproduce:

  1. Create the retrofit interface based on the sdk
  2. Design the class around this interface

Expected Vs. Actual Behavior:

Expected Behavior: Compile correctly and return recipe information Actual Behavior: Compiles correctly and accepts input


Code Snippets:

RecipeServiceInterface


package com.example.mealtime1;

import com.spoonacular.client.model.GetRecipeInformation200Response;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;
import retrofit2.http.Query;

interface RecipeService {
    @GET("/recipes/{id}/information")
    Call<GetRecipeInformation200Response> getRecipeInformation(
            @Path("id") int id,
            @Query("includeNutrition") boolean includeNutrition
    );
}

RecipeResponseListener


package com.example.tester;

public interface RecipeResponseListener<T> {
    void onSuccess(T result);
    void onFailure(int errorCode);
}

RecipeSearch


package com.example.mealtime1;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import com.spoonacular.client.ApiClient;
import com.spoonacular.client.ApiException;
import com.spoonacular.client.auth.ApiKeyAuth;
import com.spoonacular.RecipesApi;

public class RecipeSearch {

    private static final String BASE_URL = "https://api.spoonacular.com";
    private static final String API_KEY = "YOUR API KEY"; // Replace with your actual API key

    public static <GetRecipeInformation200Response> void main(String[] args) {
        // Check if the recipe ID is provided as a command-line argument
        if (args.length < 1) {
            System.err.println("Usage: java RecipeSearch <recipe_id>");
            return;
        }

        // Parse the recipe ID from the command-line argument
        Integer id = Integer.parseInt(args[0]);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        RecipesApi RecipeService = retrofit.create(RecipesApi.class);

        // Set API key
        ApiClient defaultClient = com.spoonacular.client.Configuration.getDefaultApiClient();
        ApiKeyAuth apiKeyScheme = (ApiKeyAuth) defaultClient.getAuthentication("apiKeyScheme");
        apiKeyScheme.setApiKey(API_KEY);

        // Input your own information here
        Boolean includeNutrition = false; // Include nutrition data in the recipe information

        Call<GetRecipeInformation200Response> call = RecipeService.getRecipeInformation(id, includeNutrition);
        call.enqueue(new Callback<GetRecipeInformation200Response>() {
            @Override
            public void onResponse(Call<GetRecipeInformation200Response> call, Response<GetRecipeInformation200Response> response) {
                if (response.isSuccessful()) {
                    GetRecipeInformation200Response result = response.body();
                    System.out.println(result);
                } else {
                    System.err.println("Request failed with code: " + response.code());
                }
            }

            @Override
            public void onFailure(Call<GetRecipeInformation200Response> call, Throwable t) {
                if (t instanceof ApiException) {
                    ApiException apiException = (ApiException) t;
                    System.err.println("API Exception: " + apiException.getMessage());
                    // Handle ApiException appropriately
                } else {
                    System.err.println("Request failed: " + t.getMessage());
                    t.printStackTrace();
                }
            }
        });
    }
}

Environment Details:

Operating System: Windows 11 Kotlin Version: 1.5.21 Android Studio Iguana 2023.2.1 Android Gradle Plugin Version: 8.3.0 Gradle Version: 8.4


Current Status:

DONE