nero-0.3.1: Lens-based HTTP toolkit

Safe HaskellSafe-Inferred
LanguageHaskell2010

Nero

Contents

Description

This module re-exports the essential functions for quickly writing Nero applications. Use the original modules for more specialized functions.

Synopsis

Server

class Server a where Source

Ultimately any valid Nero server application must be transformed Request -> IO Response. This type class facilitates the creation of web server handling Nero applications.

Request

data Request Source

An HTTP Request.

Instances

Eq Request 
Show Request 
Param Request

It traverses the values with the same key both in the query string and the form encoded body of a POST Request.

Formed Request 
HasBody Request 
HasQuery Request 
HasPath Request 
HasHost Request 
HasUrl Request 
Server (Request -> Maybe Response) 
Server (Request -> Response) 

get :: Url -> Request Source

Smart constructor for GET Requests.

post :: Url -> Payload -> Request Source

Smart constructor for POST Requests.

params :: Traversal' Request MultiMap Source

This Traversal lets you traverse every HTTP parameter regardless of whether it's present in the query string or in the form encoded body of a POST Request. In the rare case where there are HTTP parameters in both, every parameter is still being traversed starting from the /query string/.

You might want to use param for traversing a specific parameter.

>>> let request = dummyRequestForm & query . at "name" ?~ ["hello", "out"] & form  . at "name" ?~ ["there"]
>>> foldOf params request ^? ix "name"
Just ["hello","out","there"]

body :: HasBody a => a -> Body Source

GET

POST

Response

ok :: Text -> Response Source

Creates an 200 OK response from the given text. It automatically encodes the text to 'utf-8'. The Mime type is text/plain.

movedPermanently :: Url -> Response Source

Creates an 301 Moved Permanently response with the Location corresponding to the given Url.

URL

data Url Source

Composite type of a Scheme, Host, Path, Query.

Matching

match :: Getter Text Match Source

This is just to pure with a refined type.

class Prefixed a where Source

This Prism' strips/prepends a prefix.

>>> ("/hello/there"::Text) ^? prefixed "/hello/"
Just "there"
>>> prefixed "/hello/" # ("there"::Text)
"/hello/there"

If matching the entire source it previews to an empty Text. You might use exact if you are expecting this behavior.

>>> ("hello"::Text) ^? prefixed "hello"
Just ""

This also means that to review an entire source, you need to give it an empty Text.

>>> prefixed "hello" # (mempty::Text)
"hello"

An empty Match matches to itself regardless of the pattern.

>>> preview (prefixed "hello") (review (prefixed "hello") (mempty::Text)) <&> is _Empty
Just True

Methods

prefixed :: Text -> Prism' a a Source

Instances

Prefixed Text 
Prefixed Match

Like Text instance but for the head of a Match

class Suffixed a where Source

This Prism' strips/appends a suffix.

>>> ("/hello/there"::Text) ^? suffixed "there"
Just "/hello/"
>>> suffixed "there" # ("/hello/"::Text)
"/hello/there"

If matching the entire source it previews to an empty Text. You might use exact if you are expecting this behavior.

>>> ("hello"::Text) ^? suffixed "hello"
Just ""

This also means that to review an entire source, you need to give it an empty Text.

>>> suffixed "hello" # (mempty::Text)
"hello"

An empty Match matches to itself regardless of the pattern.

>>> preview (suffixed "hello") (review (suffixed "hello") (mempty::Text)) <&> is _Empty
Just True

Methods

suffixed :: Text -> Prism' a a Source

Instances

Suffixed Text 
Suffixed Match

Like Text instance but for the head of a Match

sep :: Text -> Prism' Match Match Source

This Prism' splits/joins at the first occurrence of a boundary for the first value of a Match.

>>> pure "hello/out/there" ^? sep "/" <&> toListOf folded
Just ["out/there","hello"]
>>> sep "/" # (pure "out/there" <> pure "hello") & view _head
"hello/out/there"

Notice what happens when there is no source before or after a boundary:

>>> pure "hello/" ^? sep "/"
Just ["","hello"]
>>> (pure "hello/" <> pure "there") ^? sep "/"
Just ["","hello","there"]
>>> pure "/hello" ^? sep "/"
Just ["hello",""]
>>> (pure "/hello" <> pure "there") ^? sep "/"
Just ["hello","","there"]

When the source is identical to the boundary: >>> pure "hello" ^? sep "hello" Just []

split :: Text -> Fold Text Match Source

This is the composition of match and sep. Use this to avoid lifting a Match explicitly. Notice that, unlike sep, this is not reversible.

exact :: Text -> Prism' Text () Source

This is just an alias to only. Use this to match the entirety of the source. The source mustn't be lifted to a Match.

>>> "hello" ^? exact "hello"
Just ()

Results handling

class Target a where Source

Prism' between a Match and a target type.

Instances

Testing

dummyRequest :: Request Source

An empty GET request useful for testing.

dummyRequestForm :: Request Source

An empty POST request with an empty form encoded body useful for testing.

dummyUrl :: Url Source

Empty Url useful for testing.

Nero Prelude