git.iko.soy/typeable/servant
on master help sign in
Add patch method, and make () response expect no content
3802ef5Julian K. Arni
CHANGELOG.md+2
@@ -5,6 +5,8 @@
5* Support multiple concurrent threads
5* Support multiple concurrent threads
6* Use `ServantError` to report Errors instead of `String`
6* Use `ServantError` to report Errors instead of `String`
7* Make the clients for `Raw` endpoints return the whole `Response` value (to be able to access response headers for example)
7* Make the clients for `Raw` endpoints return the whole `Response` value (to be able to access response headers for example)
8+* Support for PATCH
9+* Make () instances expect No Content status code, and not try to decode body.
8
10
90.2.2
110.2.2
10-----
12-----
src/Servant/Client.hs+40−1
@@ -4,6 +4,7 @@
4{-# LANGUAGE TypeOperators #-}
4{-# LANGUAGE TypeOperators #-}
5{-# LANGUAGE FlexibleInstances #-}
5{-# LANGUAGE FlexibleInstances #-}
6{-# LANGUAGE OverloadedStrings #-}
6{-# LANGUAGE OverloadedStrings #-}
7+{-# LANGUAGE OverlappingInstances #-}
7{-# LANGUAGE ScopedTypeVariables #-}
8{-# LANGUAGE ScopedTypeVariables #-}
8-- | This module provides 'client' which can automatically generate
9-- | This module provides 'client' which can automatically generate
9-- querying functions for each endpoint just from the type representing your
10-- querying functions for each endpoint just from the type representing your
@@ -123,6 +124,13 @@ instance (MimeUnrender ct result) => HasClient (Get (ct ': cts) result) where
123 clientWithRoute Proxy req host =
124 clientWithRoute Proxy req host =
124 performRequestCT (Proxy :: Proxy ct) H.methodGet req [200, 203] host
125 performRequestCT (Proxy :: Proxy ct) H.methodGet req [200, 203] host
125
126
127+-- | If you have a 'Get xs ()' endpoint, the client expects a 204 No Content
128+-- HTTP header.
129+instance HasClient (Get (ct ': cts) ()) where
130+ type Client (Get (ct ': cts) ()) = BaseUrl -> EitherT ServantError IO ()
131+ clientWithRoute Proxy req host =
132+ performRequestNoBody H.methodGet req [204] host
133+
126-- | If you use a 'Header' in one of your endpoints in your API,
134-- | If you use a 'Header' in one of your endpoints in your API,
127-- the corresponding querying function will automatically take
135-- the corresponding querying function will automatically take
128-- an additional argument of the type specified by your 'Header',
136-- an additional argument of the type specified by your 'Header',
@@ -170,6 +178,13 @@ instance (MimeUnrender ct a) => HasClient (Post (ct ': cts) a) where
170 clientWithRoute Proxy req uri =
178 clientWithRoute Proxy req uri =
171 performRequestCT (Proxy :: Proxy ct) H.methodPost req [200,201] uri
179 performRequestCT (Proxy :: Proxy ct) H.methodPost req [200,201] uri
172
180
181+-- | If you have a 'Post xs ()' endpoint, the client expects a 204 No Content
182+-- HTTP header.
183+instance HasClient (Post (ct ': cts) ()) where
184+ type Client (Post (ct ': cts) ()) = BaseUrl -> EitherT ServantError IO ()
185+ clientWithRoute Proxy req host =
186+ void $ performRequestNoBody H.methodPost req [204] host
187+
173-- | If you have a 'Put' endpoint in your API, the client
188-- | If you have a 'Put' endpoint in your API, the client
174-- side querying function that is created when calling 'client'
189-- side querying function that is created when calling 'client'
175-- will just require an argument that specifies the scheme, host
190-- will just require an argument that specifies the scheme, host
@@ -180,6 +195,30 @@ instance (MimeUnrender ct a) => HasClient (Put (ct ': cts) a) where
180 clientWithRoute Proxy req host =
195 clientWithRoute Proxy req host =
181 performRequestCT (Proxy :: Proxy ct) H.methodPut req [200,201] host
196 performRequestCT (Proxy :: Proxy ct) H.methodPut req [200,201] host
182
197
198+-- | If you have a 'Put xs ()' endpoint, the client expects a 204 No Content
199+-- HTTP header.
200+instance HasClient (Put (ct ': cts) ()) where
201+ type Client (Put (ct ': cts) ()) = BaseUrl -> EitherT ServantError IO ()
202+ clientWithRoute Proxy req host =
203+ void $ performRequestNoBody H.methodPut req [204] host
204+
205+-- | If you have a 'Patch' endpoint in your API, the client
206+-- side querying function that is created when calling 'client'
207+-- will just require an argument that specifies the scheme, host
208+-- and port to send the request to.
209+instance (MimeUnrender ct a) => HasClient (Patch (ct ': cts) a) where
210+ type Client (Patch (ct ': cts) a) = BaseUrl -> EitherT ServantError IO a
211+
212+ clientWithRoute Proxy req host =
213+ performRequestCT (Proxy :: Proxy ct) H.methodPatch req [200,201] host
214+
215+-- | If you have a 'Patch xs ()' endpoint, the client expects a 204 No Content
216+-- HTTP header.
217+instance HasClient (Patch (ct ': cts) ()) where
218+ type Client (Patch (ct ': cts) ()) = BaseUrl -> EitherT ServantError IO ()
219+ clientWithRoute Proxy req host =
220+ void $ performRequestNoBody H.methodPatch req [204] host
221+
183-- | If you use a 'QueryParam' in one of your endpoints in your API,
222-- | If you use a 'QueryParam' in one of your endpoints in your API,
184-- the corresponding querying function will automatically take
223-- the corresponding querying function will automatically take
185-- an additional argument of the type specified by your 'QueryParam',
224-- an additional argument of the type specified by your 'QueryParam',
@@ -222,7 +261,7 @@ instance (KnownSymbol sym, ToText a, HasClient sublayout)
222
261
223-- | If you use a 'QueryParams' in one of your endpoints in your API,
262-- | If you use a 'QueryParams' in one of your endpoints in your API,
224-- the corresponding querying function will automatically take
263-- the corresponding querying function will automatically take
225−-- an additional argument, a list of values of the type specified
264+-- an additional argument, a list of values of the type specified
226-- by your 'QueryParams'.
265-- by your 'QueryParams'.
227--
266--
228-- If you give an empty list, nothing will be added to the query string.
267-- If you give an empty list, nothing will be added to the query string.
src/Servant/Common/Req.hs+4
@@ -174,6 +174,10 @@ performRequestCT ct reqMethod req wantedStatus reqHost = do
174 return
174 return
175 (fromByteString ct respBody)
175 (fromByteString ct respBody)
176
176
177+performRequestNoBody :: Method -> Req -> [Int] -> BaseUrl -> EitherT ServantError IO ()
178+performRequestNoBody reqMethod req wantedStatus reqHost = do
179+ _ <- performRequest reqMethod req (`elem` wantedStatus) reqHost
180+ return ()
177
181
178catchHttpException :: IO a -> IO (Either HttpException a)
182catchHttpException :: IO a -> IO (Either HttpException a)
179catchHttpException action =
183catchHttpException action =