Skip to content

Commit aabad6d

Browse files
committed
Added endpoints and functions to get collections
Added endpoints to get statistics
1 parent de95414 commit aabad6d

File tree

8 files changed

+319
-91
lines changed

8 files changed

+319
-91
lines changed

.idea/modules.xml

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

androidunsplash/src/main/java/com/kc/unsplash/Unsplash.java

+141-10
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
import android.support.annotation.Nullable;
55
import android.util.Log;
66

7+
import com.kc.unsplash.api.endpoints.CollectionsEndpointInterface;
8+
import com.kc.unsplash.api.endpoints.StatsEndpointInterface;
9+
import com.kc.unsplash.models.Collection;
710
import com.kc.unsplash.models.Photo;
811
import com.kc.unsplash.api.HeaderInterceptor;
912
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;
1115

1216
import java.util.List;
1317

@@ -23,7 +27,9 @@ public class Unsplash {
2327
public static final String BASE_URL = "https://api.unsplash.com/";
2428

2529
private String clientId;
26-
private UnsplashApiEndpointInterface apiService;
30+
private PhotosEndpointInterface photosApiService;
31+
private CollectionsEndpointInterface collectionsApiService;
32+
private StatsEndpointInterface statsApiService;
2733

2834
public Unsplash(String clientId) {
2935
this.clientId = clientId;
@@ -37,16 +43,18 @@ public Unsplash(String clientId) {
3743
.addConverterFactory(GsonConverterFactory.create())
3844
.build();
3945

40-
apiService = retrofit.create(UnsplashApiEndpointInterface.class);
46+
photosApiService = retrofit.create(PhotosEndpointInterface.class);
47+
collectionsApiService = retrofit.create(CollectionsEndpointInterface.class);
48+
statsApiService = retrofit.create(StatsEndpointInterface.class);
4149
}
4250

4351
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());
4553
call.enqueue(getMultiplePhotoCallback(listener));
4654
}
4755

4856
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());
5058
call.enqueue(getMultiplePhotoCallback(listener));
5159
}
5260

@@ -58,7 +66,7 @@ public void getRandomPhoto(@Nullable String collections,
5866
@Nullable Boolean featured, @Nullable String username,
5967
@Nullable String query, @Nullable Integer width,
6068
@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);
6270
call.enqueue(getSinglePhotoCallback(listener));
6371
}
6472

@@ -67,17 +75,17 @@ public void getRandomPhotos(@Nullable String collections,
6775
@Nullable String query, @Nullable Integer width,
6876
@Nullable Integer height, @Nullable String orientation,
6977
@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);
7179
call.enqueue(getMultiplePhotoCallback(listener));
7280
}
7381

7482
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);
7684
call.enqueue(getMultiplePhotoCallback(listener));
7785
}
7886

7987
public void getPhotoDownloadLink(@NonNull String id, final OnLinkLoadedListener listener){
80-
Call<String> call = apiService.getPhotoDownloadLink(id);
88+
Call<String> call = photosApiService.getPhotoDownloadLink(id);
8189
call.enqueue(new Callback<String>() {
8290
@Override
8391
public void onResponse(Call<String> call, Response<String> response) {
@@ -99,10 +107,74 @@ public void onFailure(Call<String> call, Throwable t) {
99107
}
100108

101109
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);
103111
call.enqueue(getSinglePhotoCallback(listener));
104112
}
105113

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+
106178
private Callback<Photo> getSinglePhotoCallback(final OnPhotoLoadedListener listener){
107179
return new Callback<Photo>() {
108180
@Override
@@ -145,6 +217,48 @@ public void onFailure(Call<List<Photo>> call, Throwable t) {
145217
};
146218
}
147219

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+
148262
public interface OnPhotosLoadedListener {
149263
public void onComplete(List<Photo> photos);
150264

@@ -164,4 +278,21 @@ public interface OnLinkLoadedListener {
164278
public void onError(String error);
165279
}
166280

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+
}
167298
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.kc.unsplash.api.endpoints;
2+
3+
import com.kc.unsplash.models.Collection;
4+
import com.kc.unsplash.models.Photo;
5+
6+
import java.util.List;
7+
8+
import retrofit2.Call;
9+
import retrofit2.http.GET;
10+
import retrofit2.http.Path;
11+
import retrofit2.http.Query;
12+
13+
public interface CollectionsEndpointInterface {
14+
15+
@GET("collections")
16+
Call<List<Collection>> getCollections(@Query("page") Integer page, @Query("per_page") Integer perPage);
17+
18+
@GET("collections/features")
19+
Call<List<Collection>> getFeaturedCollections(@Query("page") Integer page, @Query("per_page") Integer perPage);
20+
21+
@GET("collections/curated")
22+
Call<List<Collection>> getCuratedCollections(@Query("page") Integer page, @Query("per_page") Integer perPage);
23+
24+
@GET("collections/{id}")
25+
Call<Collection> getCollection(@Path("id") String id);
26+
27+
@GET("collections/curated/{id}")
28+
Call<Collection> getCuratedCollection(@Path("id") String id);
29+
30+
@GET("collections/{id}/photos")
31+
Call<List<Photo>> getCollectionPhotos(@Path("id") String id, @Query("page") Integer page, @Query("per_page") Integer perPage);
32+
33+
@GET("collections/curated/{id}/photos")
34+
Call<List<Photo>> getCuratedCollectionPhotos(@Path("id") String id, @Query("page") Integer page, @Query("per_page") Integer perPage);
35+
36+
@GET("collections/{id}/related")
37+
Call<List<Collection>> getRelatedCollections(@Path("id") String id);
38+
39+
}

androidunsplash/src/main/java/com/kc/unsplash/api/UnsplashApiEndpointInterface.java renamed to androidunsplash/src/main/java/com/kc/unsplash/api/endpoints/PhotosEndpointInterface.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.kc.unsplash.api;
1+
package com.kc.unsplash.api.endpoints;
22

33
import com.kc.unsplash.models.Photo;
44

@@ -9,7 +9,7 @@
99
import retrofit2.http.Path;
1010
import retrofit2.http.Query;
1111

12-
public interface UnsplashApiEndpointInterface {
12+
public interface PhotosEndpointInterface {
1313

1414
@GET("photos/{id}")
1515
Call<Photo> getPhoto(@Path("id") String id, @Query("w") Integer width, @Query("h") Integer height);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.kc.unsplash.api.endpoints;
2+
3+
import com.kc.unsplash.models.Stats;
4+
5+
import retrofit2.Call;
6+
import retrofit2.http.GET;
7+
import retrofit2.http.Query;
8+
9+
public interface StatsEndpointInterface {
10+
11+
@GET("stats/total")
12+
Call<Stats> getStats();
13+
14+
}

0 commit comments

Comments
 (0)