IanTDuncan / MealTime

Project for CSC 480
0 stars 0 forks source link

API - Edamam Meal Planner - Meal Planner Client #121

Closed IanTDuncan closed 4 months ago

IanTDuncan commented 4 months ago

API - Edamam Meal Planner - Meal Planner Client


Description:


"MealPlannerClient" is an abstraction layer between MealTime and the Edamam Meal Planner API. It encapsulates the logic for making HTTP requests to the API endpoints, handling the request parameters, and processing the response.

Updated On 3/4/2024 Made a new version of the client; added a testing class

Steps To Reproduce:


  1. Make sure Retrofit is properly implemented in build.gradle.kts
  2. Initialize Retrofit using MealPlannerService, make sure to include the correct URL
  3. When making the code include all the parameters that are to be passed to the URL

Expected Vs Actual Behavior:


Expected Behavior: Take the data submitted and send it to MealPlannerService, if successful return data, if not cause error Actual Behavior: Encountering errors with connecting to the service

Code Snippets:


`import android.content.Context; import android.widget.Toast; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response;

public class EdamamApiManager {

private final Context context;
private final EdamamApiService apiService;

public EdamamApiManager(Context context) {
    this.context = context;
    this.apiService = EdamamApiClient.create();
}

public void searchRecipes() {
    //String appId = "xxxxx";
    String appKey = "xxxxx";
    Call<EdamamResponse> call = apiService.searchRecipes("day", "1500-2000", "balanced", "", appKey);

    call.enqueue(new Callback<EdamamResponse>() {
        @Override
        public void onResponse(Call<EdamamResponse> call, Response<EdamamResponse> response) {
            if (response.isSuccessful()) {
                EdamamResponse edamamResponse = response.body();
                if (edamamResponse != null) {
                    System.out.print(edamamResponse);
                }
            } else {
                Toast.makeText(context, "Failed to fetch recipes", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(Call<EdamamResponse> call, Throwable t) {
            Toast.makeText(context, "Error: " + t.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });
}

}`


`package com.example.mealtime1.APIs.MealPlannerTest;

import android.content.Context;

import org.junit.Test; import org.mockito.Mockito;

public class EdamamApiManagerTest {

@Test
public void testConnectionToEdamamMealPlanner() {
    // Create an instance of EdamamApiManager
    EdamamApiManager apiManager = new EdamamApiManager(getContext());

    // Call the searchRecipes method with sample parameters
    apiManager.searchRecipes();

    // Since the searchRecipes method makes an asynchronous call,
    // you may want to add some assertions or verify some behavior here
}

// Helper method to get a mock context
private Context getContext() {
    return Mockito.mock(Context.class);
}

} `

Environment Details:


Operating System: Windows 11 Firebase SDK Version: 23.0.0 Kotlin Version: 1.5.21 Android Studio Version: 4.2.1 Gradle Version: 7.0.2

Current Status: COMPLETED