on master help sign in
Add patch method, and make () response expect no content
commit3802ef5447bcb2c22ba83c880ded4774886c6606 authorJulian K. Arni <jkarni@gmail.com> dateThu 26 Mar 2015, 14:34 (12 years ago) parent9a4cfd6
3 files changed+46−1
CHANGELOG.md+2 src/Servant/Client.hs+40−1 src/Servant/Common/Req.hs+4
CHANGELOG.md+2
@@ -5,6 +5,8 @@
55* Support multiple concurrent threads
66* Use `ServantError` to report Errors instead of `String`
77* 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.
810
9110.2.2
1012-----
src/Servant/Client.hs+40−1
@@ -4,6 +4,7 @@
44{-# LANGUAGE TypeOperators #-}
55{-# LANGUAGE FlexibleInstances #-}
66{-# LANGUAGE OverloadedStrings #-}
7+{-# LANGUAGE OverlappingInstances #-}
78{-# LANGUAGE ScopedTypeVariables #-}
89-- | This module provides 'client' which can automatically generate
910-- 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
123124 clientWithRoute Proxy req host =
124125 performRequestCT (Proxy :: Proxy ct) H.methodGet req [200, 203] host
125126
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+
126134-- | If you use a 'Header' in one of your endpoints in your API,
127135-- the corresponding querying function will automatically take
128136-- an additional argument of the type specified by your 'Header',
@@ -170,6 +178,13 @@ instance (MimeUnrender ct a) => HasClient (Post (ct ': cts) a) where
170178 clientWithRoute Proxy req uri =
171179 performRequestCT (Proxy :: Proxy ct) H.methodPost req [200,201] uri
172180
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+
173188-- | If you have a 'Put' endpoint in your API, the client
174189-- side querying function that is created when calling 'client'
175190-- will just require an argument that specifies the scheme, host
@@ -180,6 +195,30 @@ instance (MimeUnrender ct a) => HasClient (Put (ct ': cts) a) where
180195 clientWithRoute Proxy req host =
181196 performRequestCT (Proxy :: Proxy ct) H.methodPut req [200,201] host
182197
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+
183222-- | If you use a 'QueryParam' in one of your endpoints in your API,
184223-- the corresponding querying function will automatically take
185224-- an additional argument of the type specified by your 'QueryParam',
@@ -222,7 +261,7 @@ instance (KnownSymbol sym, ToText a, HasClient sublayout)
222261
223262-- | If you use a 'QueryParams' in one of your endpoints in your API,
224263-- 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
226265-- by your 'QueryParams'.
227266--
228267-- 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
174174 return
175175 (fromByteString ct respBody)
176176
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 ()
177181
178182catchHttpException :: IO a -> IO (Either HttpException a)
179183catchHttpException action =