@@ -34,7 +34,7 @@ import Hydra.Tx (
34
34
IsTx (.. ),
35
35
UTxOType ,
36
36
)
37
- import Network.HTTP.Types (status200 , status400 , status404 , status500 )
37
+ import Network.HTTP.Types (ResponseHeaders , hContentType , status200 , status400 , status404 , status500 )
38
38
import Network.Wai (
39
39
Application ,
40
40
Request (pathInfo , requestMethod ),
@@ -141,6 +141,9 @@ instance (Arbitrary tx, Arbitrary (UTxOType tx), IsTx tx) => Arbitrary (SideLoad
141
141
shrink = \ case
142
142
SideLoadSnapshotRequest snapshot -> SideLoadSnapshotRequest <$> shrink snapshot
143
143
144
+ jsonContent :: ResponseHeaders
145
+ jsonContent = [(hContentType, " application/json" )]
146
+
144
147
-- | Hydra HTTP server
145
148
httpApp ::
146
149
forall tx .
@@ -193,19 +196,19 @@ httpApp tracer directChain env pparams getHeadState getCommitInfo getPendingDepo
193
196
>>= handleRecoverCommitUtxo putClientInput (last . fromList $ pathInfo request)
194
197
>>= respond
195
198
(" GET" , [" commits" ]) ->
196
- getPendingDeposits >>= respond . responseLBS status200 [] . Aeson. encode
199
+ getPendingDeposits >>= respond . responseLBS status200 jsonContent . Aeson. encode
197
200
(" POST" , [" decommit" ]) ->
198
201
consumeRequestBodyStrict request
199
202
>>= handleDecommit putClientInput
200
203
>>= respond
201
204
(" GET" , [" protocol-parameters" ]) ->
202
- respond . responseLBS status200 [] . Aeson. encode $ pparams
205
+ respond . responseLBS status200 jsonContent . Aeson. encode $ pparams
203
206
(" POST" , [" cardano-transaction" ]) ->
204
207
consumeRequestBodyStrict request
205
208
>>= handleSubmitUserTx directChain
206
209
>>= respond
207
210
_ ->
208
- respond $ responseLBS status400 [] " Resource not found"
211
+ respond $ responseLBS status400 jsonContent . Aeson. encode $ Aeson. String " Resource not found"
209
212
210
213
-- * Handlers
211
214
@@ -225,7 +228,7 @@ handleDraftCommitUtxo ::
225
228
handleDraftCommitUtxo env directChain getCommitInfo body = do
226
229
case Aeson. eitherDecode' body :: Either String (DraftCommitTxRequest tx ) of
227
230
Left err ->
228
- pure $ responseLBS status400 [] (Aeson. encode $ Aeson. String $ pack err)
231
+ pure $ responseLBS status400 jsonContent (Aeson. encode $ Aeson. String $ pack err)
229
232
Right someCommitRequest ->
230
233
getCommitInfo >>= \ case
231
234
NormalCommit headId ->
@@ -249,7 +252,7 @@ handleDraftCommitUtxo env directChain getCommitInfo body = do
249
252
-- expires one deposit period before deadline.
250
253
deadline <- addUTCTime (3 * toNominalDiffTime depositPeriod) <$> getCurrentTime
251
254
draftDepositTx headId commitBlueprint deadline <&> \ case
252
- Left e -> responseLBS status400 [] (Aeson. encode $ toJSON e)
255
+ Left e -> responseLBS status400 jsonContent (Aeson. encode $ toJSON e)
253
256
Right depositTx -> okJSON $ DraftCommitTxResponse depositTx
254
257
255
258
draftCommit headId lookupUTxO blueprintTx = do
@@ -282,11 +285,11 @@ handleRecoverCommitUtxo putClientInput recoverPath _body = do
282
285
Left err -> pure err
283
286
Right recoverTxId -> do
284
287
putClientInput Recover {recoverTxId}
285
- pure $ responseLBS status200 [] (Aeson. encode $ Aeson. String " OK" )
288
+ pure $ responseLBS status200 jsonContent (Aeson. encode $ Aeson. String " OK" )
286
289
where
287
290
parseTxIdFromPath txIdStr =
288
291
case Aeson. eitherDecode (encodeUtf8 txIdStr) :: Either String (TxIdType tx ) of
289
- Left e -> Left (responseLBS status400 [] (Aeson. encode $ Aeson. String $ " Cannot recover funds. Failed to parse TxId: " <> pack e))
292
+ Left e -> Left (responseLBS status400 jsonContent (Aeson. encode $ Aeson. String $ " Cannot recover funds. Failed to parse TxId: " <> pack e))
290
293
Right txid -> Right txid
291
294
292
295
-- | Handle request to submit a cardano transaction.
@@ -300,23 +303,23 @@ handleSubmitUserTx ::
300
303
handleSubmitUserTx directChain body = do
301
304
case Aeson. eitherDecode' body of
302
305
Left err ->
303
- pure $ responseLBS status400 [] (Aeson. encode $ Aeson. String $ pack err)
306
+ pure $ responseLBS status400 jsonContent (Aeson. encode $ Aeson. String $ pack err)
304
307
Right txToSubmit -> do
305
308
try (submitTx txToSubmit) <&> \ case
306
309
Left (e :: PostTxError Tx ) -> badRequest e
307
310
Right _ ->
308
- responseLBS status200 [] (Aeson. encode TransactionSubmitted )
311
+ responseLBS status200 jsonContent (Aeson. encode TransactionSubmitted )
309
312
where
310
313
Chain {submitTx} = directChain
311
314
312
315
handleDecommit :: forall tx . FromJSON tx => (ClientInput tx -> IO () ) -> LBS. ByteString -> IO Response
313
316
handleDecommit putClientInput body =
314
317
case Aeson. eitherDecode' body :: Either String tx of
315
318
Left err ->
316
- pure $ responseLBS status400 [] (Aeson. encode $ Aeson. String $ pack err)
319
+ pure $ responseLBS status400 jsonContent (Aeson. encode $ Aeson. String $ pack err)
317
320
Right decommitTx -> do
318
321
putClientInput Decommit {decommitTx}
319
- pure $ responseLBS status200 [] (Aeson. encode $ Aeson. String " OK" )
322
+ pure $ responseLBS status200 jsonContent (Aeson. encode $ Aeson. String " OK" )
320
323
321
324
-- | Handle request to side load confirmed snapshot.
322
325
handleSideLoadSnapshot ::
@@ -328,16 +331,16 @@ handleSideLoadSnapshot ::
328
331
handleSideLoadSnapshot putClientInput body = do
329
332
case Aeson. eitherDecode' body :: Either String (SideLoadSnapshotRequest tx ) of
330
333
Left err ->
331
- pure $ responseLBS status400 [] (Aeson. encode $ Aeson. String $ pack err)
334
+ pure $ responseLBS status400 jsonContent (Aeson. encode $ Aeson. String $ pack err)
332
335
Right SideLoadSnapshotRequest {snapshot} -> do
333
336
putClientInput $ SideLoadSnapshot snapshot
334
- pure $ responseLBS status200 [] (Aeson. encode $ Aeson. String " OK" )
337
+ pure $ responseLBS status200 jsonContent (Aeson. encode $ Aeson. String " OK" )
335
338
336
339
badRequest :: IsChainState tx => PostTxError tx -> Response
337
- badRequest = responseLBS status400 [] . Aeson. encode . toJSON
340
+ badRequest = responseLBS status400 jsonContent . Aeson. encode . toJSON
338
341
339
342
notFound :: Response
340
- notFound = responseLBS status404 [] " "
343
+ notFound = responseLBS status404 jsonContent ( Aeson. encode $ Aeson. String " " )
341
344
342
345
okJSON :: ToJSON a => a -> Response
343
- okJSON = responseLBS status200 [] . Aeson. encode
346
+ okJSON = responseLBS status200 jsonContent . Aeson. encode
0 commit comments