cashless2ecash

cashless2ecash: pay with cards for digital cash (experimental)
Log | Files | Refs | README

commit a4be42b4fa80f808dcde8cbc1fedc800d34c6b25
parent 9fdbbd29186748869285b6f2a4c8051e49d0e627
Author: Joel-Haeberli <haebu@rubigen.ch>
Date:   Sat, 18 May 2024 15:14:00 +0200

fix: set content type headers

Diffstat:
Mc2ec/api-bank-integration.go | 10++++++++--
Mc2ec/api-terminals.go | 9+++++++--
Mc2ec/api-wire-gateway.go | 8++++++--
Mc2ec/http-util.go | 2++
Mc2ec/simulation-client.go | 5+++--
Mc2ec/taler-wire-gateway-test.sh | 8++++++++
6 files changed, 34 insertions(+), 8 deletions(-)

diff --git a/c2ec/api-bank-integration.go b/c2ec/api-bank-integration.go @@ -74,13 +74,15 @@ func bankIntegrationConfig(res http.ResponseWriter, req *http.Request) { }, } - serializedCfg, err := NewJsonCodec[BankIntegrationConfig]().EncodeToBytes(&cfg) + encoder := NewJsonCodec[BankIntegrationConfig]() + serializedCfg, err := encoder.EncodeToBytes(&cfg) if err != nil { LogInfo("bank-integration-api", fmt.Sprintf("failed serializing config: %s", err.Error())) res.WriteHeader(HTTP_INTERNAL_SERVER_ERROR) return } + res.Header().Add(CONTENT_TYPE_HEADER, encoder.HttpApplicationContentHeader()) res.WriteHeader(HTTP_OK) res.Write(serializedCfg) } @@ -131,12 +133,14 @@ func handleParameterRegistration(res http.ResponseWriter, req *http.Request) { TransferDone: withdrawal.WithdrawalStatus == CONFIRMED, } - resbyts, err := NewJsonCodec[BankWithdrawalOperationPostResponse]().EncodeToBytes(resbody) + encoder := NewJsonCodec[BankWithdrawalOperationPostResponse]() + resbyts, err := encoder.EncodeToBytes(resbody) if err != nil { LogError("bank-integration-api", err) res.WriteHeader(HTTP_INTERNAL_SERVER_ERROR) } + res.Header().Add(CONTENT_TYPE_HEADER, encoder.HttpApplicationContentHeader()) res.Write(resbyts) } @@ -254,10 +258,12 @@ func handleWithdrawalStatus(res http.ResponseWriter, req *http.Request) { if stat != 200 { res.WriteHeader(stat) } else { + res.Header().Add(CONTENT_TYPE_HEADER, "application/json") res.Write(wthdrl) } return case wthdrl := <-w: + res.Header().Add(CONTENT_TYPE_HEADER, "application/json") res.Write(wthdrl) return case status := <-errStat: diff --git a/c2ec/api-terminals.go b/c2ec/api-terminals.go @@ -55,7 +55,8 @@ func handleTerminalConfig(res http.ResponseWriter, req *http.Request) { return } - cfg, err := NewJsonCodec[TerminalConfig]().EncodeToBytes(&TerminalConfig{ + encoder := NewJsonCodec[TerminalConfig]() + cfg, err := encoder.EncodeToBytes(&TerminalConfig{ Name: "taler-terminal", Version: "0:0:0", ProviderName: p.Name, @@ -67,6 +68,8 @@ func handleTerminalConfig(res http.ResponseWriter, req *http.Request) { res.WriteHeader(HTTP_INTERNAL_SERVER_ERROR) return } + + res.Header().Add(CONTENT_TYPE_HEADER, encoder.HttpApplicationContentHeader()) res.WriteHeader(HTTP_OK) res.Write(cfg) } @@ -129,7 +132,8 @@ func handleWithdrawalSetup(res http.ResponseWriter, req *http.Request) { return } - encodedBody, err := NewJsonCodec[TerminalWithdrawalSetupResponse]().EncodeToBytes( + encoder := NewJsonCodec[TerminalWithdrawalSetupResponse]() + encodedBody, err := encoder.EncodeToBytes( &TerminalWithdrawalSetupResponse{ Wopid: talerBinaryEncode(generatedWopid), }, @@ -140,6 +144,7 @@ func handleWithdrawalSetup(res http.ResponseWriter, req *http.Request) { return } + res.Header().Add(CONTENT_TYPE_HEADER, encoder.HttpApplicationContentHeader()) res.Write(encodedBody) } diff --git a/c2ec/api-wire-gateway.go b/c2ec/api-wire-gateway.go @@ -308,13 +308,15 @@ func historyIncoming(res http.ResponseWriter, req *http.Request) { } } - enc, err := NewJsonCodec[[]*IncomingReserveTransaction]().EncodeToBytes(&transactions) + encoder := NewJsonCodec[[]*IncomingReserveTransaction]() + enc, err := encoder.EncodeToBytes(&transactions) if err != nil { LogError("wire-gateway-api", err) res.WriteHeader(HTTP_INTERNAL_SERVER_ERROR) return } + res.Header().Add(CONTENT_TYPE_HEADER, encoder.HttpApplicationContentHeader()) res.WriteHeader(HTTP_OK) res.Write(enc) } @@ -397,13 +399,15 @@ func historyOutgoing(res http.ResponseWriter, req *http.Request) { OutgoingTransactions: transactions, DebitAccount: CONFIG.Server.CreditAccount, } - enc, err := NewJsonCodec[OutgoingHistory]().EncodeToBytes(&outgoingHistory) + encoder := NewJsonCodec[OutgoingHistory]() + enc, err := encoder.EncodeToBytes(&outgoingHistory) if err != nil { LogError("wire-gateway-api", err) res.WriteHeader(HTTP_INTERNAL_SERVER_ERROR) return } + res.Header().Add(CONTENT_TYPE_HEADER, encoder.HttpApplicationContentHeader()) res.WriteHeader(HTTP_OK) res.Write(enc) } diff --git a/c2ec/http-util.go b/c2ec/http-util.go @@ -21,6 +21,8 @@ const HTTP_METHOD_NOT_ALLOWED = 405 const HTTP_CONFLICT = 409 const HTTP_INTERNAL_SERVER_ERROR = 500 +const CONTENT_TYPE_HEADER = "Content-Type" + // Function reads and validates a param of a request in the // correct format according to the transform function supplied. // When the transform fails, it returns false as second return diff --git a/c2ec/simulation-client.go b/c2ec/simulation-client.go @@ -44,6 +44,7 @@ func (sc *SimulationClient) FormatPayto(w *Withdrawal) string { func (sc *SimulationClient) SetupClient(p *Provider) error { + LogInfo("simulation-client", "setting up simulation client. probably not what you want in production") fmt.Println("setting up simulation client. probably not what you want in production") sc.AllowNextWithdrawal = true @@ -54,7 +55,7 @@ func (sc *SimulationClient) SetupClient(p *Provider) error { func (sc *SimulationClient) GetTransaction(transactionId string) (ProviderTransaction, error) { - fmt.Println("getting transaction from simulation provider") + LogInfo("simulation-client", "getting transaction from simulation provider") time.Sleep(time.Duration(sc.providerBackendAttestationDelayMs) * time.Millisecond) st := new(SimulationTransaction) st.allow = sc.AllowNextWithdrawal @@ -63,6 +64,6 @@ func (sc *SimulationClient) GetTransaction(transactionId string) (ProviderTransa func (*SimulationClient) Refund(transactionId string) error { - fmt.Println("refund triggered for simulation provider with transaction id: ", transactionId) + LogInfo("simulation-client", "refund triggered for simulation provider with transaction id: "+transactionId) return nil } diff --git a/c2ec/taler-wire-gateway-test.sh b/c2ec/taler-wire-gateway-test.sh @@ -10,12 +10,20 @@ ### INCOMING HISTORY taler-exchange-wire-gateway-client +taler-exchange-wire-gateway-client -i -b https://bank.example.com + + ### TRANSFER taler-exchange-wire-gateway-client +taler-exchange-wire-gateway-client -C payto://example/bank/account -a EUR:10.00 -b https://bank.example.com +taler-exchange-wire-gateway-client -D payto://example/bank/account -a EUR:10.00 -b https://bank.example.com + ### OUTGOING HISTORY taler-exchange-wire-gateway-client +taler-exchange-wire-gateway-client -o -b https://bank.example.com + ### cleanup test data \ No newline at end of file