servant

Custom JSON errors for Servant-server

蓝咒 提交于 2019-12-07 07:58:39
问题 When using servant, I'd like to return all errors as JSON. Currently, if a request fails to parse, I see an error message like this, returned as plain text Failed reading: not a valid json value Instead I would like to return this as application/json {"error":"Failed reading: not a valid json value"} How can I do this? The docs say ServantErr is the default error type, and I can certainly respond with custom errors inside my handlers, but if parsing fails I don't see how I can return a custom

Using Servant with Yesod shakespeare (Hamlet, Julius, Lucius)

旧巷老猫 提交于 2019-12-06 14:56:32
How i can use shakespeare (from yesod) for servant webservices APIs? I try: type TestAPI = "tests" :> Get '[JSON] [Test] :<|> "Test.html" :> Get '[HTML] Html serverTestAPI :: ServerT TestAPI AppM serverTestAPI = tests :<|> test :<|> testHtml tests :: AppM [Test] tests = do return [ Test 1 "Test 1" , Test 2 "Test 2" ] testHtml = [hamlet| $doctype 5 ......... |] But I get error! As @Carsten points out, in this case what you want is shamlet . The key thing is implementing proper instances of the ToMarkup typeclass. I recommend you to read this excellent introduction on Shakespeare templates. A

Issues using pattern matching with servant-client

空扰寡人 提交于 2019-12-06 08:26:47
In the Servant docs , we have the following api: type API = "position" :> Capture "x" Int :> Capture "y" Int :> Get '[JSON] Position :<|> "hello" :> QueryParam "name" String :> Get '[JSON] HelloMessage :<|> "marketing" :> ReqBody '[JSON] ClientInfo :> Post '[JSON] Email and we can define client functions like so: api :: Proxy API api = Proxy position :<|> hello :<|> marketing = client api If our api type instead looked like: type API = QueryParam "test" Int :> ( "position" :> Capture "x" Int :> Capture "y" Int :> Get '[JSON] Position :<|> "hello" :> QueryParam "name" String :> Get '[JSON]

Can IO actions be sequenced while keeping the logic in a pure function?

守給你的承諾、 提交于 2019-12-06 00:26:45
问题 I have the following code which grabs two pages of data from a paginated API endpoint. I'd like to modify query function to keep getting pages until it finds no more data (so replace take 2 in the code below with something which looks at the API response). My question is wether it is possible to achieve this without changing query function to an IO function. And if so, how would I go about it. If not, is there a way of doing this without writing recursive function? Here is the code: #!/usr

Custom JSON errors for Servant-server

痞子三分冷 提交于 2019-12-05 10:51:19
When using servant , I'd like to return all errors as JSON. Currently, if a request fails to parse, I see an error message like this, returned as plain text Failed reading: not a valid json value Instead I would like to return this as application/json {"error":"Failed reading: not a valid json value"} How can I do this? The docs say ServantErr is the default error type, and I can certainly respond with custom errors inside my handlers, but if parsing fails I don't see how I can return a custom error. First, some language extensions {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE

What does an apostrophe in front of a list ( '[Something] ) mean in Haskell?

百般思念 提交于 2019-11-28 00:54:18
I was reading the Servant documentation and came across this line: type UserAPI = "users" :> QueryParam "sortby" SortBy :> Get '[JSON] [User] What is the ' doing to that list? This is DataKinds in action, which: lifts values at the type level, and lifts types to the kind level This however causes confusion at the type level. Now, in types, [X] might either be [X] :: * , the list-of- X type, or instead we might have [X] :: [T] due to the lifting -- that is the value [X] (list containing only the single value X ), with X of type T , lifted at the type level. To overcome this ambiguity, GHC