Skip to content

Commit

Permalink
refactor: comments, make thing to the standard (coinbase data)
Browse files Browse the repository at this point in the history
  • Loading branch information
will7200 committed May 10, 2021
1 parent 1334737 commit 4a8351c
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 63 deletions.
2 changes: 1 addition & 1 deletion internal/pc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func Sync(email, password string, cfg *personalcapital.Configuration, holds prov
target = left
case providers.ExistsInOriginationOnly:
left := holds[value.LPos]
log.Infof("%s not found in pc, create new holding\n", left.CurrencySymbolName())
log.Infof("%s not found in pc, create new holding", left.CurrencySymbolName())
if err != nil {
log.Fatal(err)
}
Expand Down
38 changes: 20 additions & 18 deletions internal/providers/bscscan/holding.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (d *Data) Validate() error {
return errs.AsError()
}

// Coinbase Provider
// Provider for bscscan
type Provider struct {
once sync.Once
data *Data
Expand All @@ -69,24 +69,25 @@ func (p *Provider) Name() string {
return "bscscan"
}

func (p *Provider) GetHoldings() (providers.Holdings, error) {
p.once.Do(func() {
client := etherscan.NewCustomized(etherscan.Customization{
Timeout: 15 * time.Second,
Key: p.data.ApiKey,
BaseURL: p.data.BaseURL,
Verbose: p.data.Debug,
BeforeRequest: nil,
AfterRequest: nil,
})
p.client = client
//client.BeforeRequest = func(module, action string, param map[string]interface{}) error {
// // ...
//}
//client.AfterRequest = func(module, action string, param map[string]interface{}, outcome interface{}, requestErr error) {
// // ...
//}
func (p *Provider) Once() {
client := etherscan.NewCustomized(etherscan.Customization{
Timeout: 15 * time.Second,
Key: p.data.ApiKey,
BaseURL: p.data.BaseURL,
Verbose: p.data.Debug,
BeforeRequest: nil,
AfterRequest: nil,
})
p.client = client
//client.BeforeRequest = func(module, action string, param map[string]interface{}) error {
// // ...
//}
//client.AfterRequest = func(module, action string, param map[string]interface{}, outcome interface{}, requestErr error) {
// // ...
//}
}

func (p *Provider) GetHoldings() (providers.Holdings, error) {
client := p.client
h := make([]providers.Holding, 0, 25)
for _, account := range p.data.Accounts {
Expand Down Expand Up @@ -139,5 +140,6 @@ func (p *Provider) Open(config providers.Config, params ...interface{}) (provide
} else {
return nil, errors.New("invalid parameters")
}
p.Once()
return p, nil
}
100 changes: 59 additions & 41 deletions internal/providers/coinbase/holding.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/HereMobilityDevelopers/mediary"
"github.com/mitchellh/mapstructure"
"github.com/shopspring/decimal"
"github.com/will7200/go-crypto-sync/internal/common"
"go.uber.org/zap"
Expand All @@ -21,12 +22,31 @@ func init() {
providers.Register("coinbase", &Provider{})
}

// Coinbase Provider
type Data struct {
ApiKey string `mapstructure:"apiKey"`
ApiSecret string `mapstructure:"apiSecret"`
Debug bool `mapstructure:"debug"`
}

func (d *Data) SetDefaults() {

}

func (d *Data) Validate() (err error) {
errs := new(common.Errors)
if d.ApiKey == "" {
errs.Add(errors.New("Invalid API Key"))
}
if d.ApiSecret == "" {
errs.Add(errors.New("Invalid API Secret"))
}
return errs.AsError()
}

// Provider for Coinbase
type Provider struct {
apiKey string
apiSecret string
debug bool
once sync.Once
once sync.Once
data *Data

//
client *coinbase.APIClient
Expand All @@ -42,26 +62,28 @@ func (p *Provider) Name() string {
return "coinbase"
}

func (p *Provider) Once() {
config := coinbase.NewConfiguration()
config.Debug = p.data.Debug
config.APIKey = coinbase.APIKey{
Key: p.data.ApiKey,
Secret: p.data.ApiSecret,
}
client := &http.Client{
Timeout: 15 * time.Second,
}
httpClient := mediary.Init().WithPreconfiguredClient(client)

if p.data.Debug {
httpClient = httpClient.AddInterceptors(common.DumpRequestResponseWrappedLogger(p.logger))
}
config.HTTPClient = httpClient.Build()
p.client = coinbase.NewAPIClient(config)
}

// GetHoldings returns all holds for coinbase
func (p *Provider) GetHoldings() (providers.Holdings, error) {
p.once.Do(func() {
config := coinbase.NewConfiguration()
config.Debug = p.debug
config.APIKey = coinbase.APIKey{
Key: p.apiKey,
Secret: p.apiSecret,
}
client := &http.Client{
Timeout: 15 * time.Second,
}
httpClient := mediary.Init().WithPreconfiguredClient(client)

if p.debug {
httpClient = httpClient.AddInterceptors(common.DumpRequestResponseWrappedLogger(p.logger))
}
config.HTTPClient = httpClient.Build()
p.client = coinbase.NewAPIClient(config)
})
p.Once()
client := p.client
ctx := context.Background()
var nextURI string
Expand Down Expand Up @@ -100,15 +122,7 @@ loop:
}

func (p *Provider) GetExchange(currency1, currency2 string) (string, error) {
p.once.Do(func() {
config := coinbase.NewConfiguration()
config.Debug = p.debug
config.APIKey = coinbase.APIKey{
Key: p.apiKey,
Secret: p.apiSecret,
}
p.client = coinbase.NewAPIClient(config)
})
p.Once()
client := p.client
ctx := context.Background()
price, _, err := client.ExchangeRatesApi.GetExchangeRateFor(ctx).Currency(currency1).Execute()
Expand Down Expand Up @@ -136,25 +150,29 @@ func (p *Provider) Open(config providers.Config, params ...interface{}) (provide
if !ok {
return nil, errors.New("invalid parameters")
}
p.apiKey, ok = m["apiKey"].(string)
if !ok {
return nil, errors.New("apiKey parameter missing")
d := &Data{}
err := mapstructure.Decode(m, d)
if err != nil {
return nil, err
}
p.apiSecret, ok = m["apiSecret"].(string)
if !ok {
return nil, errors.New("apiSecret parameter missing")
d.SetDefaults()
err = d.Validate()
if err != nil {
return nil, err
}
p.debug, _ = m["debug"].(bool)
p.data = d
} else if len(params) == 2 {
for i, v := range params {
if _, ok := v.(string); !ok {
return nil, fmt.Errorf("invalid type(%T) for parameter index %d", v, i)
}
}
p.apiKey = params[0].(string)
p.apiSecret = params[1].(string)
p.data = &Data{}
p.data.ApiKey = params[0].(string)
p.data.ApiSecret = params[1].(string)
} else {
return nil, errors.New("invalid parameters")
}
p.Once()
return p, nil
}
2 changes: 1 addition & 1 deletion internal/providers/coinbasepro/holding.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (d *Data) Validate() error {
return err.AsError()
}

// Coinbase Provider
// Provider for coinbase pro
type Provider struct {
once sync.Once
data *Data
Expand Down
2 changes: 1 addition & 1 deletion internal/providers/etherscan/holding.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (d *Data) Validate() (err error) {
return
}

// Coinbase Provider
// Provider for etherscan
type Provider struct {
once sync.Once
data *Data
Expand Down
2 changes: 1 addition & 1 deletion internal/providers/nomics/pricing.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (d *Data) Validate() (err error) {
return
}

// Nomics Provider
// Provider for Nomics
type Provider struct {
data *Data
once sync.Once
Expand Down

0 comments on commit 4a8351c

Please sign in to comment.