forked from biocad/openapi3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hackage.hs
82 lines (67 loc) · 2.4 KB
/
hackage.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
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Control.Lens
import Data.Aeson
import Data.Proxy
import Data.Text (Text)
import GHC.Generics
import Data.OpenApi
import Data.OpenApi.Declare
import Data.OpenApi.Lens
import Data.OpenApi.Operation
type Username = Text
data UserSummary = UserSummary
{ summaryUsername :: Username
, summaryUserid :: Int
} deriving (Generic)
instance ToSchema UserSummary where
declareNamedSchema _ = do
usernameSchema <- declareSchemaRef (Proxy :: Proxy Username)
useridSchema <- declareSchemaRef (Proxy :: Proxy Int)
return $ NamedSchema (Just "UserSummary") $ mempty
& type_ ?~ OpenApiObject
& properties .~
[ ("summaryUsername", usernameSchema )
, ("summaryUserid" , useridSchema )
]
& required .~ [ "summaryUsername"
, "summaryUserid" ]
type Group = Text
data UserDetailed = UserDetailed
{ username :: Username
, userid :: Int
, groups :: [Group]
} deriving (Generic, ToSchema)
newtype Package = Package { packageName :: Text }
deriving (Generic, ToSchema)
hackageOpenApi :: OpenApi
hackageOpenApi = spec & components.schemas .~ defs
where
(defs, spec) = runDeclare declareHackageOpenApi mempty
declareHackageOpenApi :: Declare (Definitions Schema) OpenApi
declareHackageOpenApi = do
-- param schemas
let usernameParamSchema = toParamSchema (Proxy :: Proxy Username)
-- responses
userSummaryResponse <- declareResponse "application/json" (Proxy :: Proxy UserSummary)
userDetailedResponse <- declareResponse "application/json" (Proxy :: Proxy UserDetailed)
packagesResponse <- declareResponse "application/json" (Proxy :: Proxy [Package])
return $ mempty
& paths .~
[ ("/users", mempty & get ?~ (mempty
& at 200 ?~ Inline userSummaryResponse))
, ("/user/{username}", mempty & get ?~ (mempty
& parameters .~ [ Inline $ mempty
& name .~ "username"
& required ?~ True
& in_ .~ ParamPath
& schema ?~ Inline usernameParamSchema ]
& at 200 ?~ Inline userDetailedResponse))
, ("/packages", mempty & get ?~ (mempty
& at 200 ?~ Inline packagesResponse))
]
main :: IO ()
main = putStrLn . read . show . encode $ hackageOpenApi