@@ -1,3 +1,4 @@
1+{-# LANGUAGE AllowAmbiguousTypes #-}
12{-# LANGUAGE CPP #-}
23{-# LANGUAGE DataKinds #-}
34{-# LANGUAGE FlexibleContexts #-}
@@ -43,6 +44,7 @@ import Prelude.Compat
4344import Servant.API
4445import Servant.Client.Experimental.Auth
4546import Servant.Client.HttpClient
47+import Servant.Client.Class
4648import Servant.Common.BaseUrl
4749import Servant.Common.BasicAuth
4850import Servant.Common.Req
@@ -60,15 +62,15 @@ import Servant.Common.Req
6062-- > getAllBooks :: ClientM [Book]
6163-- > postNewBook :: Book -> ClientM Book
6264-- > (getAllBooks :<|> postNewBook) = client myApi
63−client :: HasClient api => Proxy api -> Client api
65+client :: HasClient api => Proxy api -> Client m api
6466client p = clientWithRoute p defReq
6567
6668-- | This class lets us define how each API combinator
6769-- influences the creation of an HTTP request. It's mostly
6870-- an internal class, you can just use 'client'.
6971class HasClient api where
70− type Client api :: *
71− clientWithRoute :: Proxy api -> Req -> Client api
72+ type Client (m :: * -> *) (api :: *) :: *
73+ clientWithRoute :: Proxy api -> Req -> Client m api
7274
7375
7476-- | A client querying function for @a ':<|>' b@ will actually hand you
@@ -85,7 +87,7 @@ class HasClient api where
8587-- > postNewBook :: Book -> ClientM Book
8688-- > (getAllBooks :<|> postNewBook) = client myApi
8789instance (HasClient a, HasClient b) => HasClient (a :<|> b) where
88− type Client (a :<|> b) = Client a :<|> Client b
90+ type Client m (a :<|> b) = Client m a :<|> Client m b
8991 clientWithRoute Proxy req =
9092 clientWithRoute (Proxy :: Proxy a) req :<|>
9193 clientWithRoute (Proxy :: Proxy b) req
@@ -104,7 +106,7 @@ data EmptyClient = EmptyClient deriving (Eq, Show, Bounded, Enum)
104106-- > getAllBooks :: ClientM [Book]
105107-- > (getAllBooks :<|> EmptyClient) = client myApi
106108instance HasClient EmptyAPI where
107− type Client EmptyAPI = EmptyClient
109+ type Client m EmptyAPI = EmptyClient
108110 clientWithRoute Proxy _ = EmptyClient
109111
110112-- | If you use a 'Capture' in one of your endpoints in your API,
@@ -129,8 +131,8 @@ instance HasClient EmptyAPI where
129131instance (KnownSymbol capture, ToHttpApiData a, HasClient api)
130132 => HasClient (Capture capture a :> api) where
131133
132− type Client (Capture capture a :> api) =
133− a -> Client api
134+ type Client m (Capture capture a :> api) =
135+ a -> Client m api
134136
135137 clientWithRoute Proxy req val =
136138 clientWithRoute (Proxy :: Proxy api)
@@ -161,8 +163,8 @@ instance (KnownSymbol capture, ToHttpApiData a, HasClient api)
161163instance (KnownSymbol capture, ToHttpApiData a, HasClient sublayout)
162164 => HasClient (CaptureAll capture a :> sublayout) where
163165
164− type Client (CaptureAll capture a :> sublayout) =
165− [a] -> Client sublayout
166+ type Client m (CaptureAll capture a :> sublayout) =
167+ [a] -> Client m sublayout
166168
167169 clientWithRoute Proxy req vals =
168170 clientWithRoute (Proxy :: Proxy sublayout)
@@ -172,42 +174,42 @@ instance (KnownSymbol capture, ToHttpApiData a, HasClient sublayout)
172174
173175instance OVERLAPPABLE_
174176 -- Note [Non-Empty Content Types]
175− (MimeUnrender ct a, ReflectMethod method, cts' ~ (ct ': cts)
177+ (RunClient m ct ([H.Header], a), MimeUnrender ct a, ReflectMethod method, cts' ~ (ct ': cts)
176178 ) => HasClient (Verb method status cts' a) where
177− type Client (Verb method status cts' a) = ClientM a
179+ type Client m (Verb method status cts' a) = m a
178180 clientWithRoute Proxy req = do
179− snd <$> performRequestCT (Proxy :: Proxy ct) method req
181+ snd <$> runRequest (Proxy :: Proxy ct) method req
180182 where method = reflectMethod (Proxy :: Proxy method)
181183
182184instance OVERLAPPING_
183− (ReflectMethod method) => HasClient (Verb method status cts NoContent) where
184− type Client (Verb method status cts NoContent)
185− = ClientM NoContent
185+ (RunClient m NoContent [H.Header], ReflectMethod method) => HasClient (Verb method status cts NoContent) where
186+ type Client m (Verb method status cts NoContent)
187+ = m NoContent
186188 clientWithRoute Proxy req = do
187− performRequestNoBody method req >> return NoContent
189+ runRequest (Proxy :: Proxy NoContent) method req >> return NoContent
188190 where method = reflectMethod (Proxy :: Proxy method)
189191
190192instance OVERLAPPING_
191193 -- Note [Non-Empty Content Types]
192− ( MimeUnrender ct a, BuildHeadersTo ls, ReflectMethod method, cts' ~ (ct ': cts)
194+ ( RunClient m ct ([H.Header], a), MimeUnrender ct a, BuildHeadersTo ls, ReflectMethod method, cts' ~ (ct ': cts)
193195 ) => HasClient (Verb method status cts' (Headers ls a)) where
194− type Client (Verb method status cts' (Headers ls a))
195− = ClientM (Headers ls a)
196+ type Client m (Verb method status cts' (Headers ls a))
197+ = m (Headers ls a)
196198 clientWithRoute Proxy req = do
197199 let method = reflectMethod (Proxy :: Proxy method)
198− (hdrs, resp) <- performRequestCT (Proxy :: Proxy ct) method req
200+ (hdrs, resp) <- runRequest (Proxy :: Proxy ct) method req
199201 return $ Headers { getResponse = resp
200202 , getHeadersHList = buildHeadersTo hdrs
201203 }
202204
203205instance OVERLAPPING_
204− ( BuildHeadersTo ls, ReflectMethod method
206+ ( RunClient m NoContent [H.Header], BuildHeadersTo ls, ReflectMethod method
205207 ) => HasClient (Verb method status cts (Headers ls NoContent)) where
206− type Client (Verb method status cts (Headers ls NoContent))
207− = ClientM (Headers ls NoContent)
208+ type Client m (Verb method status cts (Headers ls NoContent))
209+ = m (Headers ls NoContent)
208210 clientWithRoute Proxy req = do
209211 let method = reflectMethod (Proxy :: Proxy method)
210− hdrs <- performRequestNoBody method req
212+ hdrs <- runRequest method req
211213 return $ Headers { getResponse = NoContent
212214 , getHeadersHList = buildHeadersTo hdrs
213215 }
@@ -241,8 +243,8 @@ instance OVERLAPPING_
241243instance (KnownSymbol sym, ToHttpApiData a, HasClient api)
242244 => HasClient (Header sym a :> api) where
243245
244− type Client (Header sym a :> api) =
245− Maybe a -> Client api
246+ type Client m (Header sym a :> api) =
247+ Maybe a -> Client m api
246248
247249 clientWithRoute Proxy req mval =
248250 clientWithRoute (Proxy :: Proxy api)
@@ -258,21 +260,21 @@ instance (KnownSymbol sym, ToHttpApiData a, HasClient api)
258260instance HasClient api
259261 => HasClient (HttpVersion :> api) where
260262
261− type Client (HttpVersion :> api) =
262− Client api
263+ type Client m (HttpVersion :> api) =
264+ Client m api
263265
264266 clientWithRoute Proxy =
265267 clientWithRoute (Proxy :: Proxy api)
266268
267269-- | Ignore @'Summary'@ in client functions.
268270instance HasClient api => HasClient (Summary desc :> api) where
269− type Client (Summary desc :> api) = Client api
271+ type Client m (Summary desc :> api) = Client m api
270272
271273 clientWithRoute _ = clientWithRoute (Proxy :: Proxy api)
272274
273275-- | Ignore @'Description'@ in client functions.
274276instance HasClient api => HasClient (Description desc :> api) where
275− type Client (Description desc :> api) = Client api
277+ type Client m (Description desc :> api) = Client m api
276278
277279 clientWithRoute _ = clientWithRoute (Proxy :: Proxy api)
278280
@@ -304,8 +306,8 @@ instance HasClient api => HasClient (Description desc :> api) where
304306instance (KnownSymbol sym, ToHttpApiData a, HasClient api)
305307 => HasClient (QueryParam sym a :> api) where
306308
307− type Client (QueryParam sym a :> api) =
308− Maybe a -> Client api
309+ type Client m (QueryParam sym a :> api) =
310+ Maybe a -> Client m api
309311
310312 -- if mparam = Nothing, we don't add it to the query string
311313 clientWithRoute Proxy req mparam =
@@ -349,8 +351,8 @@ instance (KnownSymbol sym, ToHttpApiData a, HasClient api)
349351instance (KnownSymbol sym, ToHttpApiData a, HasClient api)
350352 => HasClient (QueryParams sym a :> api) where
351353
352− type Client (QueryParams sym a :> api) =
353− [a] -> Client api
354+ type Client m (QueryParams sym a :> api) =
355+ [a] -> Client m api
354356
355357 clientWithRoute Proxy req paramlist =
356358 clientWithRoute (Proxy :: Proxy api)
@@ -387,8 +389,8 @@ instance (KnownSymbol sym, ToHttpApiData a, HasClient api)
387389instance (KnownSymbol sym, HasClient api)
388390 => HasClient (QueryFlag sym :> api) where
389391
390− type Client (QueryFlag sym :> api) =
391− Bool -> Client api
392+ type Client m (QueryFlag sym :> api) =
393+ Bool -> Client m api
392394
393395 clientWithRoute Proxy req flag =
394396 clientWithRoute (Proxy :: Proxy api)
@@ -403,12 +405,12 @@ instance (KnownSymbol sym, HasClient api)
403405-- | Pick a 'Method' and specify where the server you want to query is. You get
404406-- back the full `Response`.
405407instance HasClient Raw where
406− type Client Raw
407− = H.Method -> ClientM (Int, ByteString, MediaType, [HTTP.Header], Response ByteString)
408+ type Client m Raw
409+ = H.Method -> m (Int, ByteString, MediaType, [HTTP.Header], Response ByteString)
408410
409411 clientWithRoute :: Proxy Raw -> Req -> Client Raw
410412 clientWithRoute Proxy req httpMethod = do
411− performRequest httpMethod req
413+ runRequest (Proxy :: Proxy NoContent) httpMethod req
412414
413415-- | If you use a 'ReqBody' in one of your endpoints in your API,
414416-- the corresponding querying function will automatically take
@@ -431,8 +433,8 @@ instance HasClient Raw where
431433instance (MimeRender ct a, HasClient api)
432434 => HasClient (ReqBody (ct ': cts) a :> api) where
433435
434− type Client (ReqBody (ct ': cts) a :> api) =
435− a -> Client api
436+ type Client m (ReqBody (ct ': cts) a :> api) =
437+ a -> Client m api
436438
437439 clientWithRoute Proxy req body =
438440 clientWithRoute (Proxy :: Proxy api)
@@ -445,7 +447,7 @@ instance (MimeRender ct a, HasClient api)
445447
446448-- | Make the querying function append @path@ to the request path.
447449instance (KnownSymbol path, HasClient api) => HasClient (path :> api) where
448− type Client (path :> api) = Client api
450+ type Client m (path :> api) = Client m api
449451
450452 clientWithRoute Proxy req =
451453 clientWithRoute (Proxy :: Proxy api)
@@ -454,19 +456,19 @@ instance (KnownSymbol path, HasClient api) => HasClient (path :> api) where
454456 where p = symbolVal (Proxy :: Proxy path)
455457
456458instance HasClient api => HasClient (Vault :> api) where
457− type Client (Vault :> api) = Client api
459+ type Client m (Vault :> api) = Client m api
458460
459461 clientWithRoute Proxy req =
460462 clientWithRoute (Proxy :: Proxy api) req
461463
462464instance HasClient api => HasClient (RemoteHost :> api) where
463− type Client (RemoteHost :> api) = Client api
465+ type Client m (RemoteHost :> api) = Client m api
464466
465467 clientWithRoute Proxy req =
466468 clientWithRoute (Proxy :: Proxy api) req
467469
468470instance HasClient api => HasClient (IsSecure :> api) where
469− type Client (IsSecure :> api) = Client api
471+ type Client m (IsSecure :> api) = Client m api
470472
471473 clientWithRoute Proxy req =
472474 clientWithRoute (Proxy :: Proxy api) req
@@ -474,13 +476,13 @@ instance HasClient api => HasClient (IsSecure :> api) where
474476instance HasClient subapi =>
475477 HasClient (WithNamedContext name context subapi) where
476478
477− type Client (WithNamedContext name context subapi) = Client subapi
479+ type Client m (WithNamedContext name context subapi) = Client m subapi
478480 clientWithRoute Proxy = clientWithRoute (Proxy :: Proxy subapi)
479481
480482instance ( HasClient api
481483 ) => HasClient (AuthProtect tag :> api) where
482− type Client (AuthProtect tag :> api)
483− = AuthenticateReq (AuthProtect tag) -> Client api
484+ type Client m (AuthProtect tag :> api)
485+ = AuthenticateReq (AuthProtect tag) -> Client m api
484486
485487 clientWithRoute Proxy req (AuthenticateReq (val,func)) =
486488 clientWithRoute (Proxy :: Proxy api) (func val req)
@@ -488,7 +490,7 @@ instance ( HasClient api
488490-- * Basic Authentication
489491
490492instance HasClient api => HasClient (BasicAuth realm usr :> api) where
491− type Client (BasicAuth realm usr :> api) = BasicAuthData -> Client api
493+ type Client m (BasicAuth realm usr :> api) = BasicAuthData -> Client m api
492494
493495 clientWithRoute Proxy req val =
494496 clientWithRoute (Proxy :: Proxy api) (basicAuthReq val req)