@@ -52,16 +52,17 @@ import Servant.Common.Req
5252-- > getAllBooks :: BaseUrl -> EitherT String IO [Book]
5353-- > postNewBook :: Book -> BaseUrl -> EitherT String IO Book
5454-- > (getAllBooks :<|> postNewBook) = client myApi
55−client :: HasClient layout => Proxy layout -> Client layout
56−client p = clientWithRoute p defReq
55+client :: HasClient layout => Proxy layout -> BaseUrl -> Client layout
56+client p baseurl = clientWithRoute p defReq baseurl
5757
5858-- | This class lets us define how each API combinator
5959-- influences the creation of an HTTP request. It's mostly
6060-- an internal class, you can just use 'client'.
6161class HasClient layout where
6262 type Client layout :: *
63− clientWithRoute :: Proxy layout -> Req -> Client layout
63+ clientWithRoute :: Proxy layout -> Req -> BaseUrl -> Client layout
6464
65+{-type Client layout = Client layout-}
6566
6667-- | A client querying function for @a ':<|>' b@ will actually hand you
6768-- one function for querying @a@ and another one for querying @b@,
@@ -78,9 +79,9 @@ class HasClient layout where
7879-- > (getAllBooks :<|> postNewBook) = client myApi
7980instance (HasClient a, HasClient b) => HasClient (a :<|> b) where
8081 type Client (a :<|> b) = Client a :<|> Client b
81− clientWithRoute Proxy req =
82− clientWithRoute (Proxy :: Proxy a) req :<|>
83− clientWithRoute (Proxy :: Proxy b) req
82+ clientWithRoute Proxy req baseurl =
83+ clientWithRoute (Proxy :: Proxy a) req baseurl :<|>
84+ clientWithRoute (Proxy :: Proxy b) req baseurl
8485
8586-- | If you use a 'Capture' in one of your endpoints in your API,
8687-- the corresponding querying function will automatically take
@@ -107,9 +108,10 @@ instance (KnownSymbol capture, ToText a, HasClient sublayout)
107108 type Client (Capture capture a :> sublayout) =
108109 a -> Client sublayout
109110
110− clientWithRoute Proxy req val =
111− clientWithRoute (Proxy :: Proxy sublayout) $
112− appendToPath p req
111+ clientWithRoute Proxy req baseurl val =
112+ clientWithRoute (Proxy :: Proxy sublayout)
113+ (appendToPath p req)
114+ baseurl
113115
114116 where p = unpack (toText val)
115117
@@ -122,9 +124,9 @@ instance
122124 {-# OVERLAPPABLE #-}
123125#endif
124126 (MimeUnrender ct a) => HasClient (Delete (ct ': cts) a) where
125− type Client (Delete (ct ': cts) a) = BaseUrl -> EitherT ServantError IO a
126− clientWithRoute Proxy req host =
127− snd <$> performRequestCT (Proxy :: Proxy ct) H.methodDelete req [200, 202] host
127+ type Client (Delete (ct ': cts) a) = EitherT ServantError IO a
128+ clientWithRoute Proxy req baseurl =
129+ snd <$> performRequestCT (Proxy :: Proxy ct) H.methodDelete req [200, 202] baseurl
128130
129131-- | If you have a 'Delete xs ()' endpoint, the client expects a 204 No Content
130132-- HTTP header.
@@ -133,9 +135,9 @@ instance
133135 {-# OVERLAPPING #-}
134136#endif
135137 HasClient (Delete (ct ': cts) ()) where
136− type Client (Delete (ct ': cts) ()) = BaseUrl -> EitherT ServantError IO ()
137− clientWithRoute Proxy req host =
138− void $ performRequestNoBody H.methodDelete req [204] host
138+ type Client (Delete (ct ': cts) ()) = EitherT ServantError IO ()
139+ clientWithRoute Proxy req baseurl =
140+ void $ performRequestNoBody H.methodDelete req [204] baseurl
139141
140142-- | If you have a 'Delete xs (Headers ls x)' endpoint, the client expects the
141143-- corresponding headers.
@@ -145,14 +147,13 @@ instance
145147#endif
146148 ( MimeUnrender ct a, BuildHeadersTo ls
147149 ) => HasClient (Delete (ct ': cts) (Headers ls a)) where
148− type Client (Delete (ct ': cts) (Headers ls a)) = BaseUrl -> EitherT ServantError IO (Headers ls a)
149− clientWithRoute Proxy req host = do
150− (hdrs, resp) <- performRequestCT (Proxy :: Proxy ct) H.methodDelete req [200, 202] host
150+ type Client (Delete (ct ': cts) (Headers ls a)) = EitherT ServantError IO (Headers ls a)
151+ clientWithRoute Proxy req baseurl = do
152+ (hdrs, resp) <- performRequestCT (Proxy :: Proxy ct) H.methodDelete req [200, 202] baseurl
151153 return $ Headers { getResponse = resp
152154 , getHeadersHList = buildHeadersTo hdrs
153155 }
154156
155−
156157-- | If you have a 'Get' endpoint in your API, the client
157158-- side querying function that is created when calling 'client'
158159-- will just require an argument that specifies the scheme, host
@@ -162,9 +163,9 @@ instance
162163 {-# OVERLAPPABLE #-}
163164#endif
164165 (MimeUnrender ct result) => HasClient (Get (ct ': cts) result) where
165− type Client (Get (ct ': cts) result) = BaseUrl -> EitherT ServantError IO result
166− clientWithRoute Proxy req host =
167− snd <$> performRequestCT (Proxy :: Proxy ct) H.methodGet req [200, 203] host
166+ type Client (Get (ct ': cts) result) = EitherT ServantError IO result
167+ clientWithRoute Proxy req baseurl =
168+ snd <$> performRequestCT (Proxy :: Proxy ct) H.methodGet req [200, 203] baseurl
168169
169170-- | If you have a 'Get xs ()' endpoint, the client expects a 204 No Content
170171-- HTTP status.
@@ -173,9 +174,9 @@ instance
173174 {-# OVERLAPPING #-}
174175#endif
175176 HasClient (Get (ct ': cts) ()) where
176− type Client (Get (ct ': cts) ()) = BaseUrl -> EitherT ServantError IO ()
177− clientWithRoute Proxy req host =
178− performRequestNoBody H.methodGet req [204] host
177+ type Client (Get (ct ': cts) ()) = EitherT ServantError IO ()
178+ clientWithRoute Proxy req baseurl =
179+ performRequestNoBody H.methodGet req [204] baseurl
179180
180181-- | If you have a 'Get xs (Headers ls x)' endpoint, the client expects the
181182-- corresponding headers.
@@ -185,9 +186,9 @@ instance
185186#endif
186187 ( MimeUnrender ct a, BuildHeadersTo ls
187188 ) => HasClient (Get (ct ': cts) (Headers ls a)) where
188− type Client (Get (ct ': cts) (Headers ls a)) = BaseUrl -> EitherT ServantError IO (Headers ls a)
189− clientWithRoute Proxy req host = do
190− (hdrs, resp) <- performRequestCT (Proxy :: Proxy ct) H.methodGet req [200, 203, 204] host
189+ type Client (Get (ct ': cts) (Headers ls a)) = EitherT ServantError IO (Headers ls a)
190+ clientWithRoute Proxy req baseurl = do
191+ (hdrs, resp) <- performRequestCT (Proxy :: Proxy ct) H.methodGet req [200, 203, 204] baseurl
191192 return $ Headers { getResponse = resp
192193 , getHeadersHList = buildHeadersTo hdrs
193194 }
@@ -223,9 +224,13 @@ instance (KnownSymbol sym, ToText a, HasClient sublayout)
223224 type Client (Header sym a :> sublayout) =
224225 Maybe a -> Client sublayout
225226
226− clientWithRoute Proxy req mval =
227− clientWithRoute (Proxy :: Proxy sublayout) $
228− maybe req (\value -> Servant.Common.Req.addHeader hname value req) mval
227+ clientWithRoute Proxy req baseurl mval =
228+ clientWithRoute (Proxy :: Proxy sublayout)
229+ (maybe req
230+ (\value -> Servant.Common.Req.addHeader hname value req)
231+ mval
232+ )
233+ baseurl
229234
230235 where hname = symbolVal (Proxy :: Proxy sym)
231236
@@ -238,10 +243,9 @@ instance
238243 {-# OVERLAPPABLE #-}
239244#endif
240245 (MimeUnrender ct a) => HasClient (Post (ct ': cts) a) where
241− type Client (Post (ct ': cts) a) = BaseUrl -> EitherT ServantError IO a
242−
243− clientWithRoute Proxy req uri =
244− snd <$> performRequestCT (Proxy :: Proxy ct) H.methodPost req [200,201] uri
246+ type Client (Post (ct ': cts) a) = EitherT ServantError IO a
247+ clientWithRoute Proxy req baseurl =
248+ snd <$> performRequestCT (Proxy :: Proxy ct) H.methodPost req [200,201] baseurl
245249
246250-- | If you have a 'Post xs ()' endpoint, the client expects a 204 No Content
247251-- HTTP header.
@@ -250,9 +254,9 @@ instance
250254 {-# OVERLAPPING #-}
251255#endif
252256 HasClient (Post (ct ': cts) ()) where
253− type Client (Post (ct ': cts) ()) = BaseUrl -> EitherT ServantError IO ()
254− clientWithRoute Proxy req host =
255− void $ performRequestNoBody H.methodPost req [204] host
257+ type Client (Post (ct ': cts) ()) = EitherT ServantError IO ()
258+ clientWithRoute Proxy req baseurl =
259+ void $ performRequestNoBody H.methodPost req [204] baseurl
256260
257261-- | If you have a 'Post xs (Headers ls x)' endpoint, the client expects the
258262-- corresponding headers.
@@ -262,9 +266,9 @@ instance
262266#endif
263267 ( MimeUnrender ct a, BuildHeadersTo ls
264268 ) => HasClient (Post (ct ': cts) (Headers ls a)) where
265− type Client (Post (ct ': cts) (Headers ls a)) = BaseUrl -> EitherT ServantError IO (Headers ls a)
266− clientWithRoute Proxy req host = do
267− (hdrs, resp) <- performRequestCT (Proxy :: Proxy ct) H.methodPost req [200, 201] host
269+ type Client (Post (ct ': cts) (Headers ls a)) = EitherT ServantError IO (Headers ls a)
270+ clientWithRoute Proxy req baseurl = do
271+ (hdrs, resp) <- performRequestCT (Proxy :: Proxy ct) H.methodPost req [200, 201] baseurl
268272 return $ Headers { getResponse = resp
269273 , getHeadersHList = buildHeadersTo hdrs
270274 }
@@ -278,10 +282,9 @@ instance
278282 {-# OVERLAPPABLE #-}
279283#endif
280284 (MimeUnrender ct a) => HasClient (Put (ct ': cts) a) where
281− type Client (Put (ct ': cts) a) = BaseUrl -> EitherT ServantError IO a
282−
283− clientWithRoute Proxy req host =
284− snd <$> performRequestCT (Proxy :: Proxy ct) H.methodPut req [200,201] host
285+ type Client (Put (ct ': cts) a) = EitherT ServantError IO a
286+ clientWithRoute Proxy req baseurl =
287+ snd <$> performRequestCT (Proxy :: Proxy ct) H.methodPut req [200,201] baseurl
285288
286289-- | If you have a 'Put xs ()' endpoint, the client expects a 204 No Content
287290-- HTTP header.
@@ -290,9 +293,9 @@ instance
290293 {-# OVERLAPPING #-}
291294#endif
292295 HasClient (Put (ct ': cts) ()) where
293− type Client (Put (ct ': cts) ()) = BaseUrl -> EitherT ServantError IO ()
294− clientWithRoute Proxy req host =
295− void $ performRequestNoBody H.methodPut req [204] host
296+ type Client (Put (ct ': cts) ()) = EitherT ServantError IO ()
297+ clientWithRoute Proxy req baseurl =
298+ void $ performRequestNoBody H.methodPut req [204] baseurl
296299
297300-- | If you have a 'Put xs (Headers ls x)' endpoint, the client expects the
298301-- corresponding headers.
@@ -302,9 +305,9 @@ instance
302305#endif
303306 ( MimeUnrender ct a, BuildHeadersTo ls
304307 ) => HasClient (Put (ct ': cts) (Headers ls a)) where
305− type Client (Put (ct ': cts) (Headers ls a)) = BaseUrl -> EitherT ServantError IO (Headers ls a)
306− clientWithRoute Proxy req host = do
307− (hdrs, resp) <- performRequestCT (Proxy :: Proxy ct) H.methodPut req [200, 201] host
308+ type Client (Put (ct ': cts) (Headers ls a)) = EitherT ServantError IO (Headers ls a)
309+ clientWithRoute Proxy req baseurl = do
310+ (hdrs, resp) <- performRequestCT (Proxy :: Proxy ct) H.methodPut req [200, 201] baseurl
308311 return $ Headers { getResponse = resp
309312 , getHeadersHList = buildHeadersTo hdrs
310313 }
@@ -318,10 +321,9 @@ instance
318321 {-# OVERLAPPABLE #-}
319322#endif
320323 (MimeUnrender ct a) => HasClient (Patch (ct ': cts) a) where
321− type Client (Patch (ct ': cts) a) = BaseUrl -> EitherT ServantError IO a
322−
323− clientWithRoute Proxy req host =
324− snd <$> performRequestCT (Proxy :: Proxy ct) H.methodPatch req [200,201] host
324+ type Client (Patch (ct ': cts) a) = EitherT ServantError IO a
325+ clientWithRoute Proxy req baseurl =
326+ snd <$> performRequestCT (Proxy :: Proxy ct) H.methodPatch req [200,201] baseurl
325327
326328-- | If you have a 'Patch xs ()' endpoint, the client expects a 204 No Content
327329-- HTTP header.
@@ -330,9 +332,9 @@ instance
330332 {-# OVERLAPPING #-}
331333#endif
332334 HasClient (Patch (ct ': cts) ()) where
333− type Client (Patch (ct ': cts) ()) = BaseUrl -> EitherT ServantError IO ()
334− clientWithRoute Proxy req host =
335− void $ performRequestNoBody H.methodPatch req [204] host
335+ type Client (Patch (ct ': cts) ()) = EitherT ServantError IO ()
336+ clientWithRoute Proxy req baseurl =
337+ void $ performRequestNoBody H.methodPatch req [204] baseurl
336338
337339-- | If you have a 'Patch xs (Headers ls x)' endpoint, the client expects the
338340-- corresponding headers.
@@ -342,9 +344,9 @@ instance
342344#endif
343345 ( MimeUnrender ct a, BuildHeadersTo ls
344346 ) => HasClient (Patch (ct ': cts) (Headers ls a)) where
345− type Client (Patch (ct ': cts) (Headers ls a)) = BaseUrl -> EitherT ServantError IO (Headers ls a)
346− clientWithRoute Proxy req host = do
347− (hdrs, resp) <- performRequestCT (Proxy :: Proxy ct) H.methodPatch req [200, 201, 204] host
347+ type Client (Patch (ct ': cts) (Headers ls a)) = EitherT ServantError IO (Headers ls a)
348+ clientWithRoute Proxy req baseurl = do
349+ (hdrs, resp) <- performRequestCT (Proxy :: Proxy ct) H.methodPatch req [200, 201, 204] baseurl
348350 return $ Headers { getResponse = resp
349351 , getHeadersHList = buildHeadersTo hdrs
350352 }
@@ -381,9 +383,13 @@ instance (KnownSymbol sym, ToText a, HasClient sublayout)
381383 Maybe a -> Client sublayout
382384
383385 -- if mparam = Nothing, we don't add it to the query string
384− clientWithRoute Proxy req mparam =
385− clientWithRoute (Proxy :: Proxy sublayout) $
386− maybe req (flip (appendToQueryString pname) req . Just) mparamText
386+ clientWithRoute Proxy req baseurl mparam =
387+ clientWithRoute (Proxy :: Proxy sublayout)
388+ (maybe req
389+ (flip (appendToQueryString pname) req . Just)
390+ mparamText
391+ )
392+ baseurl
387393
388394 where pname = cs pname'
389395 pname' = symbolVal (Proxy :: Proxy sym)
@@ -422,9 +428,13 @@ instance (KnownSymbol sym, ToText a, HasClient sublayout)
422428 type Client (QueryParams sym a :> sublayout) =
423429 [a] -> Client sublayout
424430
425− clientWithRoute Proxy req paramlist =
426− clientWithRoute (Proxy :: Proxy sublayout) $
427− foldl' (\ req' -> maybe req' (flip (appendToQueryString pname) req' . Just)) req paramlist'
431+ clientWithRoute Proxy req baseurl paramlist =
432+ clientWithRoute (Proxy :: Proxy sublayout)
433+ (foldl' (\ req' -> maybe req' (flip (appendToQueryString pname) req' . Just))
434+ req
435+ paramlist'
436+ )
437+ baseurl
428438
429439 where pname = cs pname'
430440 pname' = symbolVal (Proxy :: Proxy sym)
@@ -457,11 +467,13 @@ instance (KnownSymbol sym, HasClient sublayout)
457467 type Client (QueryFlag sym :> sublayout) =
458468 Bool -> Client sublayout
459469
460− clientWithRoute Proxy req flag =
461− clientWithRoute (Proxy :: Proxy sublayout) $
462− if flag
463− then appendToQueryString paramname Nothing req
464− else req
470+ clientWithRoute Proxy req baseurl flag =
471+ clientWithRoute (Proxy :: Proxy sublayout)
472+ (if flag
473+ then appendToQueryString paramname Nothing req
474+ else req
475+ )
476+ baseurl
465477
466478 where paramname = cs $ symbolVal (Proxy :: Proxy sym)
467479
@@ -497,9 +509,13 @@ instance (KnownSymbol sym, ToText a, HasClient sublayout)
497509 Maybe a -> Client sublayout
498510
499511 -- if mparam = Nothing, we don't add it to the query string
500− clientWithRoute Proxy req mparam =
501− clientWithRoute (Proxy :: Proxy sublayout) $
502− maybe req (flip (appendToMatrixParams pname . Just) req) mparamText
512+ clientWithRoute Proxy req baseurl mparam =
513+ clientWithRoute (Proxy :: Proxy sublayout)
514+ (maybe req
515+ (flip (appendToMatrixParams pname . Just) req)
516+ mparamText
517+ )
518+ baseurl
503519
504520 where pname = symbolVal (Proxy :: Proxy sym)
505521 mparamText = fmap (cs . toText) mparam
@@ -537,9 +553,13 @@ instance (KnownSymbol sym, ToText a, HasClient sublayout)
537553 type Client (MatrixParams sym a :> sublayout) =
538554 [a] -> Client sublayout
539555
540− clientWithRoute Proxy req paramlist =
541− clientWithRoute (Proxy :: Proxy sublayout) $
542− foldl' (\ req' value -> maybe req' (flip (appendToMatrixParams pname) req' . Just . cs) value) req paramlist'
556+ clientWithRoute Proxy req baseurl paramlist =
557+ clientWithRoute (Proxy :: Proxy sublayout)
558+ (foldl' (\ req' value -> maybe req' (flip (appendToMatrixParams pname) req' . Just . cs) value)
559+ req
560+ paramlist'
561+ )
562+ baseurl
543563
544564 where pname = cs pname'
545565 pname' = symbolVal (Proxy :: Proxy sym)
@@ -572,22 +592,24 @@ instance (KnownSymbol sym, HasClient sublayout)
572592 type Client (MatrixFlag sym :> sublayout) =
573593 Bool -> Client sublayout
574594
575− clientWithRoute Proxy req flag =
576− clientWithRoute (Proxy :: Proxy sublayout) $
577− if flag
578− then appendToMatrixParams paramname Nothing req
579− else req
595+ clientWithRoute Proxy req baseurl flag =
596+ clientWithRoute (Proxy :: Proxy sublayout)
597+ (if flag
598+ then appendToMatrixParams paramname Nothing req
599+ else req
600+ )
601+ baseurl
580602
581603 where paramname = cs $ symbolVal (Proxy :: Proxy sym)
582604
583605-- | Pick a 'Method' and specify where the server you want to query is. You get
584606-- back the full `Response`.
585607instance HasClient Raw where
586− type Client Raw = H.Method -> BaseUrl -> EitherT ServantError IO (Int, ByteString, MediaType, [HTTP.Header], Response ByteString)
608+ type Client Raw = H.Method -> EitherT ServantError IO (Int, ByteString, MediaType, [HTTP.Header], Response ByteString)
587609
588− clientWithRoute :: Proxy Raw -> Req -> Client Raw
589− clientWithRoute Proxy req httpMethod host = do
590− performRequest httpMethod req (const True) host
610+ clientWithRoute :: Proxy Raw -> Req -> BaseUrl -> Client Raw
611+ clientWithRoute Proxy req baseurl httpMethod = do
612+ performRequest httpMethod req (const True) baseurl
591613
592614-- | If you use a 'ReqBody' in one of your endpoints in your API,
593615-- the corresponding querying function will automatically take
@@ -613,18 +635,23 @@ instance (MimeRender ct a, HasClient sublayout)
613635 type Client (ReqBody (ct ': cts) a :> sublayout) =
614636 a -> Client sublayout
615637
616− clientWithRoute Proxy req body =
617− clientWithRoute (Proxy :: Proxy sublayout) $ do
618− let ctProxy = Proxy :: Proxy ct
619− setRQBody (mimeRender ctProxy body) (contentType ctProxy) req
638+ clientWithRoute Proxy req baseurl body =
639+ clientWithRoute (Proxy :: Proxy sublayout)
640+ (let ctProxy = Proxy :: Proxy ct
641+ in setRQBody (mimeRender ctProxy body)
642+ (contentType ctProxy)
643+ req
644+ )
645+ baseurl
620646
621647-- | Make the querying function append @path@ to the request path.
622648instance (KnownSymbol path, HasClient sublayout) => HasClient (path :> sublayout) where
623649 type Client (path :> sublayout) = Client sublayout
624650
625− clientWithRoute Proxy req =
626− clientWithRoute (Proxy :: Proxy sublayout) $
627− appendToPath p req
651+ clientWithRoute Proxy req baseurl =
652+ clientWithRoute (Proxy :: Proxy sublayout)
653+ (appendToPath p req)
654+ baseurl
628655
629656 where p = symbolVal (Proxy :: Proxy path)
630657