Skip to content

Commit

Permalink
fix: pc client invalid string for long decimal points
Browse files Browse the repository at this point in the history
  • Loading branch information
will7200 committed Apr 20, 2021
1 parent f8a1bda commit cc4b4ab
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
5 changes: 4 additions & 1 deletion cmd/crypto-sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
_ "github.com/will7200/go-crypto-sync/internal/holdings/coinbasepro"
_ "github.com/will7200/go-crypto-sync/internal/holdings/etherscan"
"github.com/will7200/go-crypto-sync/internal/pc"
"github.com/will7200/go-crypto-sync/pkg/personalcapital"
)

var (
Expand Down Expand Up @@ -72,7 +73,9 @@ func (s *SyncCmd) Run(ctx *Context) error {
raw := personCapitalValues.Execute(ctx.Tree)
email := raw.Values()[0].(*toml.Tree).Get("email").(string)
password := raw.Values()[0].(*toml.Tree).Get("password").(string)
pc.Sync(email, password, allHoldings.MapReduce(), pricingData)
cfg := personalcapital.NewConfiguration()
cfg.Debug = ctx.Debug
pc.Sync(email, password, cfg, allHoldings.MapReduce(), pricingData)
}
return nil
}
2 changes: 1 addition & 1 deletion internal/common/traffic.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ func DumpRequestResponse(req *http.Request, handler mediary.Handler) (*http.Resp
fmt.Printf("%s\n", bytes)
}
}
return handler(req)
return r, err
}
27 changes: 14 additions & 13 deletions internal/pc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import (
"strings"
"time"

"github.com/HereMobilityDevelopers/mediary"
"github.com/davecgh/go-spew/spew"
"github.com/shopspring/decimal"

"github.com/will7200/go-crypto-sync/internal/common"
"github.com/will7200/go-crypto-sync/internal/holdings"
"github.com/will7200/go-crypto-sync/pkg/personalcapital"
)
Expand All @@ -25,7 +27,7 @@ var (

// Sync source holdings to personal capital account
// Currently cookies will be saved in the working directory of where this command is run
func Sync(email, password string, holds holdings.Holdings, pricing holdings.Price) {
func Sync(email, password string, cfg *personalcapital.Configuration, holds holdings.Holdings, pricing holdings.Price) {

// Get saved cookies if available
var cookies []*http.Cookie
Expand All @@ -37,15 +39,17 @@ func Sync(email, password string, holds holdings.Holdings, pricing holdings.Pric
Jar: cookieJar,
Timeout: 30 * time.Second,
}
httpClient := mediary.Init().WithPreconfiguredClient(client)

cfg := personalcapital.NewConfiguration()

if cfg.Debug {
httpClient = httpClient.AddInterceptors(common.DumpRequestResponse)
}
u, err := url.Parse(cfg.Host)
if err != nil {
log.Println(err)
log.Fatal(err)
}
cfg.HTTPClient = httpClient.Build()
client.Jar.SetCookies(u, cookies)
cfg.HTTPClient = client

apiClient := personalcapital.NewAPIClient(cfg)
resp, err := apiClient.Authentication.Login(context.Background(), personalcapital.LoginParams{
Expand Down Expand Up @@ -78,9 +82,9 @@ func Sync(email, password string, holds holdings.Holdings, pricing holdings.Pric
}
log.Println("Holdings ", holds)
m := holds.HasCurrencyMap(func(l holdings.IHolding) string {
return strings.ToLower(l.CurrencyName())
return strings.ToLower(l.CurrencySymbolName())
}, func(r holdings.IHolding) string {
return strings.ToLower(strings.TrimSpace(strings.Split(r.CurrencyName(), "-")[0]))
return strings.ToLower(strings.TrimSpace(strings.Split(r.CurrencySymbolName(), "-")[0]))
}, personalcapital.PCHoldingsToIHoldings(pcHoldings.Holdings)...)

for key, value := range m {
Expand Down Expand Up @@ -135,7 +139,6 @@ func Sync(email, password string, holds holdings.Holdings, pricing holdings.Pric
if err != nil {
panic(err)
}
quantityFloat, _ := quantity.Float64()
p, err := pricing.GetExchange(holds[value.LPos].CurrencySymbolName(), "USD")
if err != nil {
panic(err)
Expand All @@ -144,21 +147,19 @@ func Sync(email, password string, holds holdings.Holdings, pricing holdings.Pric
if err != nil {
panic(err)
}
priceFloat, _ := pf.Float64()
v := pf.Mul(quantity)
valueFloat, _ := v.Float64()
name := holds[value.LPos].CurrencyName()
if len(name) < 5 {
name = name + " - " + "Cryptocurrency"
}
d := &personalcapital.HoldingAddRequest{
Ticker: name,
Quantity: quantityFloat,
Quantity: quantity.StringFixed(2),
Description: holds[value.LPos].CurrencySymbolName(),
Source: "USER",
Price: priceFloat,
Price: pf.StringFixed(14),
UserAccountId: account.UserAccountID,
Value: valueFloat,
Value: v.StringFixed(2),
}
_, err = apiClient.Holdings.AddHolding(context.Background(), d)
if err != nil {
Expand Down
22 changes: 11 additions & 11 deletions pkg/personalcapital/holdings.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,17 +295,17 @@ const (
type (
HoldingAddRequest struct {
// userAccountId=80828031&ticker=Stellar+Lumens&cusip=&description=XML&quantity=965.08&price=0.06&value=57.9048&source=USER&costBasis=&lastServerChangeId=152&csrf=3c19636c-175f-447c-9cc7-f56a5be07f0d&apiClient=WEB
Ticker string `url:"ticker"`
Quantity float64 `url:"quantity"`
Description string `url:"description"`
Source string `url:"source"`
Price float64 `url:"price"`
UserAccountId int `url:"userAccountId"`
CostBasis string `url:"costBasis"`
Value float64 `url:"value"`
LastServerChangeId string `url:"lastServerChangeId"`
Csrf string `url:"csrf"`
ApiClient string `url:"apiClient"`
Ticker string `url:"ticker"`
Quantity string `url:"quantity"`
Description string `url:"description"`
Source string `url:"source"`
Price string `url:"price"`
UserAccountId int `url:"userAccountId"`
CostBasis string `url:"costBasis"`
Value string `url:"value"`
LastServerChangeId string `url:"lastServerChangeId"`
Csrf string `url:"csrf"`
ApiClient string `url:"apiClient"`
}

AddHoldingResponse struct {
Expand Down

0 comments on commit cc4b4ab

Please sign in to comment.