4
4
import android .support .annotation .Nullable ;
5
5
import android .util .Log ;
6
6
7
+ import com .kc .unsplash .api .endpoints .CollectionsEndpointInterface ;
8
+ import com .kc .unsplash .api .endpoints .StatsEndpointInterface ;
9
+ import com .kc .unsplash .models .Collection ;
7
10
import com .kc .unsplash .models .Photo ;
8
11
import com .kc .unsplash .api .HeaderInterceptor ;
9
12
import com .kc .unsplash .api .Order ;
10
- import com .kc .unsplash .api .UnsplashApiEndpointInterface ;
13
+ import com .kc .unsplash .api .endpoints .PhotosEndpointInterface ;
14
+ import com .kc .unsplash .models .Stats ;
11
15
12
16
import java .util .List ;
13
17
@@ -23,7 +27,9 @@ public class Unsplash {
23
27
public static final String BASE_URL = "https://api.unsplash.com/" ;
24
28
25
29
private String clientId ;
26
- private UnsplashApiEndpointInterface apiService ;
30
+ private PhotosEndpointInterface photosApiService ;
31
+ private CollectionsEndpointInterface collectionsApiService ;
32
+ private StatsEndpointInterface statsApiService ;
27
33
28
34
public Unsplash (String clientId ) {
29
35
this .clientId = clientId ;
@@ -37,16 +43,18 @@ public Unsplash(String clientId) {
37
43
.addConverterFactory (GsonConverterFactory .create ())
38
44
.build ();
39
45
40
- apiService = retrofit .create (UnsplashApiEndpointInterface .class );
46
+ photosApiService = retrofit .create (PhotosEndpointInterface .class );
47
+ collectionsApiService = retrofit .create (CollectionsEndpointInterface .class );
48
+ statsApiService = retrofit .create (StatsEndpointInterface .class );
41
49
}
42
50
43
51
public void getPhotos (Integer page , Integer perPage , Order order , final OnPhotosLoadedListener listener ){
44
- Call <List <Photo >> call = apiService .getPhotos (page , perPage , order .getOrder ());
52
+ Call <List <Photo >> call = photosApiService .getPhotos (page , perPage , order .getOrder ());
45
53
call .enqueue (getMultiplePhotoCallback (listener ));
46
54
}
47
55
48
56
public void getCuratedPhotos (Integer page , Integer perPage , Order order , final OnPhotosLoadedListener listener ){
49
- Call <List <Photo >> call = apiService .getCuratedPhotos (page , perPage , order .getOrder ());
57
+ Call <List <Photo >> call = photosApiService .getCuratedPhotos (page , perPage , order .getOrder ());
50
58
call .enqueue (getMultiplePhotoCallback (listener ));
51
59
}
52
60
@@ -58,7 +66,7 @@ public void getRandomPhoto(@Nullable String collections,
58
66
@ Nullable Boolean featured , @ Nullable String username ,
59
67
@ Nullable String query , @ Nullable Integer width ,
60
68
@ Nullable Integer height , @ Nullable String orientation , OnPhotoLoadedListener listener ){
61
- Call <Photo > call = apiService .getRandomPhoto (collections , featured , username , query , width , height , orientation );
69
+ Call <Photo > call = photosApiService .getRandomPhoto (collections , featured , username , query , width , height , orientation );
62
70
call .enqueue (getSinglePhotoCallback (listener ));
63
71
}
64
72
@@ -67,17 +75,17 @@ public void getRandomPhotos(@Nullable String collections,
67
75
@ Nullable String query , @ Nullable Integer width ,
68
76
@ Nullable Integer height , @ Nullable String orientation ,
69
77
@ Nullable Integer count , OnPhotosLoadedListener listener ){
70
- Call <List <Photo >> call = apiService .getRandomPhotos (collections , featured , username , query , width , height , orientation , count );
78
+ Call <List <Photo >> call = photosApiService .getRandomPhotos (collections , featured , username , query , width , height , orientation , count );
71
79
call .enqueue (getMultiplePhotoCallback (listener ));
72
80
}
73
81
74
82
public void searchPhotos (@ NonNull String query , @ Nullable Integer page , @ Nullable Integer perPage , OnPhotosLoadedListener listener ){
75
- Call <List <Photo >> call = apiService .searchPhotos (query , page , perPage );
83
+ Call <List <Photo >> call = photosApiService .searchPhotos (query , page , perPage );
76
84
call .enqueue (getMultiplePhotoCallback (listener ));
77
85
}
78
86
79
87
public void getPhotoDownloadLink (@ NonNull String id , final OnLinkLoadedListener listener ){
80
- Call <String > call = apiService .getPhotoDownloadLink (id );
88
+ Call <String > call = photosApiService .getPhotoDownloadLink (id );
81
89
call .enqueue (new Callback <String >() {
82
90
@ Override
83
91
public void onResponse (Call <String > call , Response <String > response ) {
@@ -99,10 +107,74 @@ public void onFailure(Call<String> call, Throwable t) {
99
107
}
100
108
101
109
public void getPhoto (@ NonNull String id , @ Nullable Integer width , @ Nullable Integer height , final OnPhotoLoadedListener listener ){
102
- Call <Photo > call = apiService .getPhoto (id , width , height );
110
+ Call <Photo > call = photosApiService .getPhoto (id , width , height );
103
111
call .enqueue (getSinglePhotoCallback (listener ));
104
112
}
105
113
114
+ public void getCollections (Integer page , Integer perPage , final OnCollectionsLoadedListener listener ){
115
+ Call <List <Collection >> call = collectionsApiService .getCollections (page , perPage );
116
+ call .enqueue (getMultipleCollectionsCallback (listener ));
117
+ }
118
+
119
+ public void getFeaturedCollections (Integer page , Integer perPage , final OnCollectionsLoadedListener listener ){
120
+ Call <List <Collection >> call = collectionsApiService .getFeaturedCollections (page , perPage );
121
+ call .enqueue (getMultipleCollectionsCallback (listener ));
122
+ }
123
+
124
+ public void getCuratedCollections (Integer page , Integer perPage , final OnCollectionsLoadedListener listener ){
125
+ Call <List <Collection >> call = collectionsApiService .getCuratedCollections (page , perPage );
126
+ call .enqueue (getMultipleCollectionsCallback (listener ));
127
+ }
128
+
129
+ public void getRelatedCollections (String id , final OnCollectionsLoadedListener listener ){
130
+ Call <List <Collection >> call = collectionsApiService .getRelatedCollections (id );
131
+ call .enqueue (getMultipleCollectionsCallback (listener ));
132
+ }
133
+
134
+ public void getCollection (String id , final OnCollectionLoadedListener listener ){
135
+ Call <Collection > call = collectionsApiService .getCollection (id );
136
+ call .enqueue (getSingleCollectionCallback (listener ));
137
+ }
138
+
139
+ public void getCuratedCollection (String id , final OnCollectionLoadedListener listener ){
140
+ Call <Collection > call = collectionsApiService .getCuratedCollection (id );
141
+ call .enqueue (getSingleCollectionCallback (listener ));
142
+ }
143
+
144
+ public void getCollectionPhotos (String id , Integer page , Integer perPage , final OnPhotosLoadedListener listener ){
145
+ Call <List <Photo >> call = collectionsApiService .getCollectionPhotos (id , page , perPage );
146
+ call .enqueue (getMultiplePhotoCallback (listener ));
147
+ }
148
+
149
+ public void getCuratedCollectionPhotos (String id , Integer page , Integer perPage , final OnPhotosLoadedListener listener ){
150
+ Call <List <Photo >> call = collectionsApiService .getCuratedCollectionPhotos (id , page , perPage );
151
+ call .enqueue (getMultiplePhotoCallback (listener ));
152
+ }
153
+
154
+ public void getStats (final OnStatsLoadedListener listener ){
155
+ Call <Stats > call = statsApiService .getStats ();
156
+ call .enqueue (new Callback <Stats >() {
157
+ @ Override
158
+ public void onResponse (Call <Stats > call , Response <Stats > response ) {
159
+ int statusCode = response .code ();
160
+ if (statusCode == 200 ) {
161
+ listener .onComplete (response .body ());
162
+ }
163
+ else if (statusCode == 401 ) {
164
+ Log .d ("Unsplash" , "Unauthorized, Check your client Id" );
165
+ }
166
+ }
167
+
168
+ @ Override
169
+ public void onFailure (Call <Stats > call , Throwable t ) {
170
+ listener .onError (t .getMessage ());
171
+ }
172
+ }
173
+ );
174
+ }
175
+
176
+ // CALLBACKS
177
+
106
178
private Callback <Photo > getSinglePhotoCallback (final OnPhotoLoadedListener listener ){
107
179
return new Callback <Photo >() {
108
180
@ Override
@@ -145,6 +217,48 @@ public void onFailure(Call<List<Photo>> call, Throwable t) {
145
217
};
146
218
}
147
219
220
+ private Callback <Collection > getSingleCollectionCallback (final OnCollectionLoadedListener listener ){
221
+ return new Callback <Collection >() {
222
+ @ Override
223
+ public void onResponse (Call <Collection > call , Response <Collection > response ) {
224
+ int statusCode = response .code ();
225
+ if (statusCode == 200 ) {
226
+ Collection collections = response .body ();
227
+ listener .onComplete (collections );
228
+ }
229
+ else if (statusCode == 401 ) {
230
+ Log .d ("Unsplash" , "Unauthorized, Check your client Id" );
231
+ }
232
+ }
233
+
234
+ @ Override
235
+ public void onFailure (Call <Collection > call , Throwable t ) {
236
+ listener .onError (t .getMessage ());
237
+ }
238
+ };
239
+ }
240
+
241
+ private Callback <List <Collection >> getMultipleCollectionsCallback (final OnCollectionsLoadedListener listener ){
242
+ return new Callback <List <Collection >>() {
243
+ @ Override
244
+ public void onResponse (Call <List <Collection >> call , Response <List <Collection >> response ) {
245
+ int statusCode = response .code ();
246
+ if (statusCode == 200 ) {
247
+ List <Collection > collections = response .body ();
248
+ listener .onComplete (collections );
249
+ }
250
+ else if (statusCode == 401 ) {
251
+ Log .d ("Unsplash" , "Unauthorized, Check your client Id" );
252
+ }
253
+ }
254
+
255
+ @ Override
256
+ public void onFailure (Call <List <Collection >> call , Throwable t ) {
257
+ listener .onError (t .getMessage ());
258
+ }
259
+ };
260
+ }
261
+
148
262
public interface OnPhotosLoadedListener {
149
263
public void onComplete (List <Photo > photos );
150
264
@@ -164,4 +278,21 @@ public interface OnLinkLoadedListener {
164
278
public void onError (String error );
165
279
}
166
280
281
+ public interface OnCollectionsLoadedListener {
282
+ public void onComplete (List <Collection > collections );
283
+
284
+ public void onError (String error );
285
+ }
286
+
287
+ public interface OnCollectionLoadedListener {
288
+ public void onComplete (Collection photos );
289
+
290
+ public void onError (String error );
291
+ }
292
+
293
+ public interface OnStatsLoadedListener {
294
+ public void onComplete (Stats stats );
295
+
296
+ public void onError (String error );
297
+ }
167
298
}
0 commit comments