-
Notifications
You must be signed in to change notification settings - Fork 32
Support for MultiVerb #59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
0ac4728
c22c184
c7bc3a0
6ad0adc
4005975
cc93a74
9b2a862
6ef4632
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE TypeOperators #-} | ||
{-# LANGUAGE DerivingVia #-} | ||
module Todo where | ||
|
||
import Control.Lens | ||
|
@@ -18,6 +19,8 @@ import Data.Typeable (Typeable) | |
import GHC.Generics | ||
import Servant | ||
import Servant.OpenApi | ||
import qualified Generics.SOP as GSOP | ||
import Servant.API.MultiVerb | ||
|
||
todoAPI :: Proxy TodoAPI | ||
todoAPI = Proxy | ||
|
@@ -28,7 +31,8 @@ type TodoAPI | |
:<|> "todo" :> ReqBody '[JSON] Todo :> Post '[JSON] TodoId | ||
:<|> "todo" :> Capture "id" TodoId :> Get '[JSON] Todo | ||
:<|> "todo" :> Capture "id" TodoId :> ReqBody '[JSON] Todo :> Put '[JSON] TodoId | ||
|
||
:<|> "todo" :> "choices" :> MultipleChoicesInt | ||
|
||
-- | API for serving @swagger.json@. | ||
type SwaggerAPI = "swagger.json" :> Get '[JSON] OpenApi | ||
|
||
|
@@ -71,3 +75,28 @@ server = return todoSwagger :<|> error "not implemented" | |
-- | Output generated @swagger.json@ file for the @'TodoAPI'@. | ||
writeSwaggerJSON :: IO () | ||
writeSwaggerJSON = BL8.writeFile "example/swagger.json" (encodePretty todoSwagger) | ||
|
||
type MultiResponses = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By the way, I liked We have this helper type: newtype UVerbT xs m a
= UVerbT { unUVerbT :: ExceptT (Union xs) m a }
deriving newtype (Functor, Applicative, Monad, MonadTrans)
deriving newtype instance MonadReader r m => MonadReader r (UVerbT xs m)
instance MonadError e m => MonadError e (UVerbT xs m) where
throwError = lift . throwError
catchError (UVerbT act) h = UVerbT $ ExceptT $
runExceptT act `catchError` (runExceptT . unUVerbT . h)
runUVerbT :: (Monad m, HasStatus x, IsMember x xs) => UVerbT xs m x -> m (Union xs)
runUVerbT (UVerbT act) = either id id <$> runExceptT (act >>= respond)
throwUVerb :: (Monad m, HasStatus x, IsMember x xs) => x -> UVerbT xs m a
throwUVerb = UVerbT . ExceptT . fmap Left . respond There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can understand, but MultiVerb lifts a lot of limitations of UVerb, like haskell-servant/servant#1369. Maybe we can get further using the former. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I see. I've never had a need for this |
||
'[ RespondEmpty 400 "Negative" | ||
, Respond 200 "Even number" Bool | ||
, Respond 200 "Odd number" Int | ||
] | ||
|
||
-- All possible return types | ||
data MultiResult | ||
= NegativeNumber | ||
| Even Bool | ||
| Odd Int | ||
deriving stock (Generic) | ||
deriving (AsUnion MultiResponses) | ||
via GenericAsUnion MultiResponses MultiResult | ||
|
||
instance GSOP.Generic MultiResult | ||
|
||
type MultipleChoicesInt = | ||
Capture "int" Int | ||
:> MultiVerb | ||
'GET | ||
'[JSON] | ||
MultiResponses | ||
MultiResult |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,10 +88,13 @@ library | |
, insert-ordered-containers >=0.2.1.0 && <0.3 | ||
, lens >=4.17 && <5.4 | ||
, servant >=0.17 && <0.21 | ||
, servant-server >=0.17 && <0.21 | ||
, servant-client-core >=0.17 && <0.21 | ||
Comment on lines
+91
to
+92
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wow, can we save this package from depending on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shame... |
||
, singleton-bool >=0.1.4 && <0.2 | ||
, openapi3 >=3.2.3 && <3.3 | ||
, text >=1.2.3.0 && <3 | ||
, unordered-containers >=0.2.9.0 && <0.3 | ||
, generics-sop >=0.5.1 | ||
|
||
, hspec | ||
, QuickCheck | ||
|
Uh oh!
There was an error while loading. Please reload this page.