GithubHelp home page GithubHelp logo

isabella232 / pick-1 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from juxt/pick

0.0 0.0 0.0 186 KB

A Clojure library for HTTP server-driven content negotiation.

Home Page: https://juxt.pro

Emacs Lisp 0.09% Clojure 99.19% Makefile 0.71%

pick-1's Introduction

pick

A Clojure library for HTTP server-driven content negotiation.

Warning

STATUS: Alpha. Maturing.

Quick Start

Suppose you are writing a web service and want to provide proactive content negotiation based on the accept headers in the client request. Suppose also that you have established which representations you want to choose between.

A representation is a map that one or more of the following keys:

  • :juxt.http.alpha/content-type — the media-type and charset of the representation

  • :juxt.http.alpha/content-encoding — how the representation has been encoded (e.g. gzip)

  • :juxt.http.alpha/content-language — the natural language of the representation (e.g. es)

  • :juxt.http.alpha/content-length — the length of the payload

Example 1. An example representation
(alias 'http (create-ns 'juxt.http.alpha))

{
…
 {:juxt.http.alpha/content-type "text/html;charset=utf-8"
  :juxt.http.alpha/content-encoding "gzip"
  :juxt.http.alpha/content-language "en-US"
  :juxt.http.alpha/content-length 20578}
…
}

You can use pick to select the representation that is most acceptable to the user agent.

Here’s how:

(require
  '[juxt.pick.alpha.ring :refer [pick]])

(alias 'http (create-ns 'juxt.http.alpha))

(pick
  ;; A ring request (where headers indicate preferences)
  {:request-method :get
   :uri "/"
   :headers {"accept" "text/html"
             "accept-language" "de"}}

  ;; Possible representations
  [
   {:id 1
    ::http/content-type "text/html;charset=utf-8"
    ::http/content-language "en"}

   {:id 2
    ::http/content-type "text/html;charset=utf-8"
    ::http/content-language "de"}

   {:id 3
    ::http/content-type "text/plain;charset=utf-8"}])

=>
{:juxt.pick.alpha/representation {…} ; most acceptable representation
 :juxt.pick.alpha/representations […] ; all representations (scored)
}

Introduction

Content negotiation is a feature of HTTP that allows different 'representations' to be served from the same URI.

pick is a Clojure library that determines quality values of each of the negotiable dimensions and corresponding rules specified in RFC 7231. These can be used by content negotiation algorithms to determine the most appropriate content to respond with.

pick also includes a built-in reference implementation based on Apache’s de-facto content negotiation algorithm.

Problem Statement

Content negotiation is one of the primary ways that the web is inclusive, supporting a wide range of user agents and other software-based clients.

Content negotiation can also contribute to improved performance, by allowing the compression of content for optimising network bandwidth and by cache integration.

Also, since content negotiation allows for gradual migration from old to new data formats it enables graceful and continuous improvement, balancing the needs for both innovation and solid dependability.

However, while content negotiation is often used for static resources, which is well implemented by web servers such as Apache’s httpd and nginx, it is often missing from, or poorly implemented by, the majority of web libraries and frameworks.

API Reference

The pick function 3 arguments:

  • The Ring request

  • A collection of possible representations

  • An option map

The option map supports the following entries:

:juxt.pick.alpha/explain?

if truthy, provide an explain in the return value

:juxt.pick.alpha/vary?

if truthy, compute the preferences that will vary the choice

Alternatives

Most Clojure alternatives only deal with media-types, and perhaps charsets. Note that pick fully implements all proactive (server-driven) content negotiation rules contained in RFC 7231, including media-types, charsets, encodings, languages, wildcards, qvalues and precedence rules. Where not prescribed specifically by the RFC, pick adopts the de-facto decisions made by the Apache httpd engine.

ring-middleware-format and Metosin’s Muuntaja library negotiate media-types and charsets, and perform automatic decoding/encoding of request/response bodies

Caution

pick only provides the decision of which representation(s) to select, given the context of an HTTP request and a set of representation to choose between.

Unlike Muuntaja, pick does not include support for format transformation or coercion, which is considered out of scope for this library.

References

RFC 7231 is the normative standard on content negotiation.

While pick attempts to be reasonably performant, due to the per-request nature of content negotiation some users may consider using a memoization strategy, making use of a memoization library such as clojure.core.memoize.

pick-1's People

Contributors

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