-
Notifications
You must be signed in to change notification settings - Fork 3
/
UserHandlers.purs
108 lines (101 loc) · 3.03 KB
/
UserHandlers.purs
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
module Seath.HTTP.UserHandlers (mkHttpHandlers) where
import Contract.Prelude
import Aeson (class EncodeAeson, decodeJsonString)
import Data.UUID (UUID, parseUUID)
import Effect.Aff (Aff, error, throwError)
import Payload.ResponseTypes (Response(Response))
import Seath.Common.Types (UID(UID))
import Seath.Core.Types (UserAction)
import Seath.HTTP.Client (UserClient)
import Seath.HTTP.Client as Client
import Seath.HTTP.Types
( IncludeRequest(IncludeRequest)
, SendSignedRequest(SendSignedRequest)
)
import Seath.Network.Types
( AcceptSignedTransactionError
, ActionStatus
, IncludeActionError
, NetworkHandlers
, SendSignedTransaction
)
import Type.Proxy (Proxy(Proxy))
mkHttpHandlers
:: forall a
. EncodeAeson a
=> String
-> NetworkHandlers a
mkHttpHandlers leaderUrl =
{ submitToLeader: handleSendAction client
, sendSignedToLeader: handleSendSignedToLeader client
, refuseToSign: handleRefuseToSign client
, getActionStatus: handleGetStatus client
}
where
client :: UserClient a
client = Client.mkUserClient (Proxy :: Proxy a) leaderUrl
handleSendAction
:: forall a
. EncodeAeson a
=> UserClient a
-> UserAction a
-> Aff (Either IncludeActionError UUID)
handleSendAction client action = do
res <- client.leader.includeAction
{ body: IncludeRequest action }
case res of
Right resp -> do
convertResonse resp
Left r -> throwError
(error $ "Leader failed to respond to send action: " <> show r)
where
convertResonse (Response r) =
if (r.body.status == "success") then
maybe (throwError $ error "Can't parse request ID") (Right >>> pure)
(parseUUID r.body.data)
else either (show >>> error >>> throwError) (Left >>> pure)
(decodeJsonString r.body.data)
handleSendSignedToLeader
:: forall a
. UserClient a
-> SendSignedTransaction
-> Aff (Either AcceptSignedTransactionError Unit)
handleSendSignedToLeader client sendSig = do
res <- client.leader.acceptSignedTransaction
{ body: SendSignedRequest sendSig }
case res of
-- FIXME: leader responds witn Unit always
-- but client expects AcceptSignedTransactionError as well
Right _resp -> pure $ Right unit
Left e -> throwError (error $ show e)
handleRefuseToSign
:: forall a
. UserClient a
-> UUID
-> Aff Unit
handleRefuseToSign client uuid = do
res <-
client.leader.refuseToSign
{ params: { uid: UID uuid } }
case res of
Right _ -> pure unit
Left r -> throwError (error $ show r)
handleGetStatus
:: forall a
. UserClient a
-> UUID
-> Aff ActionStatus
handleGetStatus client uuid = do
res <-
client.leader.actionStatus
{ params: { uid: UID uuid } }
case res of
Right resp -> convertResonse resp
Left r -> throwError (error $ "Leader failed to respond: " <> show r)
where
convertResonse (Response r) =
if (r.body.status == "success") then
either (show >>> error >>> throwError) pure
(decodeJsonString r.body.data)
else either (show >>> error >>> throwError) (error >>> throwError)
(decodeJsonString r.body.data)