-
Notifications
You must be signed in to change notification settings - Fork 59
/
hackage.hs
86 lines (71 loc) · 2.56 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
82
83
84
85
{-# 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.Swagger
import Data.Swagger.Declare
import Data.Swagger.Lens
import Data.Swagger.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_ ?~ SwaggerObject
& 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)
hackageSwagger :: Swagger
hackageSwagger = spec & definitions .~ defs
where
(defs, spec) = runDeclare declareHackageSwagger mempty
declareHackageSwagger :: Declare (Definitions Schema) Swagger
declareHackageSwagger = do
-- param schemas
let usernameParamSchema = toParamSchema (Proxy :: Proxy Username)
-- responses
userSummaryResponse <- declareResponse (Proxy :: Proxy UserSummary)
userDetailedResponse <- declareResponse (Proxy :: Proxy UserDetailed)
packagesResponse <- declareResponse (Proxy :: Proxy [Package])
return $ mempty
& paths .~
[ ("/users", mempty & get ?~ (mempty
& produces ?~ MimeList ["application/json"]
& at 200 ?~ Inline userSummaryResponse))
, ("/user/{username}", mempty & get ?~ (mempty
& produces ?~ MimeList ["application/json"]
& parameters .~ [ Inline $ mempty
& name .~ "username"
& required ?~ True
& schema .~ ParamOther (mempty
& in_ .~ ParamPath
& paramSchema .~ usernameParamSchema) ]
& at 200 ?~ Inline userDetailedResponse))
, ("/packages", mempty & get ?~ (mempty
& produces ?~ MimeList ["application/json"]
& at 200 ?~ Inline packagesResponse))
]
main :: IO ()
main = putStrLn . read . show . encode $ hackageSwagger