Skip to content

Commit 5d65733

Browse files
committed
Fixed bug when searching api
Updated readme Updated demo app
1 parent aabad6d commit 5d65733

File tree

12 files changed

+539
-36
lines changed

12 files changed

+539
-36
lines changed

README.md

+42-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ An unofficial Unsplash API library for Android
33

44
## Usage
55

6-
### Get A List of Photos
6+
### Initialize Unsplash Client
77
~~~~~
88
Unsplash unsplash = new Unsplash(YOUR_CLIENT_ID);
9+
~~~~~
10+
You can sign up for a client id at <https://unsplash.com/developers>
11+
12+
### Get A List of Photos
13+
~~~~~
914
unsplash.getPhotos(1, 10, Order.LATEST, new Unsplash.OnPhotosLoadedListener() {
1015
@Override
1116
public void onComplete(List<Photo> photos) {
@@ -21,7 +26,6 @@ unsplash.getPhotos(1, 10, Order.LATEST, new Unsplash.OnPhotosLoadedListener() {
2126

2227
### Get A Photo By Id
2328
~~~~~
24-
Unsplash unsplash = new Unsplash(YOUR_CLIENT_ID);
2529
unsplash.getPhoto(PHOTO_ID, new Unsplash.OnPhotoLoadedListener() {
2630
@Override
2731
public void onComplete(Photo photo) {
@@ -35,6 +39,40 @@ unsplash.getPhoto(PHOTO_ID, new Unsplash.OnPhotoLoadedListener() {
3539
});
3640
~~~~~
3741

42+
### Search
43+
~~~~~
44+
unsplash.searchPhotos(query, new Unsplash.OnSearchCompleteListener() {
45+
@Override
46+
public void onComplete(SearchResults results) {
47+
Log.d("Photos", "Total Results Found " + results.getTotal());
48+
List<Photo> photos = results.getResults();
49+
}
50+
51+
@Override
52+
public void onError(String error) {
53+
Log.d("Unsplash", error);
54+
}
55+
});
56+
~~~~~
57+
58+
## Other Features
59+
~~~~~
60+
getCuratedPhotos()
61+
getRandomPhoto()
62+
getRandomPhotos()
63+
getPhotoDownloadLink
64+
getCollections()
65+
getFeaturedCollections()
66+
getCuratedCollections()
67+
getRelatedCollections()
68+
getCollection()
69+
getCuratedCollection()
70+
getCollectionPhotos()
71+
getCuratedCollectionPhotos()
72+
getStats()
73+
~~~~~
74+
75+
Take a look at the API documentation for what each call does in detail <https://unsplash.com/documentation>
76+
3877
## To-Do
39-
+ Statistics
40-
+ Actions that require user authentication
78+
+ User Authentication

androidunsplash/build.gradle

+27
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,30 @@ dependencies {
2828
compile 'com.squareup.retrofit2:converter-gson:2.2.0'
2929
compile 'com.android.support:appcompat-v7:25.3.1'
3030
}
31+
32+
ext {
33+
bintrayRepo = 'android-unsplash'
34+
bintrayName = 'android-unsplash'
35+
36+
publishedGroupId = 'com.kc.androidunsplash'
37+
libraryName = 'androidunsplash'
38+
artifact = 'androidunsplash'
39+
40+
libraryDescription = 'An unofficial library to access the Unsplash API'
41+
42+
siteUrl = 'https://github.com/KeenenCharles/AndroidUnplash'
43+
gitUrl = 'https://github.com/KeenenCharles/AndroidUnplash.git'
44+
45+
libraryVersion = '0.1.0'
46+
47+
developerId = 'keenencharles'
48+
developerName = 'Keenen Charles'
49+
developerEmail = 'keenencharles@gmail.com'
50+
51+
licenseName = 'MIT License'
52+
licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
53+
allLicenses = ["MIT"]
54+
}
55+
56+
apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle'
57+
apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/bintrayv1.gradle'

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

+54-13
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.kc.unsplash.api.HeaderInterceptor;
1212
import com.kc.unsplash.api.Order;
1313
import com.kc.unsplash.api.endpoints.PhotosEndpointInterface;
14+
import com.kc.unsplash.models.SearchResults;
1415
import com.kc.unsplash.models.Stats;
1516

1617
import java.util.List;
@@ -30,6 +31,7 @@ public class Unsplash {
3031
private PhotosEndpointInterface photosApiService;
3132
private CollectionsEndpointInterface collectionsApiService;
3233
private StatsEndpointInterface statsApiService;
34+
private String TAG = "Unsplash";
3335

3436
public Unsplash(String clientId) {
3537
this.clientId = clientId;
@@ -79,9 +81,13 @@ public void getRandomPhotos(@Nullable String collections,
7981
call.enqueue(getMultiplePhotoCallback(listener));
8082
}
8183

82-
public void searchPhotos(@NonNull String query, @Nullable Integer page, @Nullable Integer perPage, OnPhotosLoadedListener listener){
83-
Call<List<Photo>> call = photosApiService.searchPhotos(query, page, perPage);
84-
call.enqueue(getMultiplePhotoCallback(listener));
84+
public void searchPhotos(@NonNull String query, OnSearchCompleteListener listener){
85+
searchPhotos(query, null, null, listener);
86+
}
87+
88+
public void searchPhotos(@NonNull String query, @Nullable Integer page, @Nullable Integer perPage, OnSearchCompleteListener listener){
89+
Call<SearchResults> call = photosApiService.searchPhotos(query, page, perPage);
90+
call.enqueue(getSearchResultsCallback(listener));
8591
}
8692

8793
public void getPhotoDownloadLink(@NonNull String id, final OnLinkLoadedListener listener){
@@ -94,7 +100,7 @@ public void onResponse(Call<String> call, Response<String> response) {
94100
listener.onComplete(response.body());
95101
}
96102
else if(statusCode == 401) {
97-
Log.d("Unsplash", "Unauthorized, Check your client Id");
103+
Log.d(TAG, "Unauthorized, Check your client Id");
98104
}
99105
}
100106

@@ -157,11 +163,12 @@ public void getStats(final OnStatsLoadedListener listener){
157163
@Override
158164
public void onResponse(Call<Stats> call, Response<Stats> response) {
159165
int statusCode = response.code();
166+
Log.d(TAG, "Status Code = " + statusCode);
160167
if(statusCode == 200) {
161168
listener.onComplete(response.body());
162169
}
163170
else if(statusCode == 401) {
164-
Log.d("Unsplash", "Unauthorized, Check your client Id");
171+
Log.d(TAG, "Unauthorized, Check your client Id");
165172
}
166173
}
167174

@@ -180,12 +187,13 @@ private Callback<Photo> getSinglePhotoCallback(final OnPhotoLoadedListener liste
180187
@Override
181188
public void onResponse(Call<Photo> call, Response<Photo> response) {
182189
int statusCode = response.code();
190+
Log.d(TAG, "Status Code = " + statusCode);
183191
if(statusCode == 200) {
184192
Photo photo = response.body();
185193
listener.onComplete(photo);
186194
}
187195
else if(statusCode == 401) {
188-
Log.d("Unsplash", "Unauthorized, Check your client Id");
196+
Log.d(TAG, "Unauthorized, Check your client Id");
189197
}
190198
}
191199

@@ -201,17 +209,20 @@ private Callback<List<Photo>> getMultiplePhotoCallback(final OnPhotosLoadedListe
201209
@Override
202210
public void onResponse(Call<List<Photo>> call, Response<List<Photo>> response) {
203211
int statusCode = response.code();
212+
Log.d(TAG, "Url = " + call.request().url());
213+
Log.d(TAG, "Status Code = " + statusCode);
204214
if(statusCode == 200) {
205215
List<Photo> photos = response.body();
206216
listener.onComplete(photos);
207217
}
208218
else if(statusCode == 401) {
209-
Log.d("Unsplash", "Unauthorized, Check your client Id");
219+
Log.d(TAG, "Unauthorized, Check your client Id");
210220
}
211221
}
212222

213223
@Override
214224
public void onFailure(Call<List<Photo>> call, Throwable t) {
225+
Log.d(TAG, "Url = " + call.request().url());
215226
listener.onError(t.getMessage());
216227
}
217228
};
@@ -222,12 +233,13 @@ private Callback<Collection> getSingleCollectionCallback(final OnCollectionLoade
222233
@Override
223234
public void onResponse(Call<Collection> call, Response<Collection> response) {
224235
int statusCode = response.code();
236+
Log.d(TAG, "Status Code = " + statusCode);
225237
if(statusCode == 200) {
226238
Collection collections = response.body();
227239
listener.onComplete(collections);
228240
}
229241
else if(statusCode == 401) {
230-
Log.d("Unsplash", "Unauthorized, Check your client Id");
242+
Log.d(TAG, "Unauthorized, Check your client Id");
231243
}
232244
}
233245

@@ -238,17 +250,40 @@ public void onFailure(Call<Collection> call, Throwable t) {
238250
};
239251
}
240252

253+
private Callback<SearchResults> getSearchResultsCallback(final OnSearchCompleteListener listener){
254+
return new Callback<SearchResults>() {
255+
@Override
256+
public void onResponse(Call<SearchResults> call, Response<SearchResults> response) {
257+
int statusCode = response.code();
258+
Log.d(TAG, "Status Code = " + statusCode);
259+
if(statusCode == 200) {
260+
SearchResults results = response.body();
261+
listener.onComplete(results);
262+
}
263+
else if(statusCode == 401) {
264+
Log.d(TAG, "Unauthorized, Check your client Id");
265+
}
266+
}
267+
268+
@Override
269+
public void onFailure(Call<SearchResults> call, Throwable t) {
270+
listener.onError(t.getMessage());
271+
}
272+
};
273+
}
274+
241275
private Callback<List<Collection>> getMultipleCollectionsCallback(final OnCollectionsLoadedListener listener){
242276
return new Callback<List<Collection>>() {
243277
@Override
244278
public void onResponse(Call<List<Collection>> call, Response<List<Collection>> response) {
245279
int statusCode = response.code();
280+
Log.d(TAG, "Status Code = " + statusCode);
246281
if(statusCode == 200) {
247282
List<Collection> collections = response.body();
248283
listener.onComplete(collections);
249284
}
250285
else if(statusCode == 401) {
251-
Log.d("Unsplash", "Unauthorized, Check your client Id");
286+
Log.d(TAG, "Unauthorized, Check your client Id");
252287
}
253288
}
254289

@@ -260,15 +295,21 @@ public void onFailure(Call<List<Collection>> call, Throwable t) {
260295
}
261296

262297
public interface OnPhotosLoadedListener {
263-
public void onComplete(List<Photo> photos);
298+
void onComplete(List<Photo> photos);
264299

265-
public void onError(String error);
300+
void onError(String error);
301+
}
302+
303+
public interface OnSearchCompleteListener {
304+
void onComplete(SearchResults results);
305+
306+
void onError(String error);
266307
}
267308

268309
public interface OnPhotoLoadedListener {
269-
public void onComplete(Photo photo);
310+
void onComplete(Photo photo);
270311

271-
public void onError(String error);
312+
void onError(String error);
272313
}
273314

274315
public interface OnLinkLoadedListener {

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

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

33
import com.kc.unsplash.models.Photo;
4+
import com.kc.unsplash.models.SearchResults;
45

56
import java.util.List;
67

@@ -39,5 +40,5 @@ Call<List<Photo>> getRandomPhotos(@Query("collections") String collections,
3940
Call<String> getPhotoDownloadLink(@Path("id") String id);
4041

4142
@GET("search/photos")
42-
Call<List<Photo>> searchPhotos(@Query("query") String query, @Query("page") Integer page, @Query("per_page") Integer perPage);
43+
Call<SearchResults> searchPhotos(@Query("query") String query, @Query("page") Integer page, @Query("per_page") Integer perPage);
4344
}

0 commit comments

Comments
 (0)