-
Notifications
You must be signed in to change notification settings - Fork 0
/
RequestManager.java
159 lines (130 loc) · 5.94 KB
/
RequestManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.example.reciepeapp;
import android.content.Context;
import com.example.reciepeapp.Listeners.InstructionsListener;
import com.example.reciepeapp.Listeners.RandomRecipeResponseListener;
import com.example.reciepeapp.Listeners.RecipeDetailsListener;
import com.example.reciepeapp.Listeners.SimilarRecipeListener;
import com.example.reciepeapp.Models.InstructionsResponse;
import com.example.reciepeapp.Models.RandomRecipeApiResponse;
import com.example.reciepeapp.Models.RecipeDetailsResponse;
import com.example.reciepeapp.Models.SimilarRecipeResponse;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Path;
import retrofit2.http.Query;
public class RequestManager {
Context context;
Retrofit retrofit=new Retrofit.Builder()
.baseUrl("https://api.spoonacular.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
public RequestManager(Context context) {
this.context = context;
}
public void getRandomRecipes(RandomRecipeResponseListener listener, List<String> tags){
CallRandomRecipes callRandomRecipes=retrofit.create(CallRandomRecipes.class);
Call<RandomRecipeApiResponse> call= callRandomRecipes.callRandomRecipe(context.getString(R.string.api_key), "15", tags);
call.enqueue(new Callback<RandomRecipeApiResponse>() {
@Override
public void onResponse(Call<RandomRecipeApiResponse> call, Response<RandomRecipeApiResponse> response) {
if (!response.isSuccessful()){
listener.didError(response.message());
return;
}
listener.didFetch(response.body(), response.message());
}
@Override
public void onFailure(Call<RandomRecipeApiResponse> call, Throwable t) {
listener.didError(t.getMessage());
}
});
}
public void getRecipeDetails(RecipeDetailsListener listener, int id){
CallRecipeDetails callRecipeDetails = retrofit.create(CallRecipeDetails.class);
Call<RecipeDetailsResponse> call = callRecipeDetails.callRecipeDetails(id, context.getString(R.string.api_key));
call.enqueue(new Callback<RecipeDetailsResponse>() {
@Override
public void onResponse(Call<RecipeDetailsResponse> call, Response<RecipeDetailsResponse> response) {
if(!response.isSuccessful()){
listener.didError(response.message());
return;
}
listener.didFetch(response.body(), response.message());
}
@Override
public void onFailure(Call<RecipeDetailsResponse> call, Throwable t) {
listener.didError(t.getMessage());
}
});
}
public void getSimilarRecipes(SimilarRecipeListener listener, int id ){
CallSimilarRecipes callSimilarRecipes = retrofit.create(CallSimilarRecipes.class);
Call<List<SimilarRecipeResponse>> call = callSimilarRecipes.callSimilarRecipe(id, "4", context.getString(R.string.api_key));
call.enqueue(new Callback<List<SimilarRecipeResponse>>() {
@Override
public void onResponse(Call<List<SimilarRecipeResponse>> call, Response<List<SimilarRecipeResponse>> response) {
if(!response.isSuccessful()){
listener.didError(response.message());
}
listener.didFetch(response.body(), response.message());
}
@Override
public void onFailure(Call<List<SimilarRecipeResponse>> call, Throwable t) {
listener.didError(t.getMessage());
}
});
}
public void getInstructions(InstructionsListener listener, int id){
CallInstructions callInstructions = retrofit.create(CallInstructions.class);
Call<List<InstructionsResponse>> call = callInstructions.callInstructions(id, context.getString(R.string.api_key));
call.enqueue(new Callback<List<InstructionsResponse>>() {
@Override
public void onResponse(Call<List<InstructionsResponse>> call, Response<List<InstructionsResponse>> response) {
if(!response.isSuccessful()) {
listener.didError(response.message());
return;
}
listener.didFetch(response.body(), response.message());
}
@Override
public void onFailure(Call<List<InstructionsResponse>> call, Throwable t) {
listener.didError(t.getMessage());
}
});
}
private interface CallRandomRecipes{
@GET("recipes/random")
Call<RandomRecipeApiResponse> callRandomRecipe(
@Query("apiKey") String apiKey,
@Query("number") String number,
@Query("tags") List<String> tags
);
}
private interface CallRecipeDetails{
@GET("recipes/{id}/information")
Call<RecipeDetailsResponse> callRecipeDetails(
@Path("id") int id,
@Query("apiKey") String apiKey
);
}
private interface CallSimilarRecipes{
@GET("recipes/{id}/similar")
Call<List<SimilarRecipeResponse>> callSimilarRecipe(
@Path("id") int id,
@Query("number") String number,
@Query("apiKey") String apiKey
);
}
private interface CallInstructions{
@GET("recipes/{id}/analyzedInstructions")
Call<List<InstructionsResponse>> callInstructions(
@Path("id") int id,
@Query("apiKey") String apiKey
);
}
}