mariomosca / reactive-http

RxJava HTTP Client for Android and Java application

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Reactive HTTP

Reactive HTTP is a lightweight REST HTTP library with Observable(RxJava) interface.

Chainable API

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

Fix Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1) on Android

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);

Add common headers

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

Send HTTP request, with HttpResponse callback.

client.create()
        .get("https://api.bar.com/do/")
        .observe()
        .subscribe(new Action1<HttpResponse>() {
               @Override
               public void call(HttpResponse response) {

               }
        });

Send HTTP request, with String callback.

client.create()
        .get("https://api.bar.com/do/")
        .observeAsString()
        .subscribe(new Action1<String>() {
               @Override
               public void call(String response) {

               }
        });

Upload a file

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) {
                          ...
                       }
                   }
        );

Sync execution

HttpRequest request = client.create()
        .get("https://api.bar.com/do/");

HttpResponse response = request.execute();
Bar result = request.execute(Bar.class);
String str = request.executeAsString();

Custom error handler

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

Logging

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);

Maven

<dependency>
    <groupId>com.lyft</groupId>
    <artifactId>reactivehttp</artifactId>
    <version>0.0.1</version>
</dependency>

Android studio

compile 'com.lyft:reactivehttp:0.0.x'

Bugs and Feedback

For bugs, questions and discussions please use the Github Issues.

Inspired by

About

RxJava HTTP Client for Android and Java application


Languages

Language:Java 100.0%