GithubHelp home page GithubHelp logo

bowtie's Introduction

bowtie

A REST client library providing a declarative API to create common REST clients using Hystrix and Ribbon.

It provides the following features:

  • Can be used in a Netflix-OSS platform or standalone.
  • Annotation based declarative clients
  • Consistent configuration using Archaius
  • Supports JSON serialization via Jackson
  • Caching using Guava Cache or Memcache

Getting started

NOTE: Assumes understanding of Archaius

Create a client as an interface

public interface UserClient {

    @Http(
        method = Verb.GET,
        uriTemplate = "/user/{username}"
    )
    public User getUser(@Path("username") String name);
}

Use the builder to configure the instance

final RestAdapter restAdapter = RestAdapter.getNamedAdapter("user-client");

Create an instance and start using

final UserClient userClient = restAdapter.create(UserClient.class);
final User user = fakeClient.getUser("jdoe");

See the FakeClient class in the tests for sample calls.

If method and parameter annotations are used, runtime annotations override the static values.

Example:

public interface AnotherClient {

    @Http(
        method = Verb.GET,
        uriTemplate = "/pet/{guid}",
        headers = {
            @Header(name="X-SESSION-ID", value="55892d6d-77df-4617-b728-6f5de97f5752")
        }
    )
    public User getPet(@Header(name="X-SESSION-ID") String sessionId, @Path("guid") String guid);
}

In the above example the value of sessionId passed into the request will be used, not the value from the method annoation.

#Configuration Use the RestAdapterConfig to configure the RestAdapter.

 final RestCache cache = GuavaRestCache.newDefaultCache();
 final RestAdapter restAdapter = RestAdapter.getNamedAdapter("user-client", RestAdapterConfig.custom()
   .withMessageSerializer(new JacksonMessageSerializer())
   .withRestCache(cache)
   .withEncoding(Encoding.gzip)
   .build());

MessageSerializers

Use MessageSerializers to control how the request and responses are serialized.

See: https://github.com/kenzanmedia/bowtie/issues/15 (May be changed later).

Encoding

Adding gzip encoding will tell Jersey to use the GZIPContentEncodingFilter for the request. Adding the Accept-Encoding header and returning the response with a GZIPInputStream.

##Caching Requests are cached using the Cache-Control header. No other caching mechanism is currently supported.

See: "<Insert link to ticket for -> Add support for Max-Age header"

Caching is done using key/value where:

  • key: :
  • value: CachedResponse

Supported caches:

  • GuavaRestCache: Pass in a Guava Cache and caching will be performed in memory
  • MemcacheRestCache: Cache the response in Memcache

Tests

HTTP

  • HTTP tests use a mock-server for http calls. When working on the unit tests start up the mock server in a separate shell (see instructions below).
  • To test additional HTTP calls use the MockServerInitializationClass to add mocks.

Memcache

  • Memcache tests use jmemcached and are started/stopped in the setup/teardown methods of the test.
  • Unfortunately jmemcached does not didn't seem to honor the TTL calls, so those are not tested.

Mock Server

mvn mockserver:run
  • Mock server is automatically started during "mvn test" and "mvn verify" lifescycles

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.