Changed to github api Example

This commit is contained in:
hgomez 2017-11-13 00:00:07 -05:00
parent 3db7b36662
commit 2c8205f5ca
5 changed files with 112 additions and 70 deletions

View File

@ -3,17 +3,25 @@ package com.baeldung.googlehttpclientguide;
import com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler;
import com.google.api.client.http.HttpRequest;
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.HttpTransport;
import com.google.api.client.http.apache.ApacheHttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.JsonObjectParser;
import com.google.api.client.json.jackson2.JacksonFactory;
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 ApacheHttpTransport();
static final JsonFactory JSON_FACTORY = new JacksonFactory();
//static final JsonFactory JSON_FACTORY = new GsonFactory();
@ -23,8 +31,9 @@ public class DailyMotionExample {
(HttpRequest request) -> {
request.setParser(new JsonObjectParser(JSON_FACTORY));
});
DailyMotionUrl url = new DailyMotionUrl("https://api.dailymotion.com/videos/");
url.fields = "id,tags,title,url";
GitHubUrl url = new GitHubUrl("https://api.github.com/users");
url.per_page = 10;
url.page = 1;
HttpRequest request = requestFactory.buildGetRequest(url);
ExponentialBackOff backoff = new ExponentialBackOff.Builder()
.setInitialIntervalMillis(500)
@ -34,37 +43,23 @@ public class DailyMotionExample {
.setRandomizationFactor(0.5)
.build();
request.setUnsuccessfulResponseHandler(new HttpBackOffUnsuccessfulResponseHandler(backoff));
VideoFeed videoFeed = request.execute().parseAs(VideoFeed.class);
if (videoFeed.list.isEmpty()) {
System.out.println("No videos found.");
} else {
if (videoFeed.hasMore) {
System.out.print("First ");
}
System.out.println(videoFeed.list.size() + " videos found:");
for (Video video : videoFeed.list) {
System.out.println();
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);
}
}
Type type = new TypeToken<List<User>>() {}.getType();
List<User> users = (List<User>)request.execute().parseAs(type);
System.out.println(users);
url.appendRawPath("/eugenp");
request = requestFactory.buildGetRequest(url);
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<HttpResponse> responseFuture = request.executeAsync(executor);
User eugen = responseFuture.get().parseAs(User.class);
System.out.println(eugen);
executor.shutdown();
}
public static void main(String[] args) {
try {
try {
run();
return;
} catch (HttpResponseException e) {
System.err.println(e.getMessage());
}
} catch (Throwable t) {
t.printStackTrace();
run();
} catch (Exception e) {
System.err.println(e.getMessage());
}
System.exit(1);
}
}

View File

@ -3,13 +3,16 @@ package com.baeldung.googlehttpclientguide;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.util.Key;
public class GitHubUrl extends GenericUrl{
public class DailyMotionUrl extends GenericUrl {
public DailyMotionUrl(String encodedUrl) {
public GitHubUrl(String encodedUrl) {
super(encodedUrl);
}
}
@Key
public String fields;
public int per_page;
@Key
public int page;
}

View File

@ -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 + '}';
}
}

View File

@ -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;
}

View File

@ -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;
}