Code Monkey home page Code Monkey logo

reactive-http's Introduction

Reactive Http

Reactive http is 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.getError(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);

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

Receive full http response

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

               }
        });

Receive response as string

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

               }
        });

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

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.1'

Bugs and Feedback

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

Inspired by

reactive-http's People

Contributors

lexer avatar matthewmichihara avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.