Skip to content

Commit

Permalink
chore(client): add usage examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mitch000001 committed Mar 3, 2024
1 parent 61c8b35 commit 32202c5
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 14 deletions.
15 changes: 15 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -71,6 +72,7 @@ func New(config Config) (*Client, error) {
}
hbciVersion = version
}
config.HBCIVersion = hbciVersion.Version()
dcfg := dialog.Config{
BankID: bankID,
HBCIURL: url,
Expand All @@ -85,6 +87,7 @@ func New(config Config) (*Client, error) {
d.SetPin(config.PIN)
client := &Client{
config: config,
bankInfo: bankInfo,
hbciVersion: hbciVersion,
pinTanDialog: d,
}
Expand All @@ -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
}
Expand All @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions client/client_feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
72 changes: 72 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}
})
}
}
68 changes: 54 additions & 14 deletions client/doc.go
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 32202c5

Please sign in to comment.