2020-10-31 20:45:46 +01:00
|
|
|
{-# LANGUAGE ConstraintKinds #-}
|
2020-11-18 19:57:20 +01:00
|
|
|
{-# LANGUAGE CPP #-}
|
2017-09-13 18:36:20 +02:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
|
|
|
{-# LANGUAGE InstanceSigs #-}
|
|
|
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE PolyKinds #-}
|
2018-03-23 17:36:24 +01:00
|
|
|
{-# LANGUAGE RankNTypes #-}
|
2017-09-13 18:36:20 +02:00
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
2020-10-31 20:45:46 +01:00
|
|
|
{-# LANGUAGE TypeApplications #-}
|
2017-09-13 18:36:20 +02:00
|
|
|
{-# LANGUAGE TypeFamilies #-}
|
|
|
|
{-# LANGUAGE TypeOperators #-}
|
|
|
|
{-# LANGUAGE UndecidableInstances #-}
|
|
|
|
|
2020-11-18 19:57:20 +01:00
|
|
|
#if MIN_VERSION_base(4,9,0) && __GLASGOW_HASKELL__ >= 802
|
|
|
|
#define HAS_TYPE_ERROR
|
|
|
|
#endif
|
|
|
|
|
2019-02-18 18:08:13 +01:00
|
|
|
module Servant.Client.Core.HasClient (
|
|
|
|
clientIn,
|
|
|
|
HasClient (..),
|
|
|
|
EmptyClient (..),
|
2020-10-31 20:45:46 +01:00
|
|
|
foldMapUnion,
|
|
|
|
matchUnion,
|
2019-02-18 18:08:13 +01:00
|
|
|
) where
|
2017-09-13 18:36:20 +02:00
|
|
|
|
2018-06-30 21:17:08 +02:00
|
|
|
import Prelude ()
|
2017-09-14 15:53:51 +02:00
|
|
|
import Prelude.Compat
|
2017-09-13 18:36:20 +02:00
|
|
|
|
2020-10-31 20:45:46 +01:00
|
|
|
import Control.Arrow
|
|
|
|
(left, (+++))
|
2019-02-18 18:08:13 +01:00
|
|
|
import Control.Monad
|
|
|
|
(unless)
|
2021-08-28 00:57:37 +02:00
|
|
|
import qualified Data.ByteString as BS
|
|
|
|
import Data.ByteString.Builder
|
|
|
|
(toLazyByteString)
|
2019-02-06 11:12:56 +01:00
|
|
|
import qualified Data.ByteString.Lazy as BL
|
2020-10-31 20:45:46 +01:00
|
|
|
import Data.Either
|
|
|
|
(partitionEithers)
|
2018-06-30 21:17:08 +02:00
|
|
|
import Data.Foldable
|
|
|
|
(toList)
|
|
|
|
import Data.List
|
|
|
|
(foldl')
|
|
|
|
import Data.Sequence
|
|
|
|
(fromList)
|
2019-02-18 18:08:13 +01:00
|
|
|
import qualified Data.Text as T
|
|
|
|
import Network.HTTP.Media
|
|
|
|
(MediaType, matches, parseAccept, (//))
|
2020-10-31 20:45:46 +01:00
|
|
|
import qualified Data.Sequence as Seq
|
|
|
|
import Data.SOP.BasicFunctors
|
|
|
|
(I (I), (:.:) (Comp))
|
|
|
|
import Data.SOP.Constraint
|
|
|
|
(All)
|
|
|
|
import Data.SOP.NP
|
|
|
|
(NP (..), cpure_NP)
|
|
|
|
import Data.SOP.NS
|
|
|
|
(NS (S))
|
2018-06-30 21:17:08 +02:00
|
|
|
import Data.String
|
|
|
|
(fromString)
|
|
|
|
import Data.Text
|
|
|
|
(Text, pack)
|
2020-10-31 20:45:46 +01:00
|
|
|
import Data.Proxy
|
|
|
|
(Proxy (Proxy))
|
2018-06-30 21:17:08 +02:00
|
|
|
import GHC.TypeLits
|
|
|
|
(KnownSymbol, symbolVal)
|
2020-10-31 20:45:46 +01:00
|
|
|
import Network.HTTP.Types
|
|
|
|
(Status)
|
2019-02-06 11:12:56 +01:00
|
|
|
import qualified Network.HTTP.Types as H
|
2018-06-30 21:17:08 +02:00
|
|
|
import Servant.API
|
|
|
|
((:<|>) ((:<|>)), (:>), AuthProtect, BasicAuth, BasicAuthData,
|
2018-06-26 19:11:28 +02:00
|
|
|
BuildHeadersTo (..), Capture', CaptureAll, Description,
|
2020-11-18 19:57:20 +01:00
|
|
|
EmptyAPI, Fragment, FramingRender (..), FramingUnrender (..),
|
2019-02-02 11:44:35 +01:00
|
|
|
FromSourceIO (..), Header', Headers (..), HttpVersion,
|
|
|
|
IsSecure, MimeRender (mimeRender),
|
2020-11-18 19:57:20 +01:00
|
|
|
MimeUnrender (mimeUnrender), NoContent (NoContent),
|
|
|
|
NoContentVerb, QueryFlag, QueryParam', QueryParams, Raw,
|
|
|
|
ReflectMethod (..), RemoteHost, ReqBody', SBoolI, Stream,
|
|
|
|
StreamBody', Summary, ToHttpApiData, ToSourceIO (..), Vault,
|
2021-06-10 17:10:50 +02:00
|
|
|
Verb, WithNamedContext, WithStatus (..), contentType, getHeadersHList,
|
2021-08-28 00:57:37 +02:00
|
|
|
getResponse, toEncodedUrlPiece, toUrlPiece)
|
2018-06-30 21:17:08 +02:00
|
|
|
import Servant.API.ContentTypes
|
2020-10-31 20:45:46 +01:00
|
|
|
(contentTypes, AllMime (allMime), AllMimeUnrender (allMimeUnrender))
|
2020-11-18 19:57:20 +01:00
|
|
|
import Servant.API.TypeLevel (FragmentUnique, AtLeastOneFragment)
|
2018-06-30 21:17:08 +02:00
|
|
|
import Servant.API.Modifiers
|
|
|
|
(FoldRequired, RequiredArgument, foldRequiredArgument)
|
2020-10-31 20:45:46 +01:00
|
|
|
import Servant.API.UVerb
|
|
|
|
(HasStatus, HasStatuses (Statuses, statuses), UVerb, Union, Unique, inject, statusOf, foldMapUnion, matchUnion)
|
2017-09-13 18:36:20 +02:00
|
|
|
|
2019-02-18 18:08:13 +01:00
|
|
|
import Servant.Client.Core.Auth
|
|
|
|
import Servant.Client.Core.BasicAuth
|
|
|
|
import Servant.Client.Core.ClientError
|
|
|
|
import Servant.Client.Core.Request
|
|
|
|
import Servant.Client.Core.Response
|
|
|
|
import Servant.Client.Core.RunClient
|
2017-09-13 18:36:20 +02:00
|
|
|
|
|
|
|
-- * Accessing APIs as a Client
|
|
|
|
|
|
|
|
-- | 'clientIn' allows you to produce operations to query an API from a client
|
|
|
|
-- within a 'RunClient' monad.
|
|
|
|
--
|
|
|
|
-- > type MyApi = "books" :> Get '[JSON] [Book] -- GET /books
|
|
|
|
-- > :<|> "books" :> ReqBody '[JSON] Book :> Post '[JSON] Book -- POST /books
|
|
|
|
-- >
|
|
|
|
-- > myApi :: Proxy MyApi
|
|
|
|
-- > myApi = Proxy
|
|
|
|
-- >
|
|
|
|
-- > clientM :: Proxy ClientM
|
|
|
|
-- > clientM = Proxy
|
|
|
|
-- >
|
|
|
|
-- > getAllBooks :: ClientM [Book]
|
|
|
|
-- > postNewBook :: Book -> ClientM Book
|
|
|
|
-- > (getAllBooks :<|> postNewBook) = myApi `clientIn` clientM
|
|
|
|
clientIn :: HasClient m api => Proxy api -> Proxy m -> Client m api
|
|
|
|
clientIn p pm = clientWithRoute pm p defaultRequest
|
|
|
|
|
|
|
|
|
|
|
|
-- | This class lets us define how each API combinator influences the creation
|
|
|
|
-- of an HTTP request.
|
|
|
|
--
|
|
|
|
-- Unless you are writing a new backend for @servant-client-core@ or new
|
|
|
|
-- combinators that you want to support client-generation, you can ignore this
|
|
|
|
-- class.
|
|
|
|
class RunClient m => HasClient m api where
|
|
|
|
type Client (m :: * -> *) (api :: *) :: *
|
|
|
|
clientWithRoute :: Proxy m -> Proxy api -> Request -> Client m api
|
2018-03-23 17:36:24 +01:00
|
|
|
hoistClientMonad
|
|
|
|
:: Proxy m
|
|
|
|
-> Proxy api
|
|
|
|
-> (forall x. mon x -> mon' x)
|
|
|
|
-> Client mon api
|
|
|
|
-> Client mon' api
|
2017-09-13 18:36:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
-- | 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 :: ClientM [Book]
|
|
|
|
-- > postNewBook :: Book -> ClientM Book
|
|
|
|
-- > (getAllBooks :<|> postNewBook) = client myApi
|
|
|
|
instance (HasClient m a, HasClient m b) => HasClient m (a :<|> b) where
|
|
|
|
type Client m (a :<|> b) = Client m a :<|> Client m b
|
|
|
|
clientWithRoute pm Proxy req =
|
|
|
|
clientWithRoute pm (Proxy :: Proxy a) req :<|>
|
|
|
|
clientWithRoute pm (Proxy :: Proxy b) req
|
|
|
|
|
2018-03-23 17:36:24 +01:00
|
|
|
hoistClientMonad pm _ f (ca :<|> cb) =
|
|
|
|
hoistClientMonad pm (Proxy :: Proxy a) f ca :<|>
|
|
|
|
hoistClientMonad pm (Proxy :: Proxy b) f cb
|
|
|
|
|
2017-09-13 18:36:20 +02:00
|
|
|
-- | Singleton type representing a client for an empty API.
|
|
|
|
data EmptyClient = EmptyClient deriving (Eq, Show, Bounded, Enum)
|
|
|
|
|
|
|
|
-- | The client for 'EmptyAPI' is simply 'EmptyClient'.
|
|
|
|
--
|
|
|
|
-- > type MyAPI = "books" :> Get '[JSON] [Book] -- GET /books
|
|
|
|
-- > :<|> "nothing" :> EmptyAPI
|
|
|
|
-- >
|
|
|
|
-- > myApi :: Proxy MyApi
|
|
|
|
-- > myApi = Proxy
|
|
|
|
-- >
|
|
|
|
-- > getAllBooks :: ClientM [Book]
|
|
|
|
-- > (getAllBooks :<|> EmptyClient) = client myApi
|
|
|
|
instance RunClient m => HasClient m EmptyAPI where
|
|
|
|
type Client m EmptyAPI = EmptyClient
|
|
|
|
clientWithRoute _pm Proxy _ = EmptyClient
|
2018-03-23 17:36:24 +01:00
|
|
|
hoistClientMonad _ _ _ EmptyClient = EmptyClient
|
2017-09-13 18:36:20 +02:00
|
|
|
|
|
|
|
-- | 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 -> ClientM Book
|
|
|
|
-- > getBook = client myApi
|
|
|
|
-- > -- then you can just use "getBook" to query that endpoint
|
|
|
|
instance (KnownSymbol capture, ToHttpApiData a, HasClient m api)
|
2018-02-09 11:05:30 +01:00
|
|
|
=> HasClient m (Capture' mods capture a :> api) where
|
2017-09-13 18:36:20 +02:00
|
|
|
|
2018-02-09 11:05:30 +01:00
|
|
|
type Client m (Capture' mods capture a :> api) =
|
2017-09-13 18:36:20 +02:00
|
|
|
a -> Client m api
|
|
|
|
|
|
|
|
clientWithRoute pm Proxy req val =
|
|
|
|
clientWithRoute pm (Proxy :: Proxy api)
|
|
|
|
(appendToPath p req)
|
|
|
|
|
|
|
|
where p = (toUrlPiece val)
|
|
|
|
|
2018-03-23 17:36:24 +01:00
|
|
|
hoistClientMonad pm _ f cl = \a ->
|
|
|
|
hoistClientMonad pm (Proxy :: Proxy api) f (cl a)
|
|
|
|
|
2017-09-13 18:36:20 +02:00
|
|
|
-- | If you use a 'CaptureAll' in one of your endpoints in your API,
|
|
|
|
-- the corresponding querying function will automatically take an
|
|
|
|
-- additional argument of a list of the type specified by your
|
|
|
|
-- 'CaptureAll'. 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 these values are turned into text by specifying
|
|
|
|
-- a 'ToHttpApiData' instance of your type.
|
|
|
|
--
|
|
|
|
-- Example:
|
|
|
|
--
|
|
|
|
-- > type MyAPI = "src" :> CaptureAll Text -> Get '[JSON] SourceFile
|
|
|
|
-- >
|
|
|
|
-- > myApi :: Proxy
|
|
|
|
-- > myApi = Proxy
|
|
|
|
--
|
|
|
|
-- > getSourceFile :: [Text] -> ClientM SourceFile
|
|
|
|
-- > getSourceFile = client myApi
|
|
|
|
-- > -- then you can use "getSourceFile" to query that endpoint
|
|
|
|
instance (KnownSymbol capture, ToHttpApiData a, HasClient m sublayout)
|
|
|
|
=> HasClient m (CaptureAll capture a :> sublayout) where
|
|
|
|
|
|
|
|
type Client m (CaptureAll capture a :> sublayout) =
|
|
|
|
[a] -> Client m sublayout
|
|
|
|
|
|
|
|
clientWithRoute pm Proxy req vals =
|
|
|
|
clientWithRoute pm (Proxy :: Proxy sublayout)
|
|
|
|
(foldl' (flip appendToPath) req ps)
|
|
|
|
|
|
|
|
where ps = map (toUrlPiece) vals
|
|
|
|
|
2018-03-23 17:36:24 +01:00
|
|
|
hoistClientMonad pm _ f cl = \as ->
|
|
|
|
hoistClientMonad pm (Proxy :: Proxy sublayout) f (cl as)
|
|
|
|
|
2018-07-11 00:39:38 +02:00
|
|
|
instance {-# OVERLAPPABLE #-}
|
2017-09-13 18:36:20 +02:00
|
|
|
-- Note [Non-Empty Content Types]
|
|
|
|
( RunClient m, MimeUnrender ct a, ReflectMethod method, cts' ~ (ct ': cts)
|
|
|
|
) => HasClient m (Verb method status cts' a) where
|
|
|
|
type Client m (Verb method status cts' a) = m a
|
|
|
|
clientWithRoute _pm Proxy req = do
|
|
|
|
response <- runRequest req
|
|
|
|
{ requestAccept = fromList $ toList accept
|
|
|
|
, requestMethod = method
|
|
|
|
}
|
|
|
|
response `decodedAs` (Proxy :: Proxy ct)
|
|
|
|
where
|
|
|
|
accept = contentTypes (Proxy :: Proxy ct)
|
|
|
|
method = reflectMethod (Proxy :: Proxy method)
|
|
|
|
|
2018-03-23 17:36:24 +01:00
|
|
|
hoistClientMonad _ _ f ma = f ma
|
|
|
|
|
2018-07-11 00:39:38 +02:00
|
|
|
instance {-# OVERLAPPING #-}
|
2017-09-13 18:36:20 +02:00
|
|
|
( RunClient m, ReflectMethod method
|
|
|
|
) => HasClient m (Verb method status cts NoContent) where
|
|
|
|
type Client m (Verb method status cts NoContent)
|
|
|
|
= m NoContent
|
|
|
|
clientWithRoute _pm Proxy req = do
|
|
|
|
_response <- runRequest req { requestMethod = method }
|
|
|
|
return NoContent
|
|
|
|
where method = reflectMethod (Proxy :: Proxy method)
|
|
|
|
|
2018-03-23 17:36:24 +01:00
|
|
|
hoistClientMonad _ _ f ma = f ma
|
|
|
|
|
2019-09-08 14:04:18 +02:00
|
|
|
instance (RunClient m, ReflectMethod method) =>
|
|
|
|
HasClient m (NoContentVerb method) where
|
2019-09-07 17:25:11 +02:00
|
|
|
type Client m (NoContentVerb method)
|
|
|
|
= m NoContent
|
|
|
|
clientWithRoute _pm Proxy req = do
|
|
|
|
_response <- runRequest req { requestMethod = method }
|
|
|
|
return NoContent
|
|
|
|
where method = reflectMethod (Proxy :: Proxy method)
|
|
|
|
|
|
|
|
hoistClientMonad _ _ f ma = f ma
|
|
|
|
|
2018-07-11 00:39:38 +02:00
|
|
|
instance {-# OVERLAPPING #-}
|
2017-09-13 18:36:20 +02:00
|
|
|
-- Note [Non-Empty Content Types]
|
|
|
|
( RunClient m, MimeUnrender ct a, BuildHeadersTo ls
|
|
|
|
, ReflectMethod method, cts' ~ (ct ': cts)
|
|
|
|
) => HasClient m (Verb method status cts' (Headers ls a)) where
|
|
|
|
type Client m (Verb method status cts' (Headers ls a))
|
|
|
|
= m (Headers ls a)
|
|
|
|
clientWithRoute _pm Proxy req = do
|
|
|
|
response <- runRequest req
|
|
|
|
{ requestMethod = method
|
|
|
|
, requestAccept = fromList $ toList accept
|
|
|
|
}
|
2019-08-11 21:19:34 +02:00
|
|
|
val <- response `decodedAs` (Proxy :: Proxy ct)
|
|
|
|
return $ Headers { getResponse = val
|
|
|
|
, getHeadersHList = buildHeadersTo . toList $ responseHeaders response
|
|
|
|
}
|
2017-09-13 18:36:20 +02:00
|
|
|
where method = reflectMethod (Proxy :: Proxy method)
|
|
|
|
accept = contentTypes (Proxy :: Proxy ct)
|
|
|
|
|
2018-03-23 17:36:24 +01:00
|
|
|
hoistClientMonad _ _ f ma = f ma
|
|
|
|
|
2018-07-11 00:39:38 +02:00
|
|
|
instance {-# OVERLAPPING #-}
|
2017-09-13 18:36:20 +02:00
|
|
|
( RunClient m, BuildHeadersTo ls, ReflectMethod method
|
|
|
|
) => HasClient m (Verb method status cts (Headers ls NoContent)) where
|
|
|
|
type Client m (Verb method status cts (Headers ls NoContent))
|
|
|
|
= m (Headers ls NoContent)
|
|
|
|
clientWithRoute _pm Proxy req = do
|
|
|
|
let method = reflectMethod (Proxy :: Proxy method)
|
|
|
|
response <- runRequest req { requestMethod = method }
|
|
|
|
return $ Headers { getResponse = NoContent
|
|
|
|
, getHeadersHList = buildHeadersTo . toList $ responseHeaders response
|
|
|
|
}
|
|
|
|
|
2018-03-23 17:36:24 +01:00
|
|
|
hoistClientMonad _ _ f ma = f ma
|
|
|
|
|
2020-10-31 20:45:46 +01:00
|
|
|
data ClientParseError = ClientParseError MediaType String | ClientStatusMismatch | ClientNoMatchingStatus
|
|
|
|
deriving (Eq, Show)
|
|
|
|
|
2021-06-10 17:10:50 +02:00
|
|
|
class UnrenderResponse (cts :: [*]) (a :: *) where
|
|
|
|
unrenderResponse :: Seq.Seq H.Header -> BL.ByteString -> Proxy cts
|
|
|
|
-> [Either (MediaType, String) a]
|
|
|
|
|
|
|
|
instance {-# OVERLAPPABLE #-} AllMimeUnrender cts a => UnrenderResponse cts a where
|
|
|
|
unrenderResponse _ body = map parse . allMimeUnrender
|
|
|
|
where parse (mediaType, parser) = left ((,) mediaType) (parser body)
|
|
|
|
|
|
|
|
instance {-# OVERLAPPING #-} forall cts a h . (UnrenderResponse cts a, BuildHeadersTo h)
|
|
|
|
=> UnrenderResponse cts (Headers h a) where
|
|
|
|
unrenderResponse hs body = (map . fmap) setHeaders . unrenderResponse hs body
|
|
|
|
where
|
|
|
|
setHeaders :: a -> Headers h a
|
|
|
|
setHeaders x = Headers x (buildHeadersTo (toList hs))
|
|
|
|
|
|
|
|
instance {-# OVERLAPPING #-} UnrenderResponse cts a
|
|
|
|
=> UnrenderResponse cts (WithStatus n a) where
|
|
|
|
unrenderResponse hs body = (map . fmap) WithStatus . unrenderResponse hs body
|
|
|
|
|
2020-10-31 20:45:46 +01:00
|
|
|
instance {-# OVERLAPPING #-}
|
|
|
|
( RunClient m,
|
|
|
|
contentTypes ~ (contentType ': otherContentTypes),
|
|
|
|
-- ('otherContentTypes' should be '_', but even -XPartialTypeSignatures does not seem
|
|
|
|
-- allow this in instance types as of 8.8.3.)
|
|
|
|
as ~ (a ': as'),
|
|
|
|
AllMime contentTypes,
|
|
|
|
ReflectMethod method,
|
2021-06-10 17:10:50 +02:00
|
|
|
All (UnrenderResponse contentTypes) as,
|
2020-10-31 20:45:46 +01:00
|
|
|
All HasStatus as, HasStatuses as',
|
|
|
|
Unique (Statuses as)
|
|
|
|
) =>
|
|
|
|
HasClient m (UVerb method contentTypes as)
|
|
|
|
where
|
|
|
|
type Client m (UVerb method contentTypes as) = m (Union as)
|
|
|
|
|
|
|
|
clientWithRoute _ _ request = do
|
|
|
|
let accept = Seq.fromList . allMime $ Proxy @contentTypes
|
|
|
|
-- offering to accept all mime types listed in the api gives best compatibility. eg.,
|
|
|
|
-- we might not own the server implementation, and the server may choose to support
|
|
|
|
-- only part of the api.
|
|
|
|
|
|
|
|
method = reflectMethod $ Proxy @method
|
|
|
|
acceptStatus = statuses (Proxy @as)
|
|
|
|
response <- runRequestAcceptStatus (Just acceptStatus) request {requestMethod = method, requestAccept = accept}
|
|
|
|
responseContentType <- checkContentTypeHeader response
|
|
|
|
unless (any (matches responseContentType) accept) $ do
|
|
|
|
throwClientError $ UnsupportedContentType responseContentType response
|
|
|
|
|
|
|
|
let status = responseStatusCode response
|
|
|
|
body = responseBody response
|
2021-06-10 17:10:50 +02:00
|
|
|
headers = responseHeaders response
|
|
|
|
res = tryParsers status $ mimeUnrenders (Proxy @contentTypes) headers body
|
2020-10-31 20:45:46 +01:00
|
|
|
case res of
|
|
|
|
Left errors -> throwClientError $ DecodeFailure (T.pack (show errors)) response
|
|
|
|
Right x -> return x
|
|
|
|
where
|
|
|
|
-- | Given a list of parsers of 'mkres', returns the first one that succeeds and all the
|
|
|
|
-- failures it encountered along the way
|
|
|
|
-- TODO; better name, rewrite haddocs.
|
|
|
|
tryParsers :: forall xs. All HasStatus xs => Status -> NP ([] :.: Either (MediaType, String)) xs -> Either [ClientParseError] (Union xs)
|
|
|
|
tryParsers _ Nil = Left [ClientNoMatchingStatus]
|
|
|
|
tryParsers status (Comp x :* xs)
|
|
|
|
| status == statusOf (Comp x) =
|
|
|
|
case partitionEithers x of
|
|
|
|
(err', []) -> (map (uncurry ClientParseError) err' ++) +++ S $ tryParsers status xs
|
|
|
|
(_, (res : _)) -> Right . inject . I $ res
|
|
|
|
| otherwise = -- no reason to parse in the first place. This ain't the one we're looking for
|
|
|
|
(ClientStatusMismatch :) +++ S $ tryParsers status xs
|
|
|
|
|
|
|
|
-- | Given a list of types, parses the given response body as each type
|
|
|
|
mimeUnrenders ::
|
|
|
|
forall cts xs.
|
2021-06-10 17:10:50 +02:00
|
|
|
All (UnrenderResponse cts) xs =>
|
2020-10-31 20:45:46 +01:00
|
|
|
Proxy cts ->
|
2021-06-10 17:10:50 +02:00
|
|
|
Seq.Seq H.Header ->
|
2020-10-31 20:45:46 +01:00
|
|
|
BL.ByteString ->
|
|
|
|
NP ([] :.: Either (MediaType, String)) xs
|
2021-06-10 17:10:50 +02:00
|
|
|
mimeUnrenders ctp headers body = cpure_NP
|
|
|
|
(Proxy @(UnrenderResponse cts))
|
|
|
|
(Comp . unrenderResponse headers body $ ctp)
|
2020-10-31 20:45:46 +01:00
|
|
|
|
|
|
|
hoistClientMonad _ _ nt s = nt s
|
|
|
|
|
2018-07-11 00:39:38 +02:00
|
|
|
instance {-# OVERLAPPABLE #-}
|
2018-11-01 18:42:30 +01:00
|
|
|
( RunStreamingClient m, MimeUnrender ct chunk, ReflectMethod method,
|
2018-06-26 19:11:28 +02:00
|
|
|
FramingUnrender framing, FromSourceIO chunk a
|
|
|
|
) => HasClient m (Stream method status framing ct a) where
|
2017-10-20 21:09:11 +02:00
|
|
|
|
2018-11-01 18:42:30 +01:00
|
|
|
type Client m (Stream method status framing ct a) = m a
|
2018-06-26 19:11:28 +02:00
|
|
|
|
|
|
|
hoistClientMonad _ _ f ma = f ma
|
2017-10-25 02:12:21 +02:00
|
|
|
|
2018-11-01 18:42:30 +01:00
|
|
|
clientWithRoute _pm Proxy req = withStreamingRequest req' $ \gres -> do
|
|
|
|
let mimeUnrender' = mimeUnrender (Proxy :: Proxy ct) :: BL.ByteString -> Either String chunk
|
|
|
|
framingUnrender' = framingUnrender (Proxy :: Proxy framing) mimeUnrender'
|
2019-02-06 11:12:56 +01:00
|
|
|
return $ fromSourceIO $ framingUnrender' $ responseBody gres
|
2018-11-01 18:42:30 +01:00
|
|
|
where
|
|
|
|
req' = req
|
|
|
|
{ requestAccept = fromList [contentType (Proxy :: Proxy ct)]
|
|
|
|
, requestMethod = reflectMethod (Proxy :: Proxy method)
|
|
|
|
}
|
2017-09-13 18:36:20 +02:00
|
|
|
|
2019-07-26 12:30:06 +02:00
|
|
|
instance {-# OVERLAPPING #-}
|
|
|
|
( RunStreamingClient m, MimeUnrender ct chunk, ReflectMethod method,
|
|
|
|
FramingUnrender framing, FromSourceIO chunk a,
|
|
|
|
BuildHeadersTo hs
|
|
|
|
) => HasClient m (Stream method status framing ct (Headers hs a)) where
|
|
|
|
|
|
|
|
type Client m (Stream method status framing ct (Headers hs a)) = m (Headers hs a)
|
|
|
|
|
|
|
|
hoistClientMonad _ _ f ma = f ma
|
|
|
|
|
|
|
|
clientWithRoute _pm Proxy req = withStreamingRequest req' $ \gres -> do
|
|
|
|
let mimeUnrender' = mimeUnrender (Proxy :: Proxy ct) :: BL.ByteString -> Either String chunk
|
|
|
|
framingUnrender' = framingUnrender (Proxy :: Proxy framing) mimeUnrender'
|
|
|
|
val = fromSourceIO $ framingUnrender' $ responseBody gres
|
|
|
|
return $ Headers
|
|
|
|
{ getResponse = val
|
|
|
|
, getHeadersHList = buildHeadersTo . toList $ responseHeaders gres
|
|
|
|
}
|
|
|
|
|
|
|
|
where
|
|
|
|
req' = req
|
|
|
|
{ requestAccept = fromList [contentType (Proxy :: Proxy ct)]
|
|
|
|
, requestMethod = reflectMethod (Proxy :: Proxy method)
|
|
|
|
}
|
|
|
|
|
2017-09-13 18:36:20 +02:00
|
|
|
-- | 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 -> 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
|
2017-12-10 13:25:14 +01:00
|
|
|
instance (KnownSymbol sym, ToHttpApiData a, HasClient m api, SBoolI (FoldRequired mods))
|
|
|
|
=> HasClient m (Header' mods sym a :> api) where
|
2017-09-13 18:36:20 +02:00
|
|
|
|
2017-12-10 13:25:14 +01:00
|
|
|
type Client m (Header' mods sym a :> api) =
|
|
|
|
RequiredArgument mods a -> Client m api
|
2017-09-13 18:36:20 +02:00
|
|
|
|
|
|
|
clientWithRoute pm Proxy req mval =
|
2017-12-10 13:25:14 +01:00
|
|
|
clientWithRoute pm (Proxy :: Proxy api) $ foldRequiredArgument
|
|
|
|
(Proxy :: Proxy mods) add (maybe req add) mval
|
|
|
|
where
|
|
|
|
hname = fromString $ symbolVal (Proxy :: Proxy sym)
|
2017-09-13 18:36:20 +02:00
|
|
|
|
2017-12-10 13:25:14 +01:00
|
|
|
add :: a -> Request
|
|
|
|
add value = addHeader hname value req
|
2017-09-13 18:36:20 +02:00
|
|
|
|
2018-03-23 17:36:24 +01:00
|
|
|
hoistClientMonad pm _ f cl = \arg ->
|
|
|
|
hoistClientMonad pm (Proxy :: Proxy api) f (cl arg)
|
|
|
|
|
2017-09-13 18:36:20 +02:00
|
|
|
-- | Using a 'HttpVersion' combinator in your API doesn't affect the client
|
|
|
|
-- functions.
|
|
|
|
instance HasClient m api
|
|
|
|
=> HasClient m (HttpVersion :> api) where
|
|
|
|
|
|
|
|
type Client m (HttpVersion :> api) =
|
|
|
|
Client m api
|
|
|
|
|
|
|
|
clientWithRoute pm Proxy =
|
|
|
|
clientWithRoute pm (Proxy :: Proxy api)
|
|
|
|
|
2018-03-23 17:36:24 +01:00
|
|
|
hoistClientMonad pm _ f cl = hoistClientMonad pm (Proxy :: Proxy api) f cl
|
|
|
|
|
2017-09-13 18:36:20 +02:00
|
|
|
-- | Ignore @'Summary'@ in client functions.
|
|
|
|
instance HasClient m api => HasClient m (Summary desc :> api) where
|
|
|
|
type Client m (Summary desc :> api) = Client m api
|
|
|
|
|
|
|
|
clientWithRoute pm _ = clientWithRoute pm (Proxy :: Proxy api)
|
|
|
|
|
2018-03-23 17:36:24 +01:00
|
|
|
hoistClientMonad pm _ f cl = hoistClientMonad pm (Proxy :: Proxy api) f cl
|
|
|
|
|
2017-09-13 18:36:20 +02:00
|
|
|
-- | Ignore @'Description'@ in client functions.
|
|
|
|
instance HasClient m api => HasClient m (Description desc :> api) where
|
|
|
|
type Client m (Description desc :> api) = Client m api
|
|
|
|
|
|
|
|
clientWithRoute pm _ = clientWithRoute pm (Proxy :: Proxy api)
|
|
|
|
|
2018-03-23 17:36:24 +01:00
|
|
|
hoistClientMonad pm _ f cl = hoistClientMonad pm (Proxy :: Proxy api) f cl
|
|
|
|
|
2017-09-13 18:36:20 +02:00
|
|
|
-- | 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 -> 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
|
2017-12-10 13:25:14 +01:00
|
|
|
instance (KnownSymbol sym, ToHttpApiData a, HasClient m api, SBoolI (FoldRequired mods))
|
|
|
|
=> HasClient m (QueryParam' mods sym a :> api) where
|
2017-09-13 18:36:20 +02:00
|
|
|
|
2017-12-10 13:25:14 +01:00
|
|
|
type Client m (QueryParam' mods sym a :> api) =
|
|
|
|
RequiredArgument mods a -> Client m api
|
2017-09-13 18:36:20 +02:00
|
|
|
|
|
|
|
-- if mparam = Nothing, we don't add it to the query string
|
|
|
|
clientWithRoute pm Proxy req mparam =
|
2017-12-10 13:25:14 +01:00
|
|
|
clientWithRoute pm (Proxy :: Proxy api) $ foldRequiredArgument
|
|
|
|
(Proxy :: Proxy mods) add (maybe req add) mparam
|
|
|
|
where
|
|
|
|
add :: a -> Request
|
2021-08-28 00:57:37 +02:00
|
|
|
add param = appendToQueryString pname (Just $ encodeQueryParam param) req
|
2017-09-13 18:36:20 +02:00
|
|
|
|
2017-12-10 13:25:14 +01:00
|
|
|
pname :: Text
|
|
|
|
pname = pack $ symbolVal (Proxy :: Proxy sym)
|
2017-09-13 18:36:20 +02:00
|
|
|
|
2018-03-23 17:36:24 +01:00
|
|
|
hoistClientMonad pm _ f cl = \arg ->
|
|
|
|
hoistClientMonad pm (Proxy :: Proxy api) f (cl arg)
|
|
|
|
|
2021-08-28 00:57:37 +02:00
|
|
|
encodeQueryParam :: ToHttpApiData a => a -> BS.ByteString
|
|
|
|
encodeQueryParam = BL.toStrict . toLazyByteString . toEncodedUrlPiece
|
|
|
|
|
2017-09-13 18:36:20 +02:00
|
|
|
-- | 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] -> 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
|
|
|
|
instance (KnownSymbol sym, ToHttpApiData a, HasClient m api)
|
|
|
|
=> HasClient m (QueryParams sym a :> api) where
|
|
|
|
|
|
|
|
type Client m (QueryParams sym a :> api) =
|
|
|
|
[a] -> Client m api
|
|
|
|
|
|
|
|
clientWithRoute pm Proxy req paramlist =
|
|
|
|
clientWithRoute pm (Proxy :: Proxy api)
|
|
|
|
(foldl' (\ req' -> maybe req' (flip (appendToQueryString pname) req' . Just))
|
|
|
|
req
|
|
|
|
paramlist'
|
|
|
|
)
|
|
|
|
|
|
|
|
where pname = pack $ symbolVal (Proxy :: Proxy sym)
|
2021-08-28 00:57:37 +02:00
|
|
|
paramlist' = map (Just . encodeQueryParam) paramlist
|
2017-09-13 18:36:20 +02:00
|
|
|
|
2018-03-23 17:36:24 +01:00
|
|
|
hoistClientMonad pm _ f cl = \as ->
|
|
|
|
hoistClientMonad pm (Proxy :: Proxy api) f (cl as)
|
|
|
|
|
2017-09-13 18:36:20 +02:00
|
|
|
-- | 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 -> 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
|
|
|
|
instance (KnownSymbol sym, HasClient m api)
|
|
|
|
=> HasClient m (QueryFlag sym :> api) where
|
|
|
|
|
|
|
|
type Client m (QueryFlag sym :> api) =
|
|
|
|
Bool -> Client m api
|
|
|
|
|
|
|
|
clientWithRoute pm Proxy req flag =
|
|
|
|
clientWithRoute pm (Proxy :: Proxy api)
|
|
|
|
(if flag
|
|
|
|
then appendToQueryString paramname Nothing req
|
|
|
|
else req
|
|
|
|
)
|
|
|
|
|
|
|
|
where paramname = pack $ symbolVal (Proxy :: Proxy sym)
|
|
|
|
|
2018-03-23 17:36:24 +01:00
|
|
|
hoistClientMonad pm _ f cl = \b ->
|
|
|
|
hoistClientMonad pm (Proxy :: Proxy api) f (cl b)
|
2017-09-13 18:36:20 +02:00
|
|
|
|
|
|
|
-- | Pick a 'Method' and specify where the server you want to query is. You get
|
|
|
|
-- back the full `Response`.
|
|
|
|
instance RunClient m => HasClient m Raw where
|
|
|
|
type Client m Raw
|
|
|
|
= H.Method -> m Response
|
|
|
|
|
|
|
|
clientWithRoute :: Proxy m -> Proxy Raw -> Request -> Client m Raw
|
|
|
|
clientWithRoute _pm Proxy req httpMethod = do
|
|
|
|
runRequest req { requestMethod = httpMethod }
|
|
|
|
|
2018-03-23 17:36:24 +01:00
|
|
|
hoistClientMonad _ _ f cl = \meth -> f (cl meth)
|
|
|
|
|
2017-09-13 18:36:20 +02:00
|
|
|
-- | 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 -> ClientM Book
|
|
|
|
-- > addBook = client myApi
|
|
|
|
-- > -- then you can just use "addBook" to query that endpoint
|
|
|
|
instance (MimeRender ct a, HasClient m api)
|
2017-12-10 13:25:14 +01:00
|
|
|
=> HasClient m (ReqBody' mods (ct ': cts) a :> api) where
|
2017-09-13 18:36:20 +02:00
|
|
|
|
2017-12-10 13:25:14 +01:00
|
|
|
type Client m (ReqBody' mods (ct ': cts) a :> api) =
|
2017-09-13 18:36:20 +02:00
|
|
|
a -> Client m api
|
|
|
|
|
|
|
|
clientWithRoute pm Proxy req body =
|
|
|
|
clientWithRoute pm (Proxy :: Proxy api)
|
|
|
|
(let ctProxy = Proxy :: Proxy ct
|
|
|
|
in setRequestBodyLBS (mimeRender ctProxy body)
|
|
|
|
-- We use first contentType from the Accept list
|
|
|
|
(contentType ctProxy)
|
|
|
|
req
|
|
|
|
)
|
|
|
|
|
2018-03-23 17:36:24 +01:00
|
|
|
hoistClientMonad pm _ f cl = \a ->
|
|
|
|
hoistClientMonad pm (Proxy :: Proxy api) f (cl a)
|
|
|
|
|
2018-06-26 19:11:28 +02:00
|
|
|
instance
|
2019-02-02 11:44:35 +01:00
|
|
|
( HasClient m api, MimeRender ctype chunk, FramingRender framing, ToSourceIO chunk a
|
2018-11-09 20:49:53 +01:00
|
|
|
) => HasClient m (StreamBody' mods framing ctype a :> api)
|
2018-06-26 19:11:28 +02:00
|
|
|
where
|
|
|
|
|
2018-11-09 20:49:53 +01:00
|
|
|
type Client m (StreamBody' mods framing ctype a :> api) = a -> Client m api
|
2018-06-26 19:11:28 +02:00
|
|
|
|
|
|
|
hoistClientMonad pm _ f cl = \a ->
|
|
|
|
hoistClientMonad pm (Proxy :: Proxy api) f (cl a)
|
|
|
|
|
2019-02-02 11:44:35 +01:00
|
|
|
clientWithRoute pm Proxy req body
|
|
|
|
= clientWithRoute pm (Proxy :: Proxy api)
|
2019-02-06 11:12:56 +01:00
|
|
|
$ setRequestBody (RequestBodySource sourceIO) (contentType ctypeP) req
|
2019-02-02 11:44:35 +01:00
|
|
|
where
|
|
|
|
ctypeP = Proxy :: Proxy ctype
|
|
|
|
framingP = Proxy :: Proxy framing
|
|
|
|
|
|
|
|
sourceIO = framingRender
|
|
|
|
framingP
|
|
|
|
(mimeRender ctypeP :: chunk -> BL.ByteString)
|
|
|
|
(toSourceIO body)
|
|
|
|
|
2017-09-13 18:36:20 +02:00
|
|
|
-- | Make the querying function append @path@ to the request path.
|
|
|
|
instance (KnownSymbol path, HasClient m api) => HasClient m (path :> api) where
|
|
|
|
type Client m (path :> api) = Client m api
|
|
|
|
|
|
|
|
clientWithRoute pm Proxy req =
|
|
|
|
clientWithRoute pm (Proxy :: Proxy api)
|
|
|
|
(appendToPath p req)
|
|
|
|
|
|
|
|
where p = pack $ symbolVal (Proxy :: Proxy path)
|
|
|
|
|
2018-03-23 17:36:24 +01:00
|
|
|
hoistClientMonad pm _ f cl = hoistClientMonad pm (Proxy :: Proxy api) f cl
|
|
|
|
|
2017-09-13 18:36:20 +02:00
|
|
|
instance HasClient m api => HasClient m (Vault :> api) where
|
|
|
|
type Client m (Vault :> api) = Client m api
|
|
|
|
|
|
|
|
clientWithRoute pm Proxy req =
|
|
|
|
clientWithRoute pm (Proxy :: Proxy api) req
|
|
|
|
|
2018-03-23 17:36:24 +01:00
|
|
|
hoistClientMonad pm _ f cl = hoistClientMonad pm (Proxy :: Proxy api) f cl
|
|
|
|
|
2017-09-13 18:36:20 +02:00
|
|
|
instance HasClient m api => HasClient m (RemoteHost :> api) where
|
|
|
|
type Client m (RemoteHost :> api) = Client m api
|
|
|
|
|
|
|
|
clientWithRoute pm Proxy req =
|
|
|
|
clientWithRoute pm (Proxy :: Proxy api) req
|
|
|
|
|
2018-03-23 17:36:24 +01:00
|
|
|
hoistClientMonad pm _ f cl = hoistClientMonad pm (Proxy :: Proxy api) f cl
|
|
|
|
|
2017-09-13 18:36:20 +02:00
|
|
|
instance HasClient m api => HasClient m (IsSecure :> api) where
|
|
|
|
type Client m (IsSecure :> api) = Client m api
|
|
|
|
|
|
|
|
clientWithRoute pm Proxy req =
|
|
|
|
clientWithRoute pm (Proxy :: Proxy api) req
|
|
|
|
|
2018-03-23 17:36:24 +01:00
|
|
|
hoistClientMonad pm _ f cl = hoistClientMonad pm (Proxy :: Proxy api) f cl
|
|
|
|
|
2017-09-13 18:36:20 +02:00
|
|
|
instance HasClient m subapi =>
|
|
|
|
HasClient m (WithNamedContext name context subapi) where
|
|
|
|
|
|
|
|
type Client m (WithNamedContext name context subapi) = Client m subapi
|
|
|
|
clientWithRoute pm Proxy = clientWithRoute pm (Proxy :: Proxy subapi)
|
|
|
|
|
2018-03-23 17:36:24 +01:00
|
|
|
hoistClientMonad pm _ f cl = hoistClientMonad pm (Proxy :: Proxy subapi) f cl
|
|
|
|
|
2017-09-13 18:36:20 +02:00
|
|
|
instance ( HasClient m api
|
|
|
|
) => HasClient m (AuthProtect tag :> api) where
|
|
|
|
type Client m (AuthProtect tag :> api)
|
|
|
|
= AuthenticatedRequest (AuthProtect tag) -> Client m api
|
|
|
|
|
|
|
|
clientWithRoute pm Proxy req (AuthenticatedRequest (val,func)) =
|
|
|
|
clientWithRoute pm (Proxy :: Proxy api) (func val req)
|
|
|
|
|
2018-03-23 17:36:24 +01:00
|
|
|
hoistClientMonad pm _ f cl = \authreq ->
|
|
|
|
hoistClientMonad pm (Proxy :: Proxy api) f (cl authreq)
|
|
|
|
|
2020-11-18 19:57:20 +01:00
|
|
|
-- | Ignore @'Fragment'@ in client functions.
|
|
|
|
-- See <https://ietf.org/rfc/rfc2616.html#section-15.1.3> for more details.
|
|
|
|
--
|
|
|
|
-- Example:
|
|
|
|
--
|
|
|
|
-- > type MyApi = "books" :> Fragment Text :> Get '[JSON] [Book]
|
|
|
|
-- >
|
|
|
|
-- > myApi :: Proxy MyApi
|
|
|
|
-- > myApi = Proxy
|
|
|
|
-- >
|
2020-11-22 11:08:32 +01:00
|
|
|
-- > getBooks :: ClientM [Book]
|
|
|
|
-- > getBooks = client myApi
|
2020-11-18 19:57:20 +01:00
|
|
|
-- > -- then you can just use "getBooksBy" to query that endpoint.
|
2020-11-22 11:08:32 +01:00
|
|
|
-- > -- 'getBooks' for all books.
|
2020-11-18 19:57:20 +01:00
|
|
|
#ifdef HAS_TYPE_ERROR
|
|
|
|
instance (AtLeastOneFragment api, FragmentUnique (Fragment a :> api), HasClient m api
|
|
|
|
#else
|
|
|
|
instance ( HasClient m api
|
|
|
|
#endif
|
|
|
|
) => HasClient m (Fragment a :> api) where
|
|
|
|
|
|
|
|
type Client m (Fragment a :> api) = Client m api
|
|
|
|
|
|
|
|
clientWithRoute pm _ = clientWithRoute pm (Proxy :: Proxy api)
|
|
|
|
|
|
|
|
hoistClientMonad pm _ = hoistClientMonad pm (Proxy :: Proxy api)
|
|
|
|
|
2017-09-13 18:36:20 +02:00
|
|
|
-- * Basic Authentication
|
|
|
|
|
|
|
|
instance HasClient m api => HasClient m (BasicAuth realm usr :> api) where
|
|
|
|
type Client m (BasicAuth realm usr :> api) = BasicAuthData -> Client m api
|
|
|
|
|
|
|
|
clientWithRoute pm Proxy req val =
|
|
|
|
clientWithRoute pm (Proxy :: Proxy api) (basicAuthReq val req)
|
|
|
|
|
2018-03-23 17:36:24 +01:00
|
|
|
hoistClientMonad pm _ f cl = \bauth ->
|
|
|
|
hoistClientMonad pm (Proxy :: Proxy api) f (cl bauth)
|
|
|
|
|
2017-09-13 18:36:20 +02:00
|
|
|
|
2019-02-18 18:08:13 +01:00
|
|
|
|
2017-09-13 18:36:20 +02:00
|
|
|
{- Note [Non-Empty Content Types]
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
Rather than have
|
|
|
|
|
|
|
|
instance (..., cts' ~ (ct ': cts)) => ... cts' ...
|
|
|
|
|
|
|
|
It may seem to make more sense to have:
|
|
|
|
|
|
|
|
instance (...) => ... (ct ': cts) ...
|
|
|
|
|
|
|
|
But this means that if another instance exists that does *not* require
|
|
|
|
non-empty lists, but is otherwise more specific, no instance will be overall
|
|
|
|
more specific. This in turn generally means adding yet another instance (one
|
|
|
|
for empty and one for non-empty lists).
|
|
|
|
-}
|
2019-02-18 18:08:13 +01:00
|
|
|
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- helpers
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
checkContentTypeHeader :: RunClient m => Response -> m MediaType
|
|
|
|
checkContentTypeHeader response =
|
|
|
|
case lookup "Content-Type" $ toList $ responseHeaders response of
|
|
|
|
Nothing -> return $ "application"//"octet-stream"
|
|
|
|
Just t -> case parseAccept t of
|
2019-02-18 19:17:46 +01:00
|
|
|
Nothing -> throwClientError $ InvalidContentTypeHeader response
|
2019-02-18 18:08:13 +01:00
|
|
|
Just t' -> return t'
|
|
|
|
|
|
|
|
decodedAs :: forall ct a m. (MimeUnrender ct a, RunClient m)
|
|
|
|
=> Response -> Proxy ct -> m a
|
|
|
|
decodedAs response ct = do
|
|
|
|
responseContentType <- checkContentTypeHeader response
|
|
|
|
unless (any (matches responseContentType) accept) $
|
2019-02-18 19:17:46 +01:00
|
|
|
throwClientError $ UnsupportedContentType responseContentType response
|
2019-02-18 18:08:13 +01:00
|
|
|
case mimeUnrender ct $ responseBody response of
|
2019-02-18 19:17:46 +01:00
|
|
|
Left err -> throwClientError $ DecodeFailure (T.pack err) response
|
2019-02-18 18:08:13 +01:00
|
|
|
Right val -> return val
|
|
|
|
where
|
2020-10-31 20:45:46 +01:00
|
|
|
accept = toList $ contentTypes ct
|