Giter Club home page Giter Club logo

simple-rest-client's Introduction

Simple Rest Client

Introduction

Simple Rest Client is an experiment about a simple rest client with caching support. By using reflection and a proxy, it allows the user to use a json rest service by simply writing a java interface. It only supports json as data exchange format (by using Gson), and uses OkHttp as HTTP client. You can use it also on Android.

How to use it

  1. Declare a java interface
@RestService("http://www.mybookstore.example.com/rest")
public interface BookStoreService {    
    @GET
    @Path("books/{ISBN}")
    @Cache
    Book getBook(@PathParam("ISBN") String ISBN);
    
    @GET
    @Path("books/{ISBN}/reviews")
    ReviewSummary[] getRewiewsSummary(@PathParam("ISBN") String ISBN);
    
    @GET
    @Path("books/{ISBN}/reviews")
    ReviewSummary[] getRewiewsSummary(@PathParam("ISBN") String ISBN,
                                      @QueryParam("from") int from,
                                      @QueryParam("to") int to);
    
    @GET
    @Path("books/{ISBN}/reviews/{ID}")
    Review getReview(@PathParam("ISBN") String ISBN,
                     @PathParam("ID") String reviewID);
    
    @POST
    @Path("books/{ISBN}/reviews/create")
    Review postReview(@HeaderParam("Auth-Token") String token,
                      @PathParam("ISBN") String ISBN,
                      Review review);
}
  1. Use the RestClientFactory to instantiate the client
BookStoreService service = RestClientFactory.createClient(BookStoreService.class);
  1. Call the service methods
  Book book = service.getBook("978-0123745149");
  ...
  ReviewSummary[] summaries = service.getReviewsSummary("978-0321563842", 10, 20);
  ...
  Review review = service.getReview("978-0321349606", "d52387880");
  ...
  Review myReview = ...
  Review result = service.postReview(token, "978-0201633610", myReview);
  if (result != null) {
    ...
  }
  1. Eventually change client properties
  RestClientInterface restClient = (RestClientInterface) service;
  restClient.setBaseUrl("http://myotherbookstore.example.com/services");

TODO

  1. Callback mechanism for exceptions thrown inside the service implementation
  2. Callback mechanism to handle HTTP responses not equals to 200
  3. OAuth support
  4. Add xml data exchange format
  5. Add compile time class generation to prevent the use of a proxy and the massive reflection usage (it sounds funny, probably it will go first)

simple-rest-client's People

Contributors

spacifici avatar

Watchers

 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.