Merge pull request #2966 from hugosama1/googlehttpclient
BAEL-1260 A Guide to google-http-client
This commit is contained in:
commit
2518348493
|
@ -622,6 +622,21 @@
|
|||
<artifactId>bcpkix-jdk15on</artifactId>
|
||||
<version>1.58</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.http-client</groupId>
|
||||
<artifactId>google-http-client</artifactId>
|
||||
<version>${googleclient.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.http-client</groupId>
|
||||
<artifactId>google-http-client-jackson2</artifactId>
|
||||
<version>${googleclient.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.http-client</groupId>
|
||||
<artifactId>google-http-client-gson</artifactId>
|
||||
<version>${googleclient.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<repositories>
|
||||
<repository>
|
||||
|
@ -639,6 +654,7 @@
|
|||
</repository>
|
||||
</repositories>
|
||||
<properties>
|
||||
<googleclient.version>1.23.0</googleclient.version>
|
||||
<crdt.version>0.1.0</crdt.version>
|
||||
<multiverse.version>0.7.0</multiverse.version>
|
||||
<cglib.version>3.2.4</cglib.version>
|
||||
|
|
|
@ -0,0 +1,65 @@
|
|||
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 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();
|
||||
|
||||
private static void run() throws Exception {
|
||||
HttpRequestFactory requestFactory
|
||||
= HTTP_TRANSPORT.createRequestFactory(
|
||||
(HttpRequest request) -> {
|
||||
request.setParser(new JsonObjectParser(JSON_FACTORY));
|
||||
});
|
||||
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)
|
||||
.setMaxElapsedTimeMillis(900000)
|
||||
.setMaxIntervalMillis(6000)
|
||||
.setMultiplier(1.5)
|
||||
.setRandomizationFactor(0.5)
|
||||
.build();
|
||||
request.setUnsuccessfulResponseHandler(new HttpBackOffUnsuccessfulResponseHandler(backoff));
|
||||
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 {
|
||||
run();
|
||||
} catch (Exception e) {
|
||||
System.err.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.googlehttpclientguide;
|
||||
|
||||
import com.google.api.client.http.GenericUrl;
|
||||
import com.google.api.client.util.Key;
|
||||
|
||||
public class GitHubUrl extends GenericUrl{
|
||||
|
||||
public GitHubUrl(String encodedUrl) {
|
||||
super(encodedUrl);
|
||||
}
|
||||
|
||||
@Key
|
||||
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 + '}';
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
# Properties file which configures the operation of the JDK logging facility.
|
||||
# The system will look for this config file to be specified as a system property:
|
||||
# -Djava.util.logging.config.file=${project_loc:dailymotion-simple-cmdline-sample}/logging.properties
|
||||
|
||||
# Set up the console handler (uncomment "level" to show more fine-grained messages)
|
||||
handlers = java.util.logging.ConsoleHandler
|
||||
java.util.logging.ConsoleHandler.level = ALL
|
||||
|
||||
# Set up logging of HTTP requests and responses (uncomment "level" to show)
|
||||
com.google.api.client.http.level = ALL
|
Loading…
Reference in New Issue