@@ -74,58 +74,36 @@ webApp db req send =
7474 _ -> send responseNotFound
7575 where
7676 responseNotFound = responseLBS status404 [] " "
77+ responseBadRequest msg = responseLBS status400 [] msg
78+ responseOk content = responseLBS status200 [(" content-type" , " application/json" )] content
7779
7880 handleGetSnapshots =
7981 listSnapshots db >>= \ snapshotsPoints ->
80- send $
81- responseLBS
82- status200
83- [(" content-type" , " application/json" )]
84- (encode snapshotsPoints)
82+ send $ responseOk (encode snapshotsPoints)
8583
86- handleGetHeader slot hash =
84+ handleWithPoint action slot hash =
8785 case makePoint slot hash of
88- Nothing ->
89- send $ responseLBS status400 [] " Malformed hash or slot"
86+ Nothing -> send $ responseBadRequest " Malformed hash or slot"
9087 Just point ->
91- getHeader db point >>= \ case
88+ action db point >>= \ case
9289 Err NotFound -> send responseNotFound
93- Err err -> send $ responseLBS status400 [] (" Bad query: " <> toBytestring err)
94- Found header -> send $ responseLBS status200 [(" content-type" , " application/text" )] (LHex. encode header)
90+ Err err -> send $ responseBadRequest (" Bad query: " <> toBytestring err)
91+ Found result -> send $ responseOk (LHex. encode result)
92+
93+ handleGetHeader slot hash = handleWithPoint getHeader slot hash
9594
9695 handleGetSnapshot slot =
9796 case makeSlot slot of
98- Nothing -> send $ responseLBS status400 [] " Malformed slot"
97+ Nothing -> send $ responseBadRequest " Malformed slot"
9998 Just slot' ->
10099 getSnapshot db slot' >>= \ case
101100 Err NotFound -> send responseNotFound
102- Err err -> send $ responseLBS status400 [] (" Bad query: " <> toBytestring err)
103- Found snapshot ->
104- send $
105- responseLBS
106- status200
107- [(" content-type" , " application/json" )]
108- (LHex. encode snapshot)
109-
110- handleGetParent slot hash =
111- case makePoint slot hash of
112- Nothing ->
113- send $ responseLBS status400 [] " Malformed hash or slot"
114- Just point ->
115- getParent db point >>= \ case
116- Err NotFound -> send responseNotFound
117- Err err -> send $ responseLBS status400 [] (" Bad query: " <> toBytestring err)
118- Found parent -> send $ responseLBS status200 [(" content-type" , " application/json" )] (LHex. encode parent)
101+ Err err -> send $ responseBadRequest (" Bad query: " <> toBytestring err)
102+ Found snapshot -> send $ responseOk (LHex. encode snapshot)
119103
120- handleGetBlock slot hash = do
121- case makePoint slot hash of
122- Nothing ->
123- send $ responseLBS status400 [] " Malformed hash or slot"
124- Just point ->
125- getBlock db point >>= \ case
126- Err NotFound -> send responseNotFound
127- Err err -> send $ responseLBS status400 [] (" Bad query: " <> toBytestring err)
128- Found parent -> send $ responseLBS status200 [(" content-type" , " application/json" )] (LHex. encode parent)
104+ handleGetParent slot hash = handleWithPoint getParent slot hash
105+
106+ handleGetBlock slot hash = handleWithPoint getBlock slot hash
129107
130108-- * Tracing
131109
0 commit comments