/
request_handler_tx_status.go
83 lines (73 loc) · 2.1 KB
/
request_handler_tx_status.go
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
package handlers
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
log "github.com/sirupsen/logrus"
"github.com/stellar/go/protocols/compliance"
"github.com/stellar/go/services/internal/bridge-compliance-shared/http/helpers"
)
// HandlerTxStatus implements /tx_status endpoint
func (rh *RequestHandler) HandlerTxStatus(w http.ResponseWriter, r *http.Request) {
txid := r.URL.Query().Get("id")
if txid == "" {
log.Info("unable to get query parameter")
helpers.Write(w, helpers.NewMissingParameter("id"))
return
}
response := compliance.TransactionStatusResponse{}
if rh.Config.Callbacks.TxStatus == "" {
response.Status = compliance.TransactionStatusUnknown
} else {
u, err := url.Parse(rh.Config.Callbacks.TxStatus)
if err != nil {
log.Error(err, "failed to parse tx status endpoint")
helpers.Write(w, helpers.InternalServerError)
return
}
q := u.Query()
q.Set("id", txid)
u.RawQuery = q.Encode()
resp, err := rh.Client.Get(u.String())
if err != nil {
log.WithFields(log.Fields{
"tx_status": u.String(),
"err": err,
}).Error("Error sending request to tx_status server")
helpers.Write(w, helpers.InternalServerError)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Error("Error reading tx_status server response")
helpers.Write(w, helpers.InternalServerError)
return
}
switch resp.StatusCode {
case http.StatusOK:
err := json.Unmarshal(body, &response)
if err != nil {
log.WithFields(log.Fields{
"tx_status": rh.Config.Callbacks.TxStatus,
"body": string(body),
}).Error("Unable to decode tx_status response")
helpers.Write(w, helpers.InternalServerError)
return
}
if response.Status == "" {
response.Status = compliance.TransactionStatusUnknown
}
default:
response.Status = compliance.TransactionStatusUnknown
}
}
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(response)
if err != nil {
log.Error("Error encoding tx status response")
helpers.Write(w, helpers.InternalServerError)
return
}
}