use of com.example.muzei.examplesource500px.FiveHundredPxService.PhotosResponse in project muzei by romannurik.
the class FiveHundredPxExampleArtSource method onTryUpdate.
@Override
protected void onTryUpdate(@UpdateReason int reason) throws RetryException {
String currentToken = (getCurrentArtwork() != null) ? getCurrentArtwork().getToken() : null;
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
@Override
public Response intercept(final Chain chain) throws IOException {
Request request = chain.request();
HttpUrl url = request.url().newBuilder().addQueryParameter("consumer_key", Config.CONSUMER_KEY).build();
request = request.newBuilder().url(url).build();
return chain.proceed(request);
}
}).build();
Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.500px.com/").client(okHttpClient).addConverterFactory(GsonConverterFactory.create()).build();
FiveHundredPxService service = retrofit.create(FiveHundredPxService.class);
PhotosResponse response;
try {
response = service.getPopularPhotos().execute().body();
} catch (IOException e) {
Log.w(TAG, "Error reading 500px response", e);
throw new RetryException();
}
if (response == null || response.photos == null) {
throw new RetryException();
}
if (response.photos.size() == 0) {
Log.w(TAG, "No photos returned from API.");
scheduleUpdate(System.currentTimeMillis() + ROTATE_TIME_MILLIS);
return;
}
Random random = new Random();
Photo photo;
String token;
while (true) {
photo = response.photos.get(random.nextInt(response.photos.size()));
token = Integer.toString(photo.id);
if (response.photos.size() <= 1 || !TextUtils.equals(token, currentToken)) {
break;
}
}
publishArtwork(new Artwork.Builder().title(photo.name).byline(photo.user.fullname).imageUri(Uri.parse(photo.image_url)).token(token).viewIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://500px.com/photo/" + photo.id))).build());
scheduleUpdate(System.currentTimeMillis() + ROTATE_TIME_MILLIS);
}
Aggregations