servant-client-0.7: 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.

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.

(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
HasClient * subapi => HasClient * (WithNamedContext name context subapi) Source 
HasClient k api => HasClient * ((:>) * k (BasicAuth realm usr) api) Source 
HasClient k api => HasClient * ((:>) * k (AuthProtect k tag) api) Source 
HasClient k api => HasClient * ((:>) * k IsSecure api) Source 
HasClient k api => HasClient * ((:>) * k RemoteHost api) Source 
HasClient k api => HasClient * ((:>) * k Vault api) 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
(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
(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
(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
HasClient k sublayout => HasClient * ((:>) * k HttpVersion sublayout) Source

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

(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
(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
(KnownSymbol path, HasClient k sublayout) => HasClient * ((:>) Symbol k path sublayout) Source

Make the querying function append path to the request path.

(BuildHeadersTo ls, ReflectMethod k method) => HasClient * (Verb k * method status cts (Headers ls NoContent)) Source 
(MimeUnrender * ct a, BuildHeadersTo ls, ReflectMethod k method, (~) [*] cts' ((:) * ct cts)) => HasClient * (Verb k * method status cts' (Headers ls a)) Source 
ReflectMethod k method => HasClient * (Verb k * method status cts NoContent) Source 
(MimeUnrender * ct a, ReflectMethod k method, (~) [*] cts' ((:) * ct cts)) => HasClient * (Verb k * method status cts' a) 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