Git Product home page Git Product logo

tentacle_response_formatter's Introduction

Tentacle Response Formatter

A small class that, given a shelf request and some simple data, will determine a correct response format (eg.: Json or XML) and convert the data to the appropriate format.

How to use

// ResponseFormatter is a Singleton so you will always get the same instance with new
ResponseFormatter formatter = new ResponseFormatter();

// create a result from a shelf.Request and data
FormatResult result = formatter.formatResponse(request, {"message": "hello"});

//body contains the generated response as String
print(result.body) // -> '{"message":"hello"}' or '<response><message>hello</message></response>' or 'hello'

//contentType is a suggestion to set as the HttpHeaders.CONTENT_TYPE in the shelf.Response you create
print(result.contentType) // -> "application/json" or "application/xml" or "text/plain"

How is the response format detected

There are currently 3 values that influence the response format detection. They are applyed in the following order:

  1. Format query param: "http://example.com/some?format=json" would force the response to be converted to Json.
  2. Format file extension: "http://example.com/some.xml" would force the response to be converted to XML.
  3. Accept Header: "accept: application/json, text/xml" would force the response to be converted to Json.

Accept headers are used in the order they are defined. So first format has precedence over second and so forth.

If no format can be detected that has a valid encoder registered the response is encoded as String. If data contains a message field it is taken for String response otherwise the result will be an empty String. The string formatter is not a real formatter but rather a fallback. Read later on how to add your own formatter and register it for content types.

Add your own response formatter

You may want to add your own formatters or content-type mappings to the formatter. To do so you have to provide a formatter function which takes dynamic data and returns a String. Furthermore you have to provide a unique name and may provide your custom content-types that should be handled by your formatter.

// add a simple formatter for csv
formatter.registerFormatter("csv", "text/csv", (dynamic data) {
  return "Your CSV result here";
});
// now ?format=csv, file.csv and accept header "text/csv" are formatted using your formatter

// override additional accept-headers
formatter.registerFormatter("allmighty", "text/allmighty", (dynamic data) {
      return "allmighty response body";
}, ["application/json", "application/xhtml+xml", "*/*", "text/plain", "application/xml"]);

// replace existing formatter
formatter.registerFormatter("json", "application/json", (dynamic data) {
  return "Your json result here";
});
// json is now handled by your formatter including all accept-headers json was initially registered for

License

Apache 2.0

tentacle_response_formatter's People

Contributors

tompro avatar andersmholmgren avatar

Watchers

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