git.iko.soy/typeable/servant
on master help sign in
add hoistClient to HasClient class
200311eAlp Mestanogullari
servant-client-core/src/Servant/Client/Core/Internal/HasClient.hs+65
@@ -6,6 +6,7 @@
6{-# LANGUAGE MultiParamTypeClasses #-}
6{-# LANGUAGE MultiParamTypeClasses #-}
7{-# LANGUAGE OverloadedStrings #-}
7{-# LANGUAGE OverloadedStrings #-}
8{-# LANGUAGE PolyKinds #-}
8{-# LANGUAGE PolyKinds #-}
9+{-# LANGUAGE RankNTypes #-}
9{-# LANGUAGE ScopedTypeVariables #-}
10{-# LANGUAGE ScopedTypeVariables #-}
10{-# LANGUAGE TypeFamilies #-}
11{-# LANGUAGE TypeFamilies #-}
11{-# LANGUAGE TypeOperators #-}
12{-# LANGUAGE TypeOperators #-}
@@ -97,6 +98,12 @@ clientIn p pm = clientWithRoute pm p defaultRequest
97class RunClient m => HasClient m api where
98class RunClient m => HasClient m api where
98 type Client (m :: * -> *) (api :: *) :: *
99 type Client (m :: * -> *) (api :: *) :: *
99 clientWithRoute :: Proxy m -> Proxy api -> Request -> Client m api
100 clientWithRoute :: Proxy m -> Proxy api -> Request -> Client m api
101+ hoistClientMonad
102+ :: Proxy m
103+ -> Proxy api
104+ -> (forall x. mon x -> mon' x)
105+ -> Client mon api
106+ -> Client mon' api
100
107
101
108
102-- | A client querying function for @a ':<|>' b@ will actually hand you
109-- | A client querying function for @a ':<|>' b@ will actually hand you
@@ -118,6 +125,10 @@ instance (HasClient m a, HasClient m b) => HasClient m (a :<|> b) where
118 clientWithRoute pm (Proxy :: Proxy a) req :<|>
125 clientWithRoute pm (Proxy :: Proxy a) req :<|>
119 clientWithRoute pm (Proxy :: Proxy b) req
126 clientWithRoute pm (Proxy :: Proxy b) req
120
127
128+ hoistClientMonad pm _ f (ca :<|> cb) =
129+ hoistClientMonad pm (Proxy :: Proxy a) f ca :<|>
130+ hoistClientMonad pm (Proxy :: Proxy b) f cb
131+
121-- | Singleton type representing a client for an empty API.
132-- | Singleton type representing a client for an empty API.
122data EmptyClient = EmptyClient deriving (Eq, Show, Bounded, Enum)
133data EmptyClient = EmptyClient deriving (Eq, Show, Bounded, Enum)
123
134
@@ -134,6 +145,7 @@ data EmptyClient = EmptyClient deriving (Eq, Show, Bounded, Enum)
134instance RunClient m => HasClient m EmptyAPI where
145instance RunClient m => HasClient m EmptyAPI where
135 type Client m EmptyAPI = EmptyClient
146 type Client m EmptyAPI = EmptyClient
136 clientWithRoute _pm Proxy _ = EmptyClient
147 clientWithRoute _pm Proxy _ = EmptyClient
148+ hoistClientMonad _ _ _ EmptyClient = EmptyClient
137
149
138-- | If you use a 'Capture' in one of your endpoints in your API,
150-- | If you use a 'Capture' in one of your endpoints in your API,
139-- the corresponding querying function will automatically take
151-- the corresponding querying function will automatically take
@@ -166,6 +178,9 @@ instance (KnownSymbol capture, ToHttpApiData a, HasClient m api)
166
178
167 where p = (toUrlPiece val)
179 where p = (toUrlPiece val)
168
180
181+ hoistClientMonad pm _ f cl = \a ->
182+ hoistClientMonad pm (Proxy :: Proxy api) f (cl a)
183+
169-- | If you use a 'CaptureAll' in one of your endpoints in your API,
184-- | If you use a 'CaptureAll' in one of your endpoints in your API,
170-- the corresponding querying function will automatically take an
185-- the corresponding querying function will automatically take an
171-- additional argument of a list of the type specified by your
186-- additional argument of a list of the type specified by your
@@ -198,6 +213,9 @@ instance (KnownSymbol capture, ToHttpApiData a, HasClient m sublayout)
198
213
199 where ps = map (toUrlPiece) vals
214 where ps = map (toUrlPiece) vals
200
215
216+ hoistClientMonad pm _ f cl = \as ->
217+ hoistClientMonad pm (Proxy :: Proxy sublayout) f (cl as)
218+
201instance OVERLAPPABLE_
219instance OVERLAPPABLE_
202 -- Note [Non-Empty Content Types]
220 -- Note [Non-Empty Content Types]
203 ( RunClient m, MimeUnrender ct a, ReflectMethod method, cts' ~ (ct ': cts)
221 ( RunClient m, MimeUnrender ct a, ReflectMethod method, cts' ~ (ct ': cts)
@@ -213,6 +231,8 @@ instance OVERLAPPABLE_
213 accept = contentTypes (Proxy :: Proxy ct)
231 accept = contentTypes (Proxy :: Proxy ct)
214 method = reflectMethod (Proxy :: Proxy method)
232 method = reflectMethod (Proxy :: Proxy method)
215
233
234+ hoistClientMonad _ _ f ma = f ma
235+
216instance OVERLAPPING_
236instance OVERLAPPING_
217 ( RunClient m, ReflectMethod method
237 ( RunClient m, ReflectMethod method
218 ) => HasClient m (Verb method status cts NoContent) where
238 ) => HasClient m (Verb method status cts NoContent) where
@@ -223,6 +243,8 @@ instance OVERLAPPING_
223 return NoContent
243 return NoContent
224 where method = reflectMethod (Proxy :: Proxy method)
244 where method = reflectMethod (Proxy :: Proxy method)
225
245
246+ hoistClientMonad _ _ f ma = f ma
247+
226instance OVERLAPPING_
248instance OVERLAPPING_
227 -- Note [Non-Empty Content Types]
249 -- Note [Non-Empty Content Types]
228 ( RunClient m, MimeUnrender ct a, BuildHeadersTo ls
250 ( RunClient m, MimeUnrender ct a, BuildHeadersTo ls
@@ -244,6 +266,8 @@ instance OVERLAPPING_
244 where method = reflectMethod (Proxy :: Proxy method)
266 where method = reflectMethod (Proxy :: Proxy method)
245 accept = contentTypes (Proxy :: Proxy ct)
267 accept = contentTypes (Proxy :: Proxy ct)
246
268
269+ hoistClientMonad _ _ f ma = f ma
270+
247instance OVERLAPPING_
271instance OVERLAPPING_
248 ( RunClient m, BuildHeadersTo ls, ReflectMethod method
272 ( RunClient m, BuildHeadersTo ls, ReflectMethod method
249 ) => HasClient m (Verb method status cts (Headers ls NoContent)) where
273 ) => HasClient m (Verb method status cts (Headers ls NoContent)) where
@@ -256,6 +280,8 @@ instance OVERLAPPING_
256 , getHeadersHList = buildHeadersTo . toList $ responseHeaders response
280 , getHeadersHList = buildHeadersTo . toList $ responseHeaders response
257 }
281 }
258
282
283+ hoistClientMonad _ _ f ma = f ma
284+
259instance OVERLAPPABLE_
285instance OVERLAPPABLE_
260 ( RunClient m, MimeUnrender ct a, ReflectMethod method,
286 ( RunClient m, MimeUnrender ct a, ReflectMethod method,
261 FramingUnrender framing a, BuildFromStream a (f a)
287 FramingUnrender framing a, BuildFromStream a (f a)
@@ -304,6 +330,7 @@ instance OVERLAPPABLE_
304 processResult (Left err, _) = Just (Left err)
330 processResult (Left err, _) = Just (Left err)
305 k go
331 k go
306
332
333+ hoistClientMonad _ _ f ma = f ma
307
334
308-- | If you use a 'Header' in one of your endpoints in your API,
335-- | If you use a 'Header' in one of your endpoints in your API,
309-- the corresponding querying function will automatically take
336-- the corresponding querying function will automatically take
@@ -345,6 +372,9 @@ instance (KnownSymbol sym, ToHttpApiData a, HasClient m api, SBoolI (FoldRequire
345 add :: a -> Request
372 add :: a -> Request
346 add value = addHeader hname value req
373 add value = addHeader hname value req
347
374
375+ hoistClientMonad pm _ f cl = \arg ->
376+ hoistClientMonad pm (Proxy :: Proxy api) f (cl arg)
377+
348-- | Using a 'HttpVersion' combinator in your API doesn't affect the client
378-- | Using a 'HttpVersion' combinator in your API doesn't affect the client
349-- functions.
379-- functions.
350instance HasClient m api
380instance HasClient m api
@@ -356,18 +386,24 @@ instance HasClient m api
356 clientWithRoute pm Proxy =
386 clientWithRoute pm Proxy =
357 clientWithRoute pm (Proxy :: Proxy api)
387 clientWithRoute pm (Proxy :: Proxy api)
358
388
389+ hoistClientMonad pm _ f cl = hoistClientMonad pm (Proxy :: Proxy api) f cl
390+
359-- | Ignore @'Summary'@ in client functions.
391-- | Ignore @'Summary'@ in client functions.
360instance HasClient m api => HasClient m (Summary desc :> api) where
392instance HasClient m api => HasClient m (Summary desc :> api) where
361 type Client m (Summary desc :> api) = Client m api
393 type Client m (Summary desc :> api) = Client m api
362
394
363 clientWithRoute pm _ = clientWithRoute pm (Proxy :: Proxy api)
395 clientWithRoute pm _ = clientWithRoute pm (Proxy :: Proxy api)
364
396
397+ hoistClientMonad pm _ f cl = hoistClientMonad pm (Proxy :: Proxy api) f cl
398+
365-- | Ignore @'Description'@ in client functions.
399-- | Ignore @'Description'@ in client functions.
366instance HasClient m api => HasClient m (Description desc :> api) where
400instance HasClient m api => HasClient m (Description desc :> api) where
367 type Client m (Description desc :> api) = Client m api
401 type Client m (Description desc :> api) = Client m api
368
402
369 clientWithRoute pm _ = clientWithRoute pm (Proxy :: Proxy api)
403 clientWithRoute pm _ = clientWithRoute pm (Proxy :: Proxy api)
370
404
405+ hoistClientMonad pm _ f cl = hoistClientMonad pm (Proxy :: Proxy api) f cl
406+
371-- | If you use a 'QueryParam' in one of your endpoints in your API,
407-- | If you use a 'QueryParam' in one of your endpoints in your API,
372-- the corresponding querying function will automatically take
408-- the corresponding querying function will automatically take
373-- an additional argument of the type specified by your 'QueryParam',
409-- an additional argument of the type specified by your 'QueryParam',
@@ -410,6 +446,9 @@ instance (KnownSymbol sym, ToHttpApiData a, HasClient m api, SBoolI (FoldRequire
410 pname :: Text
446 pname :: Text
411 pname = pack $ symbolVal (Proxy :: Proxy sym)
447 pname = pack $ symbolVal (Proxy :: Proxy sym)
412
448
449+ hoistClientMonad pm _ f cl = \arg ->
450+ hoistClientMonad pm (Proxy :: Proxy api) f (cl arg)
451+
413-- | If you use a 'QueryParams' in one of your endpoints in your API,
452-- | If you use a 'QueryParams' in one of your endpoints in your API,
414-- the corresponding querying function will automatically take
453-- the corresponding querying function will automatically take
415-- an additional argument, a list of values of the type specified
454-- an additional argument, a list of values of the type specified
@@ -453,6 +492,9 @@ instance (KnownSymbol sym, ToHttpApiData a, HasClient m api)
453 where pname = pack $ symbolVal (Proxy :: Proxy sym)
492 where pname = pack $ symbolVal (Proxy :: Proxy sym)
454 paramlist' = map (Just . toQueryParam) paramlist
493 paramlist' = map (Just . toQueryParam) paramlist
455
494
495+ hoistClientMonad pm _ f cl = \as ->
496+ hoistClientMonad pm (Proxy :: Proxy api) f (cl as)
497+
456-- | If you use a 'QueryFlag' in one of your endpoints in your API,
498-- | If you use a 'QueryFlag' in one of your endpoints in your API,
457-- the corresponding querying function will automatically take
499-- the corresponding querying function will automatically take
458-- an additional 'Bool' argument.
500-- an additional 'Bool' argument.
@@ -489,6 +531,8 @@ instance (KnownSymbol sym, HasClient m api)
489
531
490 where paramname = pack $ symbolVal (Proxy :: Proxy sym)
532 where paramname = pack $ symbolVal (Proxy :: Proxy sym)
491
533
534+ hoistClientMonad pm _ f cl = \b ->
535+ hoistClientMonad pm (Proxy :: Proxy api) f (cl b)
492
536
493-- | Pick a 'Method' and specify where the server you want to query is. You get
537-- | Pick a 'Method' and specify where the server you want to query is. You get
494-- back the full `Response`.
538-- back the full `Response`.
@@ -500,6 +544,8 @@ instance RunClient m => HasClient m Raw where
500 clientWithRoute _pm Proxy req httpMethod = do
544 clientWithRoute _pm Proxy req httpMethod = do
501 runRequest req { requestMethod = httpMethod }
545 runRequest req { requestMethod = httpMethod }
502
546
547+ hoistClientMonad _ _ f cl = \meth -> f (cl meth)
548+
503-- | If you use a 'ReqBody' in one of your endpoints in your API,
549-- | If you use a 'ReqBody' in one of your endpoints in your API,
504-- the corresponding querying function will automatically take
550-- the corresponding querying function will automatically take
505-- an additional argument of the type specified by your 'ReqBody'.
551-- an additional argument of the type specified by your 'ReqBody'.
@@ -533,6 +579,9 @@ instance (MimeRender ct a, HasClient m api)
533 req
579 req
534 )
580 )
535
581
582+ hoistClientMonad pm _ f cl = \a ->
583+ hoistClientMonad pm (Proxy :: Proxy api) f (cl a)
584+
536-- | Make the querying function append @path@ to the request path.
585-- | Make the querying function append @path@ to the request path.
537instance (KnownSymbol path, HasClient m api) => HasClient m (path :> api) where
586instance (KnownSymbol path, HasClient m api) => HasClient m (path :> api) where
538 type Client m (path :> api) = Client m api
587 type Client m (path :> api) = Client m api
@@ -543,30 +592,40 @@ instance (KnownSymbol path, HasClient m api) => HasClient m (path :> api) where
543
592
544 where p = pack $ symbolVal (Proxy :: Proxy path)
593 where p = pack $ symbolVal (Proxy :: Proxy path)
545
594
595+ hoistClientMonad pm _ f cl = hoistClientMonad pm (Proxy :: Proxy api) f cl
596+
546instance HasClient m api => HasClient m (Vault :> api) where
597instance HasClient m api => HasClient m (Vault :> api) where
547 type Client m (Vault :> api) = Client m api
598 type Client m (Vault :> api) = Client m api
548
599
549 clientWithRoute pm Proxy req =
600 clientWithRoute pm Proxy req =
550 clientWithRoute pm (Proxy :: Proxy api) req
601 clientWithRoute pm (Proxy :: Proxy api) req
551
602
603+ hoistClientMonad pm _ f cl = hoistClientMonad pm (Proxy :: Proxy api) f cl
604+
552instance HasClient m api => HasClient m (RemoteHost :> api) where
605instance HasClient m api => HasClient m (RemoteHost :> api) where
553 type Client m (RemoteHost :> api) = Client m api
606 type Client m (RemoteHost :> api) = Client m api
554
607
555 clientWithRoute pm Proxy req =
608 clientWithRoute pm Proxy req =
556 clientWithRoute pm (Proxy :: Proxy api) req
609 clientWithRoute pm (Proxy :: Proxy api) req
557
610
611+ hoistClientMonad pm _ f cl = hoistClientMonad pm (Proxy :: Proxy api) f cl
612+
558instance HasClient m api => HasClient m (IsSecure :> api) where
613instance HasClient m api => HasClient m (IsSecure :> api) where
559 type Client m (IsSecure :> api) = Client m api
614 type Client m (IsSecure :> api) = Client m api
560
615
561 clientWithRoute pm Proxy req =
616 clientWithRoute pm Proxy req =
562 clientWithRoute pm (Proxy :: Proxy api) req
617 clientWithRoute pm (Proxy :: Proxy api) req
563
618
619+ hoistClientMonad pm _ f cl = hoistClientMonad pm (Proxy :: Proxy api) f cl
620+
564instance HasClient m subapi =>
621instance HasClient m subapi =>
565 HasClient m (WithNamedContext name context subapi) where
622 HasClient m (WithNamedContext name context subapi) where
566
623
567 type Client m (WithNamedContext name context subapi) = Client m subapi
624 type Client m (WithNamedContext name context subapi) = Client m subapi
568 clientWithRoute pm Proxy = clientWithRoute pm (Proxy :: Proxy subapi)
625 clientWithRoute pm Proxy = clientWithRoute pm (Proxy :: Proxy subapi)
569
626
627+ hoistClientMonad pm _ f cl = hoistClientMonad pm (Proxy :: Proxy subapi) f cl
628+
570instance ( HasClient m api
629instance ( HasClient m api
571 ) => HasClient m (AuthProtect tag :> api) where
630 ) => HasClient m (AuthProtect tag :> api) where
572 type Client m (AuthProtect tag :> api)
631 type Client m (AuthProtect tag :> api)
@@ -575,6 +634,9 @@ instance ( HasClient m api
575 clientWithRoute pm Proxy req (AuthenticatedRequest (val,func)) =
634 clientWithRoute pm Proxy req (AuthenticatedRequest (val,func)) =
576 clientWithRoute pm (Proxy :: Proxy api) (func val req)
635 clientWithRoute pm (Proxy :: Proxy api) (func val req)
577
636
637+ hoistClientMonad pm _ f cl = \authreq ->
638+ hoistClientMonad pm (Proxy :: Proxy api) f (cl authreq)
639+
578-- * Basic Authentication
640-- * Basic Authentication
579
641
580instance HasClient m api => HasClient m (BasicAuth realm usr :> api) where
642instance HasClient m api => HasClient m (BasicAuth realm usr :> api) where
@@ -583,6 +645,9 @@ instance HasClient m api => HasClient m (BasicAuth realm usr :> api) where
583 clientWithRoute pm Proxy req val =
645 clientWithRoute pm Proxy req val =
584 clientWithRoute pm (Proxy :: Proxy api) (basicAuthReq val req)
646 clientWithRoute pm (Proxy :: Proxy api) (basicAuthReq val req)
585
647
648+ hoistClientMonad pm _ f cl = \bauth ->
649+ hoistClientMonad pm (Proxy :: Proxy api) f (cl bauth)
650+
586
651
587{- Note [Non-Empty Content Types]
652{- Note [Non-Empty Content Types]
588~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
653~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
servant-client/src/Servant/Client/Internal/HttpClient.hs+11
@@ -6,6 +6,7 @@
6{-# LANGUAGE GeneralizedNewtypeDeriving #-}
6{-# LANGUAGE GeneralizedNewtypeDeriving #-}
7{-# LANGUAGE MultiParamTypeClasses #-}
7{-# LANGUAGE MultiParamTypeClasses #-}
8{-# LANGUAGE OverloadedStrings #-}
8{-# LANGUAGE OverloadedStrings #-}
9+{-# LANGUAGE RankNTypes #-}
9{-# LANGUAGE ScopedTypeVariables #-}
10{-# LANGUAGE ScopedTypeVariables #-}
10{-# LANGUAGE TypeFamilies #-}
11{-# LANGUAGE TypeFamilies #-}
11module Servant.Client.Internal.HttpClient where
12module Servant.Client.Internal.HttpClient where
@@ -70,6 +71,16 @@ mkClientEnv mgr burl = ClientEnv mgr burl Nothing
70client :: HasClient ClientM api => Proxy api -> Client ClientM api
71client :: HasClient ClientM api => Proxy api -> Client ClientM api
71client api = api `clientIn` (Proxy :: Proxy ClientM)
72client api = api `clientIn` (Proxy :: Proxy ClientM)
72
73
74+-- | Change the monad the client functions live in, by
75+-- supplying a natural transformation.
76+hoistClient
77+ :: HasClient ClientM api
78+ => Proxy api
79+ -> (forall a. m a -> n a)
80+ -> Client m api
81+ -> Client n api
82+hoistClient = hoistClientMonad (Proxy :: Proxy ClientM)
83+
73-- | @ClientM@ is the monad in which client functions run. Contains the
84-- | @ClientM@ is the monad in which client functions run. Contains the
74-- 'Client.Manager' and 'BaseUrl' used for requests in the reader environment.
85-- 'Client.Manager' and 'BaseUrl' used for requests in the reader environment.
75newtype ClientM a = ClientM
86newtype ClientM a = ClientM