diff --git a/client/client.go b/client/client.go index 658935a..bfcbe43 100644 --- a/client/client.go +++ b/client/client.go @@ -58,6 +58,7 @@ func New(config Config) (*Client, error) { } else { url = bankInfo.URL } + config.URL = url if config.HBCIVersion > 0 { version, err := config.hbciVersion() if err != nil { @@ -71,6 +72,7 @@ func New(config Config) (*Client, error) { } hbciVersion = version } + config.HBCIVersion = hbciVersion.Version() dcfg := dialog.Config{ BankID: bankID, HBCIURL: url, @@ -85,6 +87,7 @@ func New(config Config) (*Client, error) { d.SetPin(config.PIN) client := &Client{ config: config, + bankInfo: bankInfo, hbciVersion: hbciVersion, pinTanDialog: d, } @@ -97,6 +100,7 @@ func New(config Config) (*Client, error) { // methods. type Client struct { config Config + bankInfo bankinfo.BankInfo hbciVersion segment.HBCIVersion pinTanDialog *dialog.PinTanDialog } @@ -111,6 +115,17 @@ func (c *Client) init() error { return nil } +// Config returns the config provided on creation with the additional fields being set +// after initialization +func (c *Client) Config() Config { + return c.config +} + +// BankInfo returns the bank institue the client is connected against +func (c *Client) BankInfo() bankinfo.BankInfo { + return c.bankInfo +} + // Accounts return the basic account information for the provided client config. func (c *Client) Accounts() ([]domain.AccountInformation, error) { if err := c.init(); err != nil { diff --git a/client/client_feature_test.go b/client/client_feature_test.go index bfa503b..432fd5d 100644 --- a/client/client_feature_test.go +++ b/client/client_feature_test.go @@ -13,6 +13,21 @@ import ( "github.com/mitch000001/go-hbci/iban" ) +func ExampleNew() { + clientConfig := client.Config{ + AccountID: "100000000", + BankID: "10010010", + PIN: "PIN", + } + c, err := client.New(clientConfig) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + fmt.Printf("Bank Institute: %s (%s)", c.BankInfo().Institute, c.BankInfo().City) + // Output: Bank Institute: Postbank (Berlin) +} + var testAccount domain.AccountConnection var sepaTestAccount domain.InternationalAccountConnection diff --git a/client/client_test.go b/client/client_test.go index 141d769..05c91da 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -6,6 +6,7 @@ import ( "testing" "time" + "github.com/mitch000001/go-hbci/bankinfo" "github.com/mitch000001/go-hbci/domain" https "github.com/mitch000001/go-hbci/transport/https" ) @@ -217,3 +218,74 @@ func setMockHTTPTransport(transport http.RoundTripper) func() { http.DefaultTransport = originHTTPTransport } } + +func TestClient_Config(t *testing.T) { + tests := []struct { + name string + providedConfig Config + want Config + }{ + { + name: "Minimal config", + providedConfig: Config{ + BankID: "10010010", + AccountID: "1234567890", + PIN: "12345", + }, + want: Config{ + BankID: "10010010", + AccountID: "1234567890", + PIN: "12345", + HBCIVersion: 300, + URL: "https://hbci.postbank.de/banking/hbci.do", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c, err := New(tt.providedConfig) + if err != nil { + t.Errorf("Error creating client: %v", err) + } + if got := c.Config(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Client.Config() = %#v, want %#v", got, tt.want) + } + }) + } +} + +func TestClient_BankInfo(t *testing.T) { + tests := []struct { + name string + providedConfig Config + want bankinfo.BankInfo + }{ + { + name: "Minimal config", + providedConfig: Config{ + BankID: "10010010", + }, + want: bankinfo.BankInfo{ + BankID: "10010010", + BIC: "PBNKDEFFXXX", + VersionNumber: "", + URL: "https://hbci.postbank.de/banking/hbci.do", + VersionName: "FinTS V3.0", + Institute: "Postbank", + City: "Berlin", + LastChanged: "04.02.2022", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c, err := New(tt.providedConfig) + if err != nil { + t.Errorf("Error creating client: %v", err) + } + if got := c.BankInfo(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Client.BankInfo() = %#v, want %#v", got, tt.want) + } + }) + } +} diff --git a/client/doc.go b/client/doc.go index 05e87a8..18e85c7 100644 --- a/client/doc.go +++ b/client/doc.go @@ -1,15 +1,55 @@ -// Package client provides a high level API for HBCI-Requests -// -// The main types of this package are the Config and the Client itself. The -// -// Config provides general information about the account to use. It should be -// sufficient to provide a config with BankID (i.e. 'Bankleitzahl, BLZ), -// AccountID (i.e. Kontonummer) and the PIN. The fields URL and HBCIVersion -// are optional fields for users with deeper knowledge about the bank institute -// and its HBCI endpoints. If one of these is not provided it will be looked up -// from the bankinfo package. -// -// Client provides a convenient way of issuing certain requests to the HBCI -// server. All low level APIs are queried from the Client and it returns only -// types from the domain package. +/* +Package client provides a high level API for HBCI-Requests + +The main types of this package are the Config and the Client itself. The + +Config provides general information about the account to use. It should be +sufficient to provide a config with BankID (i.e. 'Bankleitzahl, BLZ), +AccountID (i.e. Kontonummer) and the PIN. The fields URL and HBCIVersion +are optional fields for users with deeper knowledge about the bank institute +and its HBCI endpoints. If one of these is not provided it will be looked up +from the bankinfo package. + +Client provides a convenient way of issuing certain requests to the HBCI +server. All low level APIs are queried from the Client and it returns only +types from the domain package. + +# Example to get account transactions + + c := newClient() + + testAccount := domain.AccountConnection{ + AccountID: "1000000000", CountryCode: 280, BankID: "100000000" + } + + timeframe := domain.Timeframe{ + StartDate: domain.NewShortDate(time.Now().AddDate(0, 0, -90)), + } + + transactions, err := c.AccountTransactions(testAccount, timeframe, false, "") + if err != nil { + panic(err) + } + + for _, transaction := range transactions { + fmt.Printf("%+v\n", transaction) + } + +Example to get account balances + + c := newClient() + + testAccount := domain.AccountConnection{ + AccountID: "1000000000", CountryCode: 280, BankID: "100000000" + } + + balances, err := c.AccountBalances(testAccount, true) + if err != nil { + panic(err) + } + + for _, balance := range balances { + fmt.Printf("%+v\n", balance) + } +*/ package client