Reactive HTTP is a lightweight REST HTTP library with Observable(RxJava) interface.
ReactiveHttpClient client = new ReactiveHttpClient(new OkHttpClient(), new Gson(), Schedulers.executor(Executors.newFixedThreadPool(3), null, false);
client.create()
.post("https://api.bar.com/do/%s/%s", "abc", "cba")
.query("foo", "bar")
.set("Authorization", "foo:bar")
.data(new MyData(1, "2"))
.observe(MyResponse.class)
.subscribe(new Action1<MyResponse>() {
@Override
public void call(MyResponse response) {
}
},
new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
if (throwable instanceof HttpResponseException) {
HttpResponseException hre = (HttpResponseException) throwable;
GitHubError error = hre.getBodyAs(GithubApiError.class).message);
}
}
}
);
OkHttp has a problem that causes this crash on Android. The official workaround from Square is to do the following just after OkHttpClient instance is created:
OkHttpClient okHttpClient = new OkHttpClient();
URL.setURLStreamHandlerFactory(okHttpClient);
private HttpRequest createRequest() {
return client.create()
.set("Authorization", "foo:bar")
.set("Accept-Language", "en-US")
}
private Observable<Foo> requestFoo() {
return createRequest()
.get("https://api.bar.com/foo")
.observe(Foo.class);
}
client.create()
.get("https://api.bar.com/do/")
.observe()
.subscribe(new Action1<HttpResponse>() {
@Override
public void call(HttpResponse response) {
}
});
client.create()
.get("https://api.bar.com/do/")
.observeAsString()
.subscribe(new Action1<String>() {
@Override
public void call(String response) {
}
});
client.create()
.post("https://api.imgur.com/3/image")
.file("image/jpeg", file)
.set("Authorization", "Client-ID " + IMGUR_CLIENT_ID)
.observe(ImgurResponse.class)
.subscribe(new Action1<ImgurResponse>() {
@Override
public void call(ImgurResponse response) {
...
}
}
);
HttpRequest request = client.create()
.get("https://api.bar.com/do/");
HttpResponse response = request.execute();
Bar result = request.execute(Bar.class);
String str = request.executeAsString();
public class GithubApiError {
String message;
}
public class GithubException extends IOException {
private int status;
private GithubApiError error;
public GithubException(int status, GithubApiError error) {
this.status = status;
this.error = error;
}
public int getStatus() {
return status;
}
public GithubApiError getError() {
return error;
}
}
public class GithubErrorHandler implements ErrorHandler {
@Override
public Throwable handleError(HttpResponseException cause) {
GithubException ge = new GithubException(cause.getStatus(), cause.getBodyAs(GithubApiError.class));
return ge;
}
}
ReactiveHttpClient client = createClient();
client.setErrorHandler(new GithubErrorHandler());
client.create()
.get("https://api.github.com")
.set("Authorization", "foo")
.observe(Void.class)
.subscribe(new Action1<Void>() {
@Override
public void call(Void v) {
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable e) {
if (e instanceof GithubException) {
GithubException ge = (GithubException) e;
}
}
}
);
public class ConsoleLog implements HttpLog {
@Override
public void log(String message) {
for (int i = 0, len = message.length(); i < len; i += LOG_CHUNK_SIZE) {
int end = Math.min(len, i + LOG_CHUNK_SIZE);
System.out.println(message.substring(i, end));
}
}
}
// supply log class and set logging enabled param to true
ReactiveHttpClient client = new ReactiveHttpClient(new OkHttpClient(), new Gson(), Schedulers.currentThread(), new ConsoleLog(), true);
<dependency>
<groupId>com.lyft</groupId>
<artifactId>reactivehttp</artifactId>
<version>0.0.1</version>
</dependency>
compile 'com.lyft:reactivehttp:0.0.x'
For bugs, questions and discussions please use the Github Issues.
- Http Request by Kevin Sawicki
- Retrofit by Square
- Super Agent by TJ Holowaychuk