wrecker-1.2.2.0: An HTTP Performance Benchmarker

Safe HaskellNone
LanguageHaskell2010

Network.Wreq.Wrecker.API

Contents

Description

This module can be used to describe the operations that can be performed against an external API that uses JSON as its serialization format.

It exposes functions tailored to get and post data in the JSON format and to unserialize the responses into types that can be converted form JSON.

Use the Ref and RPC types to describe the external API operations.

Synopsis

Documentation

data Ref response Source #

A Ref represents an API resource whose contents can be retrieved with GET

We use the phantom type parameter response so that we can use this information for understanding what type of data the API resource will return

For example, an API endpoint returning a list of User is modelled as follows:

usersRef :: Ref [User]
usersRef = Ref "http://localhost:8080/users"

Constructors

Ref 

Fields

Instances

Eq (Ref response) Source # 

Methods

(==) :: Ref response -> Ref response -> Bool #

(/=) :: Ref response -> Ref response -> Bool #

Show (Ref response) Source # 

Methods

showsPrec :: Int -> Ref response -> ShowS #

show :: Ref response -> String #

showList :: [Ref response] -> ShowS #

FromJSON (Ref a) Source # 

Methods

parseJSON :: Value -> Parser (Ref a) #

parseJSONList :: Value -> Parser [Ref a] #

newtype RPC response request Source #

A Req represents an API resource whose contents can be retrieved with POST

We use the phantom type parameter response so that we can use this information for understanding what type of data the API resource will return

Similarly, the request parameter is used to know what type of data can be posted to the endpoint.

For example, an API endpoint that receives a User and returns a list of Friendship is modelled as follows:

newFriendRpc :: User -> RPC [Friendship] User
newFriendRpc User { userID } = RPC "http://localhost:8080/users/" <> userID <> "/friendships"

Constructors

RPC 

Fields

Instances

Eq (RPC response request) Source # 

Methods

(==) :: RPC response request -> RPC response request -> Bool #

(/=) :: RPC response request -> RPC response request -> Bool #

Show (RPC response request) Source # 

Methods

showsPrec :: Int -> RPC response request -> ShowS #

show :: RPC response request -> String #

showList :: [RPC response request] -> ShowS #

FromJSON (RPC a b) Source # 

Methods

parseJSON :: Value -> Parser (RPC a b) #

parseJSONList :: Value -> Parser [RPC a b] #

Interacting with an external API

get :: FromJSON a => Session -> Ref a -> IO a Source #

Gets the results form an API endpoint that responds in the JSON format.

users <- get sess usersRef
mapM_ (print . userID) users

post Source #

Arguments

:: (Postable req, FromJSON res) 
=> Session 
-> RPC res req 
-> req

The request payload

-> IO res 

Posts some data to an API endpoint in any Postable format and gets the response in JSON format.

Friendships friends <- post sess (newFriendRpc user) user2
mapM_ (print . userID) friends

put Source #

Arguments

:: (Putable req, FromJSON res) 
=> Session 
-> RPC res req 
-> req

The request payload

-> IO res 

Puts some data to an API endpoint in any Putable format and gets the response in JSON format.

Friendships friends <- put sess (newFriendRpc user) user2
mapM_ (print . userID) friends

rpc Source #

Arguments

:: (ToJSON req, FromJSON res) 
=> Session 
-> RPC res req 
-> req

The request payload

-> IO res 

Posts some data to an API endpoint in any JSON format and gets the response in JSON format.

Friendships friends <- post sess (newFriendRpc user) user2
mapM_ (print . userID) friends

delete :: FromJSON a => Session -> Ref a -> IO a Source #

Sends as DELETE request to an API endpoint that responds in the JSON format.

Status { success } <- delete sess usersDeleteRef

Network functions with Options

getWith :: FromJSON a => Options -> Session -> Ref a -> IO a Source #

Gets the results form an API endpoint that responds in the JSON format. This function excepts additional options, such as custom headers

let opts = Network.Wreq.defaults & Network.Wreq.auth ?~ Network.Wreq.basicAuth "user" "pass"
users <- getWith opts sess usersRef
mapM_ (print . userID) users

postWith Source #

Arguments

:: (Postable req, FromJSON res) 
=> Options 
-> Session 
-> RPC res req 
-> req

The request payload

-> IO res 

Posts some data to an API endpoint in any Postable format and gets the response in JSON format. This function excepts additional options, such as custom headers

let opts = Network.Wreq.defaults & Network.Wreq.auth ?~ Network.Wreq.basicAuth "user" "pass"
Friendships friends <- post sess (newFriendRpc user) user2
mapM_ (print . userID) friends

putWith Source #

Arguments

:: (Putable req, FromJSON res) 
=> Options 
-> Session 
-> RPC res req 
-> req

The request payload

-> IO res 

Puts some data to an API endpoint in any Putable format and gets the response in JSON format. This function excepts additional options, such as custom headers

let opts = Network.Wreq.defaults & Network.Wreq.auth ?~ Network.Wreq.basicAuth "user" "pass"
Friendships friends <- put sess (newFriendRpc user) user2
mapM_ (print . userID) friends

rpcWith Source #

Arguments

:: (ToJSON req, FromJSON res) 
=> Options 
-> Session 
-> RPC res req 
-> req

The request payload

-> IO res 

Posts some data to an API endpoint in any JSON format and gets the response in JSON format. This function excepts additional options, such as custom headers

let opts = Network.Wreq.defaults & Network.Wreq.auth ?~ Network.Wreq.basicAuth "user" "pass"
Friendships friends <- post sess (newFriendRpc user) user2
mapM_ (print . userID) friends

deleteWith :: FromJSON a => Options -> Session -> Ref a -> IO a Source #

Sends as DELETE request to an API endpoint that responds in the JSON format. This function excepts additional options, such as custom headers

let opts = Network.Wreq.defaults & Network.Wreq.auth ?~ Network.Wreq.basicAuth "user" "pass"
Status { success } <- deleteWith opts sess usersDeleteRef

Utility functions

query :: [(Text, Text)] -> Text Source #

Orphan instances

Postable () Source #

Some API endpoints are odd in that they expect a POST as a verb, but no request body is expected. This models such case.

Methods

postPayload :: () -> Request -> IO Request #

Postable [(Text, Text)] Source #

The form attributes are passed as tuples, representing key-value pairs: For example

postPayload [("username", "jon"), ("password", "snow")]

Methods

postPayload :: [(Text, Text)] -> Request -> IO Request #