|
| 1 | +# MultiVerb: Powerful endpoint types |
| 2 | + |
| 3 | +`MultiVerb` allows you to represent an API endpoint with multiple response types, status codes and headers. |
| 4 | + |
| 5 | +## Preliminaries |
| 6 | + |
| 7 | +```haskell |
| 8 | +{-# LANGUAGE GHC2021 #-} |
| 9 | +{-# LANGUAGE DataKinds #-} |
| 10 | +{-# LANGUAGE DerivingStrategies #-} |
| 11 | +{-# LANGUAGE DerivingVia #-} |
| 12 | +
|
| 13 | +import GHC.Generics |
| 14 | +import Generics.SOP qualified as GSOP |
| 15 | +import Network.Wai.Handler.Warp as Warp |
| 16 | +
|
| 17 | +import Servant.API |
| 18 | +import Servant.API.MultiVerb |
| 19 | +import Servant.Server |
| 20 | +import Servant.Server.Generic |
| 21 | +``` |
| 22 | +
|
| 23 | +## Writing an endpoint |
| 24 | +
|
| 25 | +Let us create an endpoint that captures an 'Int' and has the following logic: |
| 26 | +
|
| 27 | +* If the number is negative, we return status code 400 and an empty body; |
| 28 | +* If the number is even, we return a 'Bool' in the response body; |
| 29 | +* If the number is odd, we return another 'Int' in the response body. |
| 30 | +
|
| 31 | +
|
| 32 | +Let us list all possible HTTP responses: |
| 33 | +```haskell |
| 34 | +
|
| 35 | +type Responses = |
| 36 | + '[ RespondEmpty 400 "Negative" |
| 37 | + , Respond 200 "Odd number" Int |
| 38 | + , Respond 200 "Even number" Bool |
| 39 | + ] |
| 40 | +``` |
| 41 | +
|
| 42 | +Let us create the return type: |
| 43 | +
|
| 44 | +```haskell |
| 45 | +data Result |
| 46 | + = NegativeNumber |
| 47 | + | Odd Int |
| 48 | + | Even Bool |
| 49 | + deriving stock (Generic) |
| 50 | + deriving (AsUnion Responses) |
| 51 | + via GenericAsUnion Responses Result |
| 52 | +
|
| 53 | +instance GSOP.Generic Result |
| 54 | +``` |
| 55 | +
|
| 56 | +These deriving statements above tie together the responses and the return values, and the order in which they are defined matters. For instance, if `Even` and `Odd` had switched places in the definition of `Result`, this would provoke an error: |
| 57 | +
|
| 58 | +``` |
| 59 | +• No instance for ‘AsConstructor |
| 60 | + ((:) @Type Int ('[] @Type)) (Respond 200 "Even number" Bool)’ |
| 61 | + arising from the 'deriving' clause of a data type declaration |
| 62 | +``` |
| 63 | +
|
| 64 | +> If you would prefer to write an intance of 'AsUnion' by yourself, read more in |
| 65 | +the “Implementing AsUnion manually” section. |
| 66 | +
|
| 67 | +Finally, let us write our endpoint description: |
| 68 | +
|
| 69 | +```haskell |
| 70 | +type MultipleChoicesInt = |
| 71 | + Capture "int" Int |
| 72 | + :> MultiVerb |
| 73 | + 'GET |
| 74 | + '[JSON] |
| 75 | + Responses |
| 76 | + Result |
| 77 | +``` |
| 78 | +
|
| 79 | +## Integration in a routing table |
| 80 | +
|
| 81 | +We want to integrate our endpoint into a wider routing table with another |
| 82 | +endpoint: `version`, which returns the version of the API |
| 83 | +
|
| 84 | +```haskell |
| 85 | +data Routes mode = Routes |
| 86 | + { choicesRoutes :: mode :- "choices" :> Choices |
| 87 | + , version :: mode :- "version" :> Get '[JSON] Int |
| 88 | + } |
| 89 | + deriving stock (Generic) |
| 90 | +``` |
| 91 | +
|
| 92 | +```haskell |
| 93 | +type Choices = NamedRoutes Choices' |
| 94 | +data Choices' mode = Choices' |
| 95 | + { choices :: mode :- MultipleChoicesInt |
| 96 | + } |
| 97 | + deriving stock (Generic) |
| 98 | +
|
| 99 | +choicesServer :: Choices' AsServer |
| 100 | +choicesServer = |
| 101 | + Choices' |
| 102 | + { choices = choicesHandler |
| 103 | + } |
| 104 | +
|
| 105 | +routesServer :: Routes AsServer |
| 106 | +routesServer = |
| 107 | + Routes |
| 108 | + { choicesRoutes = choicesServer |
| 109 | + , version = versionHandler |
| 110 | + } |
| 111 | +
|
| 112 | +choicesHandler :: Int -> Handler Result |
| 113 | +choicesHandler parameter = |
| 114 | + if parameter < 0 |
| 115 | + then pure NegativeNumber |
| 116 | + else |
| 117 | + if even parameter |
| 118 | + then pure $ Odd 3 |
| 119 | + else pure $ Even True |
| 120 | +
|
| 121 | +versionHandler :: Handler Int |
| 122 | +versionHandler = pure 1 |
| 123 | +``` |
| 124 | +
|
| 125 | +We can now plug everything together: |
| 126 | +
|
| 127 | +
|
| 128 | +```haskell |
| 129 | +main :: IO () |
| 130 | +main = do |
| 131 | + putStrLn "Starting server on http://localhost:5000" |
| 132 | + let server = genericServe routesServer |
| 133 | + Warp.run 5000 server |
| 134 | +``` |
| 135 | +
|
| 136 | +Now let us run the server and observe how it behaves: |
| 137 | +
|
| 138 | +``` |
| 139 | +$ http http://localhost:5000/version |
| 140 | +HTTP/1.1 200 OK |
| 141 | +Content-Type: application/json;charset=utf-8 |
| 142 | +Date: Thu, 29 Aug 2024 14:22:20 GMT |
| 143 | +Server: Warp/3.4.1 |
| 144 | +Transfer-Encoding: chunked |
| 145 | +
|
| 146 | +1 |
| 147 | +``` |
| 148 | +
|
| 149 | +
|
| 150 | +``` |
| 151 | +$ http http://localhost:5000/choices/3 |
| 152 | +HTTP/1.1 200 OK |
| 153 | +Content-Type: application/json;charset=utf-8 |
| 154 | +Date: Thu, 29 Aug 2024 14:22:30 GMT |
| 155 | +Server: Warp/3.4.1 |
| 156 | +Transfer-Encoding: chunked |
| 157 | +
|
| 158 | +true |
| 159 | +``` |
| 160 | +
|
| 161 | +``` |
| 162 | +$ http http://localhost:5000/choices/2 |
| 163 | +HTTP/1.1 200 OK |
| 164 | +Content-Type: application/json;charset=utf-8 |
| 165 | +Date: Thu, 29 Aug 2024 14:22:33 GMT |
| 166 | +Server: Warp/3.4.1 |
| 167 | +Transfer-Encoding: chunked |
| 168 | +
|
| 169 | +3 |
| 170 | +``` |
| 171 | +
|
| 172 | +``` |
| 173 | +$ http http://localhost:5000/choices/-432 |
| 174 | +HTTP/1.1 400 Bad Request |
| 175 | +Date: Thu, 29 Aug 2024 14:22:41 GMT |
| 176 | +Server: Warp/3.4.1 |
| 177 | +Transfer-Encoding: chunked |
| 178 | +``` |
0 commit comments