Commit 9eb2b966 authored by Sybren A. Stüvel's avatar Sybren A. Stüvel

Reduced logging + closing session at exit

parent 6b228730
......@@ -7,7 +7,6 @@ import (
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
"github.com/sirupsen/logrus"
......@@ -133,23 +132,25 @@ func (c *Client) DoRequest(method, endpoint string, payload interface{}, respons
return &errorResponse
}
if err := json.Unmarshal(respBody, response); err != nil {
logger.WithFields(logrus.Fields{
logrus.ErrorKey: err,
"body": string(respBody),
}).Fatal("unable to parse response JSON")
}
if response != nil {
if err := json.Unmarshal(respBody, response); err != nil {
logger.WithFields(logrus.Fields{
logrus.ErrorKey: err,
"body": string(respBody),
}).Fatal("unable to parse response JSON")
}
fmt.Println("\nBODY:\n" + string(respBody))
fmt.Println("\nParsed and then as JSON:")
asJson, err := json.MarshalIndent(response, "", " ")
if err != nil {
panic(err)
}
os.Stdout.Write(asJson)
fmt.Println()
// fmt.Println("\nBODY:\n" + string(respBody))
// fmt.Println("\nParsed and then as JSON:")
// asJSON, err := json.MarshalIndent(response, "", " ")
// if err != nil {
// panic(err)
// }
// os.Stdout.Write(asJSON)
// fmt.Println()
logger.WithField("body", string(respBody)).Info("response")
// logger.WithField("body", string(respBody)).Info("response")
}
return nil
}
......@@ -24,10 +24,13 @@ func main() {
"2001:984:42c1:1:712e:7ce7:8e46:1fdb",
})
client.SessionStart()
defer client.SessionStop()
bankAccount := bunqapi.MonetaryAccountBank{
Currency: "EUR",
}
client.CreateMonetaryAccountBank(bankAccount)
// bankAccount := bunqapi.MonetaryAccountBank{
// Currency: "EUR",
// Description: "met geld er op?",
// Balance: &bunqapi.Amount{Value: "1033.41", Currency: "EUR"},
// }
// client.CreateMonetaryAccountBank(bankAccount)
client.GetMonetaryAccountBankList()
}
package bunqapi
import "github.com/sirupsen/logrus"
import (
"fmt"
"github.com/sirupsen/logrus"
)
// Session stores session information the client.
type Session struct {
ID int
Token string
UserCompany *UserCompany
UserPerson *UserPerson
......@@ -41,6 +47,7 @@ func (c *Client) SessionStart() {
response := sessionServerResponse{}
MergeStructs(wrappedResponse.Response, &response)
c.session.ID = response.ID.ID
c.session.Token = response.Token.Token
c.session.UserCompany = response.UserCompany
c.session.UserPerson = response.UserPerson
......@@ -51,3 +58,14 @@ func (c *Client) SessionStart() {
"session": c.session.Token,
}).Info("session created")
}
func (c *Client) SessionStop() {
logger := log.WithField("sessionID", c.session.ID)
logger.Info("closing session")
url := fmt.Sprintf("session/%d", c.session.ID)
errResp := c.DoRequest("DELETE", url, nil, nil)
if errResp != nil {
logger.WithFields(errResp.LogFields()).Error("error performing session deletion request")
}
}
......@@ -13,8 +13,6 @@ import (
"net/textproto"
"sort"
"strings"
"github.com/sirupsen/logrus"
)
// The headers to include in the signature. "X-Bunq-..." headers are always included.
......@@ -39,7 +37,7 @@ func (c *Client) SignRequest(r *http.Request) error {
hasher := sha256.New()
hash := func(value string) {
log.WithField("value", value).Debug("writing to hasher")
// log.WithField("value", value).Debug("writing to hasher")
hasher.Write([]byte(value))
}
......@@ -61,7 +59,7 @@ func (c *Client) SignRequest(r *http.Request) error {
log.WithError(err).Panic("unable to read request body for signing")
}
hasher.Write(bodyBytes)
log.WithField("value", string(bodyBytes)).Debug("writing body to hasher")
// log.WithField("value", string(bodyBytes)).Debug("writing body to hasher")
r.Body = ioutil.NopCloser(bytes.NewReader(bodyBytes))
......@@ -78,10 +76,10 @@ func (c *Client) SignRequest(r *http.Request) error {
}
encodedSig := base64.StdEncoding.EncodeToString(signature)
log.WithFields(logrus.Fields{
"shaSum": fmt.Sprintf("%x", sum),
"signature": encodedSig,
}).Debug("calculated signature")
// log.WithFields(logrus.Fields{
// "shaSum": fmt.Sprintf("%x", sum),
// "signature": encodedSig,
// }).Debug("calculated signature")
r.Header.Set(headerXBunqClientSignature, encodedSig)
return nil
......@@ -96,7 +94,7 @@ func (c *Client) VerifyResponse(r *http.Response) error {
hasher := sha256.New()
hash := func(value string) {
log.WithField("value", value).Debug("writing to hasher")
// log.WithField("value", value).Debug("writing to hasher")
hasher.Write([]byte(value))
}
......@@ -120,7 +118,7 @@ func (c *Client) VerifyResponse(r *http.Response) error {
log.WithError(err).Panic("unable to read response body for signing")
}
hasher.Write(bodyBytes)
log.WithField("value", string(bodyBytes)).Debug("writing body to hasher")
// log.WithField("value", string(bodyBytes)).Debug("writing body to hasher")
r.Body = ioutil.NopCloser(bytes.NewReader(bodyBytes))
......@@ -135,11 +133,11 @@ func (c *Client) VerifyResponse(r *http.Response) error {
log.WithError(err).Fatal("unable to verify server signature with public RSA key")
}
encodedSig := base64.StdEncoding.EncodeToString(serverSignature)
log.WithFields(logrus.Fields{
"shaSum": fmt.Sprintf("%x", sum),
"signature": encodedSig,
}).Debug("verified server response signature")
// encodedSig := base64.StdEncoding.EncodeToString(serverSignature)
// log.WithFields(logrus.Fields{
// "shaSum": fmt.Sprintf("%x", sum),
// "signature": encodedSig,
// }).Debug("verified server response signature")
return nil
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment