n26api

package module
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 8, 2023 License: MIT Imports: 14 Imported by: 6

README

N26 API Client

GitHub Releases Build Status codecov Go Report Card GoDevDoc Donate

Unofficial N26 API Client for Golang.

Disclaimer

This project is NOT sponsored or funded by N26 nor any of its competitors.

The client is built on a collection of observed API calls and methods provided by Rots/n26-api.

Prerequisites

  • Go >= 1.17

Install

go get github.com/nhatthm/n26api

Development

API Service

TBA

API Client

TBD

Testkit
Unit Test

The services can be easily mocked by using the testkit, all the mocked interface is provided by stretchr/testify/mock

For example: Mocking transaction.Finder service

package test

import (
    "context"
    "testing"
    "time"

    "github.com/google/uuid"
    transactionMock "github.com/nhatthm/n26api/pkg/testkit/transaction"
    "github.com/nhatthm/n26api/pkg/transaction"
    "github.com/stretchr/testify/assert"
)

func TestTransactions(t *testing.T) {
    t.Parallel()

    from := time.Now()
    to := from.Add(time.Hour)
    id := uuid.New()

    f := transactionMock.MockFinder(func(f *transactionMock.Finder) {
        f.On("FindAllTransactionsInRange", context.Background(), from, to).
            Return(
                []transaction.Transaction{
                    {ID: id, OriginalAmount: 3.5},
                },
                nil,
            )
    })(t)

    expected := []transaction.Transaction{
        {ID: id, OriginalAmount: 3.5},
    }

    result, err := f.FindAllTransactionsInRange(context.Background(), from, to)

    assert.Equal(t, expected, result)
    assert.NoError(t, err)
}
Integration Test

The testkit provides a mocked API server for testing.

For example:

package test

import (
    "context"
    "testing"
    "time"

    "github.com/google/uuid"
    "github.com/nhatthm/n26api"
    "github.com/nhatthm/n26api/pkg/testkit"
    "github.com/nhatthm/n26api/pkg/transaction"
)

func TestFindTransactions(t *testing.T) {
    t.Parallel()

    username := "username"
    password := "password"
    deviceID := uuid.New()
    from := time.Now()
    to := from.Add(time.Hour)
    pageSize := int64(1)
    id1 := uuid.New()
    id2 := uuid.New()

    s := testkit.MockServer(username, password, deviceID,
        testkit.WithFindAllTransactionsInRange(
            from, to, pageSize,
            []transaction.Transaction{{ID: id1}, {ID: id2}},
        ),
    )(t)

    c := n26api.NewClient(
        n26api.WithBaseURL(s.URL()),
        n26api.WithDeviceID(deviceID),
        n26api.WithCredentials(username, password),
        n26api.WithMFAWait(5*time.Millisecond),
        n26api.WithMFATimeout(time.Second),
        n26api.WithTransactionsPageSize(pageSize),
    )

    result, err := c.FindAllTransactionsInRange(context.Background(), from, to)

    // assertions
}
Server Options
WithFindAllTransactionsInRange

TBD

Test

Unit Test

Run

make test

or

make test-unit
Integration Test

TBD

GDPR

No information is used or store by this module.

References

Donation

If this project help you reduce time to develop, you can give me a cup of coffee :)

Paypal donation

paypal

       or scan this

Documentation

Overview

Package n26api provides N26 API client.

Index

Constants

View Source
const (
	// BaseURL is N26 API Base URL.
	BaseURL = api.DefaultBaseURL
	// DefaultPageSize is the default page size while requesting to N26.
	DefaultPageSize int64 = 50
)

Variables

View Source
var (
	// ErrUsernameIsEmpty indicates that the username is empty.
	ErrUsernameIsEmpty = errors.New("missing username")
	// ErrPasswordIsEmpty indicates that the username is empty.
	ErrPasswordIsEmpty = errors.New("missing password")
)
View Source
var ErrTokenKeyEmpty = errors.New("token key is empty")

ErrTokenKeyEmpty indicates that the key of token is empty and we can not persist that.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client provides all N26 APIs.

func NewClient

func NewClient(options ...Option) *Client

NewClient initiates a new transaction.Finder.

func (*Client) DeviceID added in v0.2.5

func (c *Client) DeviceID() uuid.UUID

DeviceID returns device ID.

func (*Client) FindAllTransactionsInRange

func (c *Client) FindAllTransactionsInRange(ctx context.Context, from time.Time, to time.Time) ([]transaction.Transaction, error)

FindAllTransactionsInRange finds all transactions in a time period.

type CredentialsProvider

type CredentialsProvider interface {
	// Username provides a username.
	Username() string
	// Password provides a password.
	Password() string
}

CredentialsProvider provides username and password for authentication.

func Credentials

func Credentials(username string, password string) CredentialsProvider

Credentials initiates a new credentials provider.

func CredentialsFromEnv

func CredentialsFromEnv() CredentialsProvider

CredentialsFromEnv initiates a new credentials provider.

type InMemoryTokenStorage added in v0.1.1

type InMemoryTokenStorage struct {
	// contains filtered or unexported fields
}

InMemoryTokenStorage persists auth.OAuthToken into its memory.

func NewInMemoryTokenStorage added in v0.1.1

func NewInMemoryTokenStorage() *InMemoryTokenStorage

NewInMemoryTokenStorage initiates a new InMemoryTokenStorage.

func (*InMemoryTokenStorage) Get added in v0.1.1

Get gets OAuthToken from memory.

func (*InMemoryTokenStorage) Set added in v0.1.1

Set sets OAuthToken to memory.

type Option

type Option func(c *Client)

Option configures Client.

func WithBaseURL

func WithBaseURL(baseURL string) Option

WithBaseURL sets API Base URL.

func WithClock

func WithClock(clock clock.Clock) Option

WithClock sets the clock (for testing purpose).

func WithCredentials

func WithCredentials(username string, password string) Option

WithCredentials sets username and password to login.

func WithCredentialsProvider

func WithCredentialsProvider(provider CredentialsProvider) Option

WithCredentialsProvider chains a new credentials provider.

func WithCredentialsProviderAtLast added in v0.2.1

func WithCredentialsProviderAtLast(provider CredentialsProvider) Option

WithCredentialsProviderAtLast chains a new credentials provider at last position.

func WithDeviceID

func WithDeviceID(deviceID uuid.UUID) Option

WithDeviceID sets device ID to login.

func WithMFATimeout

func WithMFATimeout(timeout time.Duration) Option

WithMFATimeout sets the MFA Timeout for authentication.

func WithMFAWait

func WithMFAWait(waitTime time.Duration) Option

WithMFAWait sets the MFA Wait Time for authentication.

func WithPassword

func WithPassword(password string) Option

WithPassword sets password to login.

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout sets API Timeout.

func WithTokenProvider

func WithTokenProvider(provider auth.TokenProvider) Option

WithTokenProvider chains a new token provider.

func WithTokenStorage added in v0.1.1

func WithTokenStorage(storage auth.TokenStorage) Option

WithTokenStorage sets token storage for the internal apiTokenProvider.

func WithTransactionsPageSize

func WithTransactionsPageSize(limit int64) Option

WithTransactionsPageSize sets page size limit for finding transactions.

func WithUsername

func WithUsername(username string) Option

WithUsername sets username to login.

type RoundTripperFunc

type RoundTripperFunc func(*http.Request) (*http.Response, error)

RoundTripperFunc is an inline http.RoundTripper.

func BasicAuthRoundTripper

func BasicAuthRoundTripper(username, password string, tripper http.RoundTripper) RoundTripperFunc

BasicAuthRoundTripper sets Basic Authorization header to the given request.

func BearerAuthRoundTripper

func BearerAuthRoundTripper(token string, tripper http.RoundTripper) RoundTripperFunc

BearerAuthRoundTripper sets Bearer Authorization header to the given request.

func TokenRoundTripper

func TokenRoundTripper(p auth.TokenProvider, tripper http.RoundTripper) RoundTripperFunc

TokenRoundTripper sets Bearer Authorization header to the given request with a token given by a auth.TokenProvider.

func (RoundTripperFunc) RoundTrip

func (fn RoundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip satisfies RoundTripperFunc.

Directories

Path Synopsis
internal
api
Package api contains autogenerated REST client.
Package api contains autogenerated REST client.
pkg
auth
Package auth provides contracts for N26 authentication.
Package auth provides contracts for N26 authentication.
testkit
Package testkit provides functionalities for testing N26 APIs.
Package testkit provides functionalities for testing N26 APIs.
testkit/auth
Package auth provides functionalities for testing N26 authentication.
Package auth provides functionalities for testing N26 authentication.
testkit/transaction
Package transaction provides functionalities for testing N26 Transaction APIs.
Package transaction provides functionalities for testing N26 Transaction APIs.
transaction
Package transaction provides contracts for N26 Transaction APIs.
Package transaction provides contracts for N26 Transaction APIs.
util
Package util provides additional helpers.
Package util provides additional helpers.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL