servant-client-0.7.1: automatical derivation of querying functions for servant webservices

Safe HaskellNone
LanguageHaskell2010

Servant.Client

Description

This module provides client which can automatically generate querying functions for each endpoint just from the type representing your API.

Synopsis

Documentation

type family AuthClientData a :: * Source #

For a resource protected by authentication (e.g. AuthProtect), we need to provide the client with some data used to add authentication data to a request

NOTE: THIS API IS EXPERIMENTAL AND SUBJECT TO CHANGE

newtype AuthenticateReq a Source #

For better type inference and to avoid usage of a data family, we newtype wrap the combination of some AuthClientData and a function to add authentication data to a request

NOTE: THIS API IS EXPERIMENTAL AND SUBJECT TO CHANGE

Constructors

AuthenticateReq 

client :: HasClient layout => Proxy layout -> Client layout Source #

client allows you to produce operations to query an API from a client.

type MyApi = "books" :> Get '[JSON] [Book] -- GET /books
        :<|> "books" :> ReqBody '[JSON] Book :> Post '[JSON] Book -- POST /books

myApi :: Proxy MyApi
myApi = Proxy

getAllBooks :: Manager -> BaseUrl -> ClientM [Book]
postNewBook :: Book -> Manager -> BaseUrl -> ClientM Book
(getAllBooks :<|> postNewBook) = client myApi

class HasClient layout where Source #

This class lets us define how each API combinator influences the creation of an HTTP request. It's mostly an internal class, you can just use client.

Minimal complete definition

clientWithRoute

Associated Types

type Client layout :: * Source #

Methods

clientWithRoute :: Proxy layout -> Req -> Client layout Source #

Instances

HasClient * Raw Source #

Pick a Method and specify where the server you want to query is. You get back the full Response.

Associated Types

type Client Raw (layout :: Raw) :: * Source #

Methods

clientWithRoute :: Proxy Raw layout -> Req -> Client Raw layout Source #

(HasClient * a, HasClient * b) => HasClient * ((:<|>) a b) Source #

A client querying function for a :<|> b will actually hand you one function for querying a and another one for querying b, stitching them together with :<|>, which really is just like a pair.

type MyApi = "books" :> Get '[JSON] [Book] -- GET /books
        :<|> "books" :> ReqBody '[JSON] Book :> Post Book -- POST /books

myApi :: Proxy MyApi
myApi = Proxy

getAllBooks :: Manager -> BaseUrl -> ClientM [Book]
postNewBook :: Book -> Manager -> BaseUrl -> ClientM Book
(getAllBooks :<|> postNewBook) = client myApi

Associated Types

type Client ((:<|>) a b) (layout :: (:<|>) a b) :: * Source #

Methods

clientWithRoute :: Proxy (a :<|> b) layout -> Req -> Client (a :<|> b) layout Source #

HasClient * subapi => HasClient * (WithNamedContext name context subapi) Source # 

Associated Types

type Client (WithNamedContext name context subapi) (layout :: WithNamedContext name context subapi) :: * Source #

Methods

clientWithRoute :: Proxy (WithNamedContext name context subapi) layout -> Req -> Client (WithNamedContext name context subapi) layout Source #

HasClient k api => HasClient * ((:>) k * (BasicAuth realm usr) api) Source # 

Associated Types

type Client ((:>) k * (BasicAuth realm usr) api) (layout :: (:>) k * (BasicAuth realm usr) api) :: * Source #

Methods

clientWithRoute :: Proxy ((k :> *) (BasicAuth realm usr) api) layout -> Req -> Client ((k :> *) (BasicAuth realm usr) api) layout Source #

HasClient k1 api => HasClient * ((:>) k1 * (AuthProtect k tag) api) Source # 

Associated Types

type Client ((:>) k1 * (AuthProtect k tag) api) (layout :: (:>) k1 * (AuthProtect k tag) api) :: * Source #

Methods

clientWithRoute :: Proxy ((k1 :> *) (AuthProtect k tag) api) layout -> Req -> Client ((k1 :> *) (AuthProtect k tag) api) layout Source #

HasClient k api => HasClient * ((:>) k * IsSecure api) Source # 

Associated Types

type Client ((:>) k * IsSecure api) (layout :: (:>) k * IsSecure api) :: * Source #

Methods

clientWithRoute :: Proxy ((k :> *) IsSecure api) layout -> Req -> Client ((k :> *) IsSecure api) layout Source #

HasClient k api => HasClient * ((:>) k * RemoteHost api) Source # 

Associated Types

type Client ((:>) k * RemoteHost api) (layout :: (:>) k * RemoteHost api) :: * Source #

Methods

clientWithRoute :: Proxy ((k :> *) RemoteHost api) layout -> Req -> Client ((k :> *) RemoteHost api) layout Source #

HasClient k api => HasClient * ((:>) k * Vault api) Source # 

Associated Types

type Client ((:>) k * Vault api) (layout :: (:>) k * Vault api) :: * Source #

Methods

clientWithRoute :: Proxy ((k :> *) Vault api) layout -> Req -> Client ((k :> *) Vault api) layout Source #

(KnownSymbol path, HasClient k sublayout) => HasClient * ((:>) k Symbol path sublayout) Source #

Make the querying function append path to the request path.

Associated Types

type Client ((:>) k Symbol path sublayout) (layout :: (:>) k Symbol path sublayout) :: * Source #

Methods

clientWithRoute :: Proxy ((k :> Symbol) path sublayout) layout -> Req -> Client ((k :> Symbol) path sublayout) layout Source #

(MimeRender * ct a, HasClient k sublayout) => HasClient * ((:>) k * (ReqBody * ((:) * ct cts) a) sublayout) Source #

If you use a ReqBody in one of your endpoints in your API, the corresponding querying function will automatically take an additional argument of the type specified by your ReqBody. That function will take care of encoding this argument as JSON and of using it as the request body.

All you need is for your type to have a ToJSON instance.

Example:

type MyApi = "books" :> ReqBody '[JSON] Book :> Post '[JSON] Book

myApi :: Proxy MyApi
myApi = Proxy

addBook :: Book -> Manager -> BaseUrl -> ClientM Book
addBook = client myApi
-- then you can just use "addBook" to query that endpoint

Associated Types

type Client ((:>) k * (ReqBody * ((:) * ct cts) a) sublayout) (layout :: (:>) k * (ReqBody * ((:) * ct cts) a) sublayout) :: * Source #

Methods

clientWithRoute :: Proxy ((k :> *) (ReqBody * ((* ': ct) cts) a) sublayout) layout -> Req -> Client ((k :> *) (ReqBody * ((* ': ct) cts) a) sublayout) layout Source #

(KnownSymbol sym, HasClient k sublayout) => HasClient * ((:>) k * (QueryFlag sym) sublayout) Source #

If you use a QueryFlag in one of your endpoints in your API, the corresponding querying function will automatically take an additional Bool argument.

If you give False, nothing will be added to the query string.

Otherwise, this function will insert a value-less query string parameter under the name associated to your QueryFlag.

Example:

type MyApi = "books" :> QueryFlag "published" :> Get '[JSON] [Book]

myApi :: Proxy MyApi
myApi = Proxy

getBooks :: Bool -> Manager -> BaseUrl -> ClientM [Book]
getBooks = client myApi
-- then you can just use "getBooks" to query that endpoint.
-- 'getBooksBy False' for all books
-- 'getBooksBy True' to only get _already published_ books

Associated Types

type Client ((:>) k * (QueryFlag sym) sublayout) (layout :: (:>) k * (QueryFlag sym) sublayout) :: * Source #

Methods

clientWithRoute :: Proxy ((k :> *) (QueryFlag sym) sublayout) layout -> Req -> Client ((k :> *) (QueryFlag sym) sublayout) layout Source #

(KnownSymbol sym, ToHttpApiData a, HasClient k sublayout) => HasClient * ((:>) k * (QueryParams * sym a) sublayout) Source #

If you use a QueryParams in one of your endpoints in your API, the corresponding querying function will automatically take an additional argument, a list of values of the type specified by your QueryParams.

If you give an empty list, nothing will be added to the query string.

Otherwise, this function will take care of inserting a textual representation of your values in the query string, under the same query string parameter name.

You can control how values for your type are turned into text by specifying a ToHttpApiData instance for your type.

Example:

type MyApi = "books" :> QueryParams "authors" Text :> Get '[JSON] [Book]

myApi :: Proxy MyApi
myApi = Proxy

getBooksBy :: [Text] -> Manager -> BaseUrl -> ClientM [Book]
getBooksBy = client myApi
-- then you can just use "getBooksBy" to query that endpoint.
-- 'getBooksBy []' for all books
-- 'getBooksBy ["Isaac Asimov", "Robert A. Heinlein"]'
--   to get all books by Asimov and Heinlein

Associated Types

type Client ((:>) k * (QueryParams * sym a) sublayout) (layout :: (:>) k * (QueryParams * sym a) sublayout) :: * Source #

Methods

clientWithRoute :: Proxy ((k :> *) (QueryParams * sym a) sublayout) layout -> Req -> Client ((k :> *) (QueryParams * sym a) sublayout) layout Source #

(KnownSymbol sym, ToHttpApiData a, HasClient k sublayout) => HasClient * ((:>) k * (QueryParam * sym a) sublayout) Source #

If you use a QueryParam in one of your endpoints in your API, the corresponding querying function will automatically take an additional argument of the type specified by your QueryParam, enclosed in Maybe.

If you give Nothing, nothing will be added to the query string.

If you give a non-Nothing value, this function will take care of inserting a textual representation of this value in the query string.

You can control how values for your type are turned into text by specifying a ToHttpApiData instance for your type.

Example:

type MyApi = "books" :> QueryParam "author" Text :> Get '[JSON] [Book]

myApi :: Proxy MyApi
myApi = Proxy

getBooksBy :: Maybe Text -> Manager -> BaseUrl -> ClientM [Book]
getBooksBy = client myApi
-- then you can just use "getBooksBy" to query that endpoint.
-- 'getBooksBy Nothing' for all books
-- 'getBooksBy (Just "Isaac Asimov")' to get all books by Isaac Asimov

Associated Types

type Client ((:>) k * (QueryParam * sym a) sublayout) (layout :: (:>) k * (QueryParam * sym a) sublayout) :: * Source #

Methods

clientWithRoute :: Proxy ((k :> *) (QueryParam * sym a) sublayout) layout -> Req -> Client ((k :> *) (QueryParam * sym a) sublayout) layout Source #

HasClient k sublayout => HasClient * ((:>) k * HttpVersion sublayout) Source #

Using a HttpVersion combinator in your API doesn't affect the client functions.

Associated Types

type Client ((:>) k * HttpVersion sublayout) (layout :: (:>) k * HttpVersion sublayout) :: * Source #

Methods

clientWithRoute :: Proxy ((k :> *) HttpVersion sublayout) layout -> Req -> Client ((k :> *) HttpVersion sublayout) layout Source #

(KnownSymbol sym, ToHttpApiData a, HasClient k sublayout) => HasClient * ((:>) k * (Header sym a) sublayout) Source #

If you use a Header in one of your endpoints in your API, the corresponding querying function will automatically take an additional argument of the type specified by your Header, wrapped in Maybe.

That function will take care of encoding this argument as Text in the request headers.

All you need is for your type to have a ToHttpApiData instance.

Example:

newtype Referer = Referer { referrer :: Text }
  deriving (Eq, Show, Generic, ToHttpApiData)

           -- GET /view-my-referer
type MyApi = "view-my-referer" :> Header "Referer" Referer :> Get '[JSON] Referer

myApi :: Proxy MyApi
myApi = Proxy

viewReferer :: Maybe Referer -> Manager -> BaseUrl -> ClientM Book
viewReferer = client myApi
-- then you can just use "viewRefer" to query that endpoint
-- specifying Nothing or e.g Just "http://haskell.org/" as arguments

Associated Types

type Client ((:>) k * (Header sym a) sublayout) (layout :: (:>) k * (Header sym a) sublayout) :: * Source #

Methods

clientWithRoute :: Proxy ((k :> *) (Header sym a) sublayout) layout -> Req -> Client ((k :> *) (Header sym a) sublayout) layout Source #

(KnownSymbol capture, ToHttpApiData a, HasClient k sublayout) => HasClient * ((:>) k * (Capture * capture a) sublayout) Source #

If you use a Capture in one of your endpoints in your API, the corresponding querying function will automatically take an additional argument of the type specified by your Capture. That function will take care of inserting a textual representation of this value at the right place in the request path.

You can control how values for this type are turned into text by specifying a ToHttpApiData instance for your type.

Example:

type MyApi = "books" :> Capture "isbn" Text :> Get '[JSON] Book

myApi :: Proxy MyApi
myApi = Proxy

getBook :: Text -> Manager -> BaseUrl -> ClientM Book
getBook = client myApi
-- then you can just use "getBook" to query that endpoint

Associated Types

type Client ((:>) k * (Capture * capture a) sublayout) (layout :: (:>) k * (Capture * capture a) sublayout) :: * Source #

Methods

clientWithRoute :: Proxy ((k :> *) (Capture * capture a) sublayout) layout -> Req -> Client ((k :> *) (Capture * capture a) sublayout) layout Source #

(BuildHeadersTo ls, ReflectMethod k1 method) => HasClient * (Verb * k1 method status cts (Headers ls NoContent)) Source # 

Associated Types

type Client (Verb * k1 method status cts (Headers ls NoContent)) (layout :: Verb * k1 method status cts (Headers ls NoContent)) :: * Source #

Methods

clientWithRoute :: Proxy (Verb * k1 method status cts (Headers ls NoContent)) layout -> Req -> Client (Verb * k1 method status cts (Headers ls NoContent)) layout Source #

(MimeUnrender * ct a, BuildHeadersTo ls, ReflectMethod k1 method, (~) [*] cts' ((:) * ct cts)) => HasClient * (Verb * k1 method status cts' (Headers ls a)) Source # 

Associated Types

type Client (Verb * k1 method status cts' (Headers ls a)) (layout :: Verb * k1 method status cts' (Headers ls a)) :: * Source #

Methods

clientWithRoute :: Proxy (Verb * k1 method status cts' (Headers ls a)) layout -> Req -> Client (Verb * k1 method status cts' (Headers ls a)) layout Source #

ReflectMethod k1 method => HasClient * (Verb * k1 method status cts NoContent) Source # 

Associated Types

type Client (Verb * k1 method status cts NoContent) (layout :: Verb * k1 method status cts NoContent) :: * Source #

Methods

clientWithRoute :: Proxy (Verb * k1 method status cts NoContent) layout -> Req -> Client (Verb * k1 method status cts NoContent) layout Source #

(MimeUnrender * ct a, ReflectMethod k1 method, (~) [*] cts' ((:) * ct cts)) => HasClient * (Verb * k1 method status cts' a) Source # 

Associated Types

type Client (Verb * k1 method status cts' a) (layout :: Verb * k1 method status cts' a) :: * Source #

Methods

clientWithRoute :: Proxy (Verb * k1 method status cts' a) layout -> Req -> Client (Verb * k1 method status cts' a) layout Source #

mkAuthenticateReq :: AuthClientData a -> (AuthClientData a -> Req -> Req) -> AuthenticateReq a Source #

Handy helper to avoid wrapping datatypes in tuples everywhere.

NOTE: THIS API IS EXPERIMENTAL AND SUBJECT TO CHANGE