-
Notifications
You must be signed in to change notification settings - Fork 5
/
Api.hs
94 lines (84 loc) · 3.15 KB
/
Api.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
module Petstore.API.Api where
import qualified Control.Applicative
import qualified Control.Exception
import qualified Control.Monad
import qualified Control.Monad.IO.Class
import qualified Data.Aeson
import qualified Data.Aeson.Encoding
import qualified Data.Aeson.Parser
import qualified Data.Aeson.Types
import qualified Data.Attoparsec.ByteString
import qualified Data.List
import qualified Data.Maybe
import qualified Data.Text
import qualified Data.Time
import qualified Data.Text.Encoding
import qualified GHC.Float
import qualified GHC.Int
import qualified GHC.Types
import qualified Network.HTTP.Types
import qualified Network.Wai
import qualified Web.HttpApiData
import Petstore.API.Request
import Petstore.API.Response
import Petstore.API.Schemas.Error
import Petstore.API.Schemas.Pet
import Petstore.API.Schemas.Pets
import Petstore.API.Response.CreatePets
import Petstore.API.Response.ListPets
import Petstore.API.Response.ShowPetById
data Api m = Api {
-- | Create a pet
createPets ::
m CreatePetsResponse,
-- | List all pets
listPets ::
-- @limit@ How many items to return at one time (max 100)
(Data.Maybe.Maybe (GHC.Int.Int32)) ->
m ListPetsResponse,
-- | Info for a specific pet
showPetById ::
-- @petId@ The id of the pet to retrieve
Data.Text.Text ->
m ShowPetByIdResponse
}
application :: (Control.Monad.IO.Class.MonadIO m) => (forall a . Network.Wai.Request -> m a -> IO a) -> Api m -> Network.Wai.Application -> Network.Wai.Application
application run api notFound request respond =
case Network.Wai.pathInfo request of
["pets"] ->
case Network.Wai.requestMethod request of
"GET" ->
optionalQueryParameter "limit" False (\__limit request respond ->
run request (do
response <- listPets api __limit
Control.Monad.IO.Class.liftIO (respond $! (toResponse response))
)) request respond
"POST" ->
run request (do
response <- createPets api
Control.Monad.IO.Class.liftIO (respond $! (toResponse response))
)
x ->
unsupportedMethod x
["pets", __petId] ->
pathVariable __petId (\__petId request respond ->
case Network.Wai.requestMethod request of
"GET" ->
run request (do
response <- showPetById api __petId
Control.Monad.IO.Class.liftIO (respond $! (toResponse response))
)
x ->
unsupportedMethod x) request respond
_ ->
notFound request respond
where
unsupportedMethod _ =
respond (Network.Wai.responseBuilder (toEnum 405) [] mempty)
{-# INLINABLE application #-}