Code Monkey home page Code Monkey logo

braintreehttp_java's Introduction

Braintree HttpClient

BraintreeHttp is a generic HTTP Client.

In it's simplest form, an HttpClient exposes an #execute method which takes an HttpRequest, executes it against the domain described in an Environment, and returns an HttpResponse. It throws an IOException if anything goes wrong during execution.

Environment

An Environment describes a domain that hosts a REST API, against which an HttpClient will make requests. Environment is a simple interface that wraps one method, #baseUrl.

Environment env = () -> "https://example.com";

Requests

HttpRequests contain all the information needed to make an HTTP request against the REST API. Specifically, one request describes a path, a verb, any path/query/form parameters, headers, attached files for upload, and body data. This class also holds a reference to the type of the response for deserializtion, if a structured response is expected.

Responses

HttpResponses contain information returned by a server in response to a request as described above. They contain a status code, headers, and any data returned by the server, deserialized in accordance with the type in the HttpRequest from which this reponse originated.

HttpRequest<MyResponsePojo> req = new HttpRequest("/path/to/resource", "GET", MyResponsePojo.class);

HttpResponse<MyResponsePojo> resp = client.execute(req);

Integer statusCode = resp.statusCode();
Headers headers = resp.headers();
MyResponsePojo responseData = resp.result();

Injectors

Injectors wrap closures that can be used for executing arbitrary pre-flight logic, such as modifying a request or logging data. Injectors are attached to an HttpClient using the #addInjector method.

The HttpClient executes its Injectors in a first-in, first-out order, before each request.

HttpClient client = new HttpClient(env);

client.addInjector(req -> {
  log.log(req);
});

client.addInjector(req -> {
  req.headers().header("Request-Id", "abcd");
});

...

Error Handling

HttpClient#execute may throw an IOException if something went wrong during the course of execution. If the server returned a non-200 response, this execption will be an instance of HttpException that will contain a status code and headers you can use for debugging.

try {
  HttpResponse<MyResponsePojo> resp = client.execute(req);
} catch(IOException ioe) {
  if (ioe instanceof HttpException) {
    // Inspect this exception for details
    HttpException he = (HttpException) ioe;
    int statusCode = ioe.statusCode();
  } else {
    // Something else went wrong
  }
}

Serializer

(De)Serialization of request and response data is done by implementations of the Serializer interface. BraintreeHttp currently supports json encoding out of the box.

SSL

By default, BraintreeHttp will use the built-in TLSSoccketFactory when connecting to URLs that use https as their scheme. If you'd like to do cert-pinning, or use a different SSL implementation, you can provide your own SSLSocketFactory via HttpClient#setSSLSocketFactory().

License

BraintreeHttp-Java is open source and available under the MIT license. See the LICENSE file for more info.

Contributions

Pull requests and new issues are welcome. See CONTRIBUTING.md for details.

braintreehttp_java's People

Contributors

braintreeps avatar hollabaq86 avatar

Watchers

 avatar  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.