Changed to github api Example
This commit is contained in:
parent
3db7b36662
commit
2c8205f5ca
@ -3,17 +3,25 @@ package com.baeldung.googlehttpclientguide;
|
|||||||
import com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler;
|
import com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler;
|
||||||
import com.google.api.client.http.HttpRequest;
|
import com.google.api.client.http.HttpRequest;
|
||||||
import com.google.api.client.http.HttpRequestFactory;
|
import com.google.api.client.http.HttpRequestFactory;
|
||||||
|
import com.google.api.client.http.HttpResponse;
|
||||||
import com.google.api.client.http.HttpResponseException;
|
import com.google.api.client.http.HttpResponseException;
|
||||||
import com.google.api.client.http.HttpTransport;
|
import com.google.api.client.http.HttpTransport;
|
||||||
|
import com.google.api.client.http.apache.ApacheHttpTransport;
|
||||||
import com.google.api.client.http.javanet.NetHttpTransport;
|
import com.google.api.client.http.javanet.NetHttpTransport;
|
||||||
import com.google.api.client.json.JsonFactory;
|
import com.google.api.client.json.JsonFactory;
|
||||||
import com.google.api.client.json.JsonObjectParser;
|
import com.google.api.client.json.JsonObjectParser;
|
||||||
import com.google.api.client.json.jackson2.JacksonFactory;
|
import com.google.api.client.json.jackson2.JacksonFactory;
|
||||||
import com.google.api.client.util.ExponentialBackOff;
|
import com.google.api.client.util.ExponentialBackOff;
|
||||||
|
import com.google.gson.reflect.TypeToken;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
|
||||||
public class DailyMotionExample {
|
public class GitHubExample {
|
||||||
|
|
||||||
static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
|
static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
|
||||||
|
//static final HttpTransport HTTP_TRANSPORT = new ApacheHttpTransport();
|
||||||
static final JsonFactory JSON_FACTORY = new JacksonFactory();
|
static final JsonFactory JSON_FACTORY = new JacksonFactory();
|
||||||
//static final JsonFactory JSON_FACTORY = new GsonFactory();
|
//static final JsonFactory JSON_FACTORY = new GsonFactory();
|
||||||
|
|
||||||
@ -23,8 +31,9 @@ public class DailyMotionExample {
|
|||||||
(HttpRequest request) -> {
|
(HttpRequest request) -> {
|
||||||
request.setParser(new JsonObjectParser(JSON_FACTORY));
|
request.setParser(new JsonObjectParser(JSON_FACTORY));
|
||||||
});
|
});
|
||||||
DailyMotionUrl url = new DailyMotionUrl("https://api.dailymotion.com/videos/");
|
GitHubUrl url = new GitHubUrl("https://api.github.com/users");
|
||||||
url.fields = "id,tags,title,url";
|
url.per_page = 10;
|
||||||
|
url.page = 1;
|
||||||
HttpRequest request = requestFactory.buildGetRequest(url);
|
HttpRequest request = requestFactory.buildGetRequest(url);
|
||||||
ExponentialBackOff backoff = new ExponentialBackOff.Builder()
|
ExponentialBackOff backoff = new ExponentialBackOff.Builder()
|
||||||
.setInitialIntervalMillis(500)
|
.setInitialIntervalMillis(500)
|
||||||
@ -34,37 +43,23 @@ public class DailyMotionExample {
|
|||||||
.setRandomizationFactor(0.5)
|
.setRandomizationFactor(0.5)
|
||||||
.build();
|
.build();
|
||||||
request.setUnsuccessfulResponseHandler(new HttpBackOffUnsuccessfulResponseHandler(backoff));
|
request.setUnsuccessfulResponseHandler(new HttpBackOffUnsuccessfulResponseHandler(backoff));
|
||||||
VideoFeed videoFeed = request.execute().parseAs(VideoFeed.class);
|
Type type = new TypeToken<List<User>>() {}.getType();
|
||||||
if (videoFeed.list.isEmpty()) {
|
List<User> users = (List<User>)request.execute().parseAs(type);
|
||||||
System.out.println("No videos found.");
|
System.out.println(users);
|
||||||
} else {
|
url.appendRawPath("/eugenp");
|
||||||
if (videoFeed.hasMore) {
|
request = requestFactory.buildGetRequest(url);
|
||||||
System.out.print("First ");
|
ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||||
}
|
Future<HttpResponse> responseFuture = request.executeAsync(executor);
|
||||||
System.out.println(videoFeed.list.size() + " videos found:");
|
User eugen = responseFuture.get().parseAs(User.class);
|
||||||
for (Video video : videoFeed.list) {
|
System.out.println(eugen);
|
||||||
System.out.println();
|
executor.shutdown();
|
||||||
System.out.println("-----------------------------------------------");
|
|
||||||
System.out.println("ID: " + video.id);
|
|
||||||
System.out.println("Title: " + video.title);
|
|
||||||
System.out.println("Tags: " + video.tags);
|
|
||||||
System.out.println("URL: " + video.url);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
try {
|
try {
|
||||||
try {
|
run();
|
||||||
run();
|
} catch (Exception e) {
|
||||||
return;
|
System.err.println(e.getMessage());
|
||||||
} catch (HttpResponseException e) {
|
|
||||||
System.err.println(e.getMessage());
|
|
||||||
}
|
|
||||||
} catch (Throwable t) {
|
|
||||||
t.printStackTrace();
|
|
||||||
}
|
}
|
||||||
System.exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -3,13 +3,16 @@ package com.baeldung.googlehttpclientguide;
|
|||||||
import com.google.api.client.http.GenericUrl;
|
import com.google.api.client.http.GenericUrl;
|
||||||
import com.google.api.client.util.Key;
|
import com.google.api.client.util.Key;
|
||||||
|
|
||||||
|
public class GitHubUrl extends GenericUrl{
|
||||||
|
|
||||||
public class DailyMotionUrl extends GenericUrl {
|
public GitHubUrl(String encodedUrl) {
|
||||||
|
|
||||||
public DailyMotionUrl(String encodedUrl) {
|
|
||||||
super(encodedUrl);
|
super(encodedUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Key
|
@Key
|
||||||
public String fields;
|
public int per_page;
|
||||||
|
|
||||||
|
@Key
|
||||||
|
public int page;
|
||||||
|
|
||||||
}
|
}
|
@ -0,0 +1,77 @@
|
|||||||
|
package com.baeldung.googlehttpclientguide;
|
||||||
|
|
||||||
|
import com.google.api.client.json.GenericJson;
|
||||||
|
import com.google.api.client.util.Key;
|
||||||
|
|
||||||
|
public class User extends GenericJson {
|
||||||
|
@Key
|
||||||
|
private String login;
|
||||||
|
@Key
|
||||||
|
private long id;
|
||||||
|
@Key
|
||||||
|
private String url;
|
||||||
|
@Key
|
||||||
|
private String company;
|
||||||
|
@Key
|
||||||
|
private String blog;
|
||||||
|
@Key
|
||||||
|
private String email;
|
||||||
|
|
||||||
|
@Key("subscriptions_url")
|
||||||
|
private String subscriptionsUrl;
|
||||||
|
|
||||||
|
public String getLogin() {
|
||||||
|
return login;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLogin(String login) {
|
||||||
|
this.login = login;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUrl() {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUrl(String url) {
|
||||||
|
this.url = url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCompany() {
|
||||||
|
return company;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCompany(String company) {
|
||||||
|
this.company = company;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getBlog() {
|
||||||
|
return blog;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBlog(String blog) {
|
||||||
|
this.blog = blog;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getEmail() {
|
||||||
|
return email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEmail(String email) {
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "User{" + "login=" + login + ", id=" + id + ", url=" + url + ", company=" + company + ", blog=" + blog + ", email=" + email + '}';
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -1,20 +0,0 @@
|
|||||||
package com.baeldung.googlehttpclientguide;
|
|
||||||
|
|
||||||
import com.google.api.client.json.GenericJson;
|
|
||||||
import com.google.api.client.util.Key;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class Video extends GenericJson {
|
|
||||||
|
|
||||||
@Key
|
|
||||||
public String id;
|
|
||||||
|
|
||||||
@Key
|
|
||||||
public List<String> tags;
|
|
||||||
|
|
||||||
@Key
|
|
||||||
public String title;
|
|
||||||
|
|
||||||
@Key
|
|
||||||
public String url;
|
|
||||||
}
|
|
@ -1,13 +0,0 @@
|
|||||||
package com.baeldung.googlehttpclientguide;
|
|
||||||
|
|
||||||
import com.google.api.client.util.Key;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class VideoFeed {
|
|
||||||
|
|
||||||
@Key
|
|
||||||
public List<Video> list;
|
|
||||||
|
|
||||||
@Key("has_more")
|
|
||||||
public boolean hasMore;
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user