Safe Haskell | None |
---|---|
Language | Haskell2010 |
This module provides client
which can automatically generate
querying functions for each endpoint just from the type representing your
API.
Documentation
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 [Book] -- GET /books :<|> "books" :> ReqBody Book :> Post Book -- POST /books myApi :: Proxy MyApi myApi = Proxy getAllBooks :: BaseUrl -> EitherT String IO [Book] postNewBook :: Book -> BaseUrl -> EitherT String IO 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
.
clientWithRoute :: Proxy layout -> Req -> Client layout Source
HasClient Raw | Pick a |
HasClient Delete | If you have a |
FromJSON a => HasClient (Put a) | If you have a |
FromJSON a => HasClient (Post a) | If you have a |
FromJSON result => HasClient (Get result) | If you have a |
(HasClient a, HasClient b) => HasClient ((:<|>) a b) | A client querying function for type MyApi = "books" :> Get [Book] -- GET /books :<|> "books" :> ReqBody Book :> Post Book -- POST /books myApi :: Proxy MyApi myApi = Proxy getAllBooks :: BaseUrl -> EitherT String IO [Book] postNewBook :: Book -> BaseUrl -> EitherT String IO Book (getAllBooks :<|> postNewBook) = client myApi |
(ToJSON a, HasClient sublayout) => HasClient ((:>) * (ReqBody * a) sublayout) | If you use a All you need is for your type to have a Example: type MyApi = "books" :> ReqBody Book :> Post Book myApi :: Proxy MyApi myApi = Proxy addBook :: Book -> BaseUrl -> EitherT String IO Book addBook = client myApi -- then you can just use "addBook" to query that endpoint |
(KnownSymbol sym, ToText a, HasClient sublayout) => HasClient ((:>) * (QueryParam Symbol * sym a) sublayout) | If you use a If you give Nothing, nothing will be added to the query string. If you give a non- You can control how values for your type are turned into
text by specifying a Example: type MyApi = "books" :> QueryParam "author" Text :> Get [Book] myApi :: Proxy MyApi myApi = Proxy getBooksBy :: Maybe Text -> BaseUrl -> EitherT String IO [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 |
(KnownSymbol sym, ToText a, HasClient sublayout) => HasClient ((:>) * (QueryParams Symbol * sym a) sublayout) | If you use a 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 Example: type MyApi = "books" :> QueryParams "authors" Text :> Get [Book] myApi :: Proxy MyApi myApi = Proxy getBooksBy :: [Text] -> BaseUrl -> EitherT String IO [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, HasClient sublayout) => HasClient ((:>) * (QueryFlag Symbol sym) sublayout) | If you use a If you give Otherwise, this function will insert a value-less query string
parameter under the name associated to your Example: type MyApi = "books" :> QueryFlag "published" :> Get [Book] myApi :: Proxy MyApi myApi = Proxy getBooks :: Bool -> BaseUrl -> EitherT String IO [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, ToText a, HasClient sublayout) => HasClient ((:>) * (Header Symbol * sym a) sublayout) | If you use a 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 Example: newtype Referer = Referer Text deriving (Eq, Show, FromText, ToText) -- GET /view-my-referer type MyApi = "view-my-referer" :> Header "Referer" Referer :> Get Referer myApi :: Proxy MyApi myApi = Proxy viewReferer :: Maybe Referer -> BaseUrl -> EitherT String IO Book viewReferer = client myApi -- then you can just use "viewRefer" to query that endpoint -- specifying Nothing or Just "http://haskell.org/" as arguments |
(KnownSymbol capture, ToText a, HasClient sublayout) => HasClient ((:>) * (Capture Symbol * capture a) sublayout) | If you use a You can control how values for this type are turned into
text by specifying a Example: type MyApi = "books" :> Capture "isbn" Text :> Get Book myApi :: Proxy MyApi myApi = Proxy getBook :: Text -> BaseUrl -> EitherT String IO Book getBook = client myApi -- then you can just use "getBook" to query that endpoint |
(KnownSymbol path, HasClient sublayout) => HasClient ((:>) Symbol path sublayout) | Make the querying function append |
module Servant.Common.BaseUrl