Skip to content

Commit

Permalink
Merge pull request #225 from VictorAvelar/bugfix/backwards-incompatib…
Browse files Browse the repository at this point in the history
…le-changes-on-v3.4

fix(config): remove breaking change
  • Loading branch information
VictorAvelar authored Feb 12, 2023
2 parents 7c96ad8 + 2ebf9b8 commit f304cab
Show file tree
Hide file tree
Showing 11 changed files with 306 additions and 42 deletions.
4 changes: 1 addition & 3 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@
},
"go.lintTool": "golangci-lint",
"go.lintOnSave": "package",
"go.toolsEnvVars": {
"CGO_ENABLED": 1
},
"go.toolsEnvVars": {},
"go.testFlags": [
"-v",
"-race"
Expand Down
56 changes: 48 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,65 @@ go get -u github.com/VictorAvelar/mollie-api-go/v3/mollie

## Usage

### Create a basic client
### Testing using API tokens

#### Using the config helper

```go
// Create a configuration object with idempotency enabled.
config := mollie.NewAPITestingConfig(true)
```

#### Using the NewConfig method

```go
// Create a configuration object with idempotency enabled.
config := mollie.NewConfig(true, mollie.ApiTokenEnv)

_ := config.ToggleIdempotency()
```

### Testing using Organization access tokens

#### Using the config helper for org tokens

```go
// main.go
/*
Parse authentication key from MOLLIE_API_TOKEN
*/
config := mollie.NewConfig(true, mollie.APITokenEnv)
client, err := mollie.NewClient(nil, config)
// Create a configuration object with idempotency enabled.
config := mollie.NewOrgTestingConfig(true)
```

#### Using the NewConfig method for org tokens

```go
// Create a configuration object with idempotency enabled.
config := mollie.NewConfig(true, mollie.OrgTokenEnv)

_ := config.ToggleIdempotency()
```

### Create an API client

```go
// build your desired config
client, err := mollie.NewClient(config)
if err != nil {
log.Fatal(err)
}
// do your operations with the authenticated client
// perform operations with the API.
```

## Upgrade guide

If you want to upgrade from v2 -> v3, the list of breaking and notable changes can be found in the [docs](docs/v3-upgrade.md).

## :warning: Notice for v3.4 & v3.5

These versions contain breaking changes in the `mollie.NewConfig` method, the number of parameters
increased thus breaking any previous implementation and requiring manual changes when updating.

The issue is patched for v3.6 which also includes a bunch of helper methods in the `mollie.Config` struct
to make the usage and changes in the values more manageable.

## API parity

Checks to the API changelog are performed constantly to ensure API parity and compatibility, however it might happen that not all the changes are implemented right away.
Expand Down
80 changes: 76 additions & 4 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -870,16 +870,88 @@ type Config struct {

Config contains information that helps during the setup of a new Mollie client.

#### func NewAPIConfig

```go
func NewAPIConfig(reqIdem bool) *Config
```

NewAPIConfig builds a configuration object with the following settings: tests
mode: disabled api token source: MOLLIE_API_TOKEN

it receives `reqIdem (boolean)` to enable the request idempotency feature.

#### func NewAPITestingConfig

```go
func NewAPITestingConfig(reqIdem bool) *Config
```

NewAPITestingConfig builds a configuration object with the following settings:
tests mode: enabled api token source: MOLLIE_API_TOKEN

it receives `reqIdem (boolean)` to enable the request idempotency feature.

#### func NewConfig

```go
func NewConfig(t, reqIdem bool, auth string) *Config
func NewConfig(t bool, auth string) *Config
```

NewConfig builds a Mollie configuration object, it takes t to indicate if our
client is meant to create requests for testing, reqIdem to enable the request
idempotency beta feature, and auth to indicate the authentication method we want
to use.
client is meant to create requests for testing, and auth to indicate the
authentication method we want to use.

#### func NewOrgConfig

```go
func NewOrgConfig(reqIdem bool) *Config
```

NewOrgConfig builds a configuration object with the following settings: tests
mode: disabled Org token source: MOLLIE_ORG_TOKEN

it receives `reqIdem (boolean)` to enable the request idempotency feature.

#### func NewOrgTestingConfig

```go
func NewOrgTestingConfig(reqIdem bool) *Config
```

NewOrgTestingConfig builds a configuration object with the following settings:
tests mode: enabled api token source: MOLLIE_ORG_TOKEN

it receives `reqIdem (boolean)` to enable the request idempotency feature.

#### func (*Config) SwitchAuthStrategy

```go
func (c *Config) SwitchAuthStrategy(auth string) string
```

SwitchAuthStrategy changes the environment variable used to fetch the auth
tokens.

Known values are: [MOLLIE_API_TOKEN,MOLLIE_ORG_TOKEN], if you use a custom
environment variable pass it as argument.

#### func (*Config) ToggleIdempotency

```go
func (c *Config) ToggleIdempotency() bool
```

ToggleIdempotency enables/disables the request idempotency feature in the
current Config.

#### func (*Config) ToggleTesting

```go
func (c *Config) ToggleTesting() bool
```

ToggleTesting enables/disables the test-mode in the current Config.

#### type ContextValue

Expand Down
5 changes: 1 addition & 4 deletions mollie/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ type ShortDate struct {
// MarshalJSON overrides the default marshal action
// for the Date struct. Returns date as YYYY-MM-DD formatted string.
func (d *ShortDate) MarshalJSON() ([]byte, error) {
bts, err := json.Marshal(d.Time.Format("2006-01-02"))
if err != nil {
return nil, fmt.Errorf("json_marshall_error: %w", err)
}
bts, _ := json.Marshal(d.Time.Format("2006-01-02"))

return bts, nil
}
Expand Down
73 changes: 70 additions & 3 deletions mollie/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,80 @@ type Config struct {
reqIdempotency bool
}

// ToggleTesting enables/disables the test-mode in the current Config.
func (c *Config) ToggleTesting() bool {
c.testing = !c.testing

return c.testing
}

// ToggleIdempotency enables/disables the request idempotency feature
// in the current Config.
func (c *Config) ToggleIdempotency() bool {
c.reqIdempotency = !c.reqIdempotency

return c.reqIdempotency
}

// SwitchAuthStrategy changes the environment variable used to fetch the
// auth tokens.
//
// Known values are: [MOLLIE_API_TOKEN,MOLLIE_ORG_TOKEN], if you use a custom
// environment variable pass it as argument.
func (c *Config) SwitchAuthStrategy(auth string) string {
c.auth = auth

return c.auth
}

/* Configuration init helpers. */

// NewConfig builds a Mollie configuration object,
// it takes t to indicate if our client is meant to create requests for testing,
// reqIdem to enable the request idempotency beta feature,
// and auth to indicate the authentication method we want to use.
func NewConfig(t, reqIdem bool, auth string) *Config {
func NewConfig(t bool, auth string) *Config {
return createConfig(t, false, auth)
}

// NewAPITestingConfig builds a configuration object with the following settings:
// tests mode: enabled
// api token source: MOLLIE_API_TOKEN
//
// it receives `reqIdem (boolean)` to enable the request idempotency feature.
func NewAPITestingConfig(reqIdem bool) *Config {
return createConfig(true, reqIdem, APITokenEnv)
}

// NewAPIConfig builds a configuration object with the following settings:
// tests mode: disabled
// api token source: MOLLIE_API_TOKEN
//
// it receives `reqIdem (boolean)` to enable the request idempotency feature.
func NewAPIConfig(reqIdem bool) *Config {
return createConfig(false, reqIdem, APITokenEnv)
}

// NewOrgTestingConfig builds a configuration object with the following settings:
// tests mode: enabled
// api token source: MOLLIE_ORG_TOKEN
//
// it receives `reqIdem (boolean)` to enable the request idempotency feature.
func NewOrgTestingConfig(reqIdem bool) *Config {
return createConfig(true, reqIdem, OrgTokenEnv)
}

// NewOrgConfig builds a configuration object with the following settings:
// tests mode: disabled
// Org token source: MOLLIE_ORG_TOKEN
//
// it receives `reqIdem (boolean)` to enable the request idempotency feature.
func NewOrgConfig(reqIdem bool) *Config {
return createConfig(false, reqIdem, OrgTokenEnv)
}

func createConfig(test, reqIdem bool, auth string) *Config {
return &Config{
testing: t,
testing: test,
auth: auth,
reqIdempotency: reqIdem,
}
Expand Down
98 changes: 94 additions & 4 deletions mollie/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,33 @@ import (
"github.com/stretchr/testify/assert"
)

func TestConfig_ToggleTesting(t *testing.T) {
c := NewAPITestingConfig(false)

assert.True(t, c.testing)
c.ToggleTesting()
assert.False(t, c.testing)
}

func TestConfig_ToggleIdempotency(t *testing.T) {
c := NewAPITestingConfig(false)

assert.False(t, c.reqIdempotency)
assert.True(t, c.ToggleIdempotency())
assert.True(t, c.reqIdempotency)
}

func TestConfig_SwitchAuthStrategy(t *testing.T) {
c := NewAPITestingConfig(false)

assert.Equal(t, APITokenEnv, c.auth)
c.SwitchAuthStrategy(OrgTokenEnv)
assert.Equal(t, OrgTokenEnv, c.auth)
}

func TestNewConfig(t *testing.T) {
type args struct {
t bool
ikg bool
auth string
}
tests := []struct {
Expand Down Expand Up @@ -65,13 +88,80 @@ func TestNewConfig(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := NewConfig(tt.args.t, tt.args.ikg, tt.args.auth)
got := NewConfig(tt.args.t, tt.args.auth)
assert.Equal(t, got, tt.want)
})
}
}

// ExampleNewConfig demonstrates how to initialize a Config
// struct with the specified values for testing and token source.
func ExampleNewConfig() {
fmt.Println(NewConfig(true, true, APITokenEnv))
// Output: &{true MOLLIE_API_TOKEN true}
conf := NewConfig(true, APITokenEnv)
fmt.Printf(
"testing config, testing: %v, req_idempotency: %v, token source: %s.",
conf.testing,
conf.reqIdempotency,
conf.auth,
)
// Output: testing config, testing: true, req_idempotency: false, token source: MOLLIE_API_TOKEN.
}

// ExampleNewAPITestingConfig demonstrates how to initialize a Config
// struct with testing mode enabled, token source from the default API
// token env variable (MOLLIE_API_TOKEN) and request idempotency feature
// enabled.
func ExampleNewAPITestingConfig() {
conf := NewAPITestingConfig(true)
fmt.Printf(
"testing api config, testing: %v, req_idempotency: %v, token source: %s.",
conf.testing,
conf.reqIdempotency,
conf.auth,
)
// Output: testing api config, testing: true, req_idempotency: true, token source: MOLLIE_API_TOKEN.
}

// ExampleNewOrgTestingConfig demonstrates how to initialize a Config
// struct with testing mode enabled, token source from the default Org
// token env variable (MOLLIE_ORG_TOKEN) and request idempotency feature
// enabled.
func ExampleNewOrgTestingConfig() {
conf := NewOrgTestingConfig(true)
fmt.Printf(
"testing org config, testing: %v, req_idempotency: %v, token source: %s.",
conf.testing,
conf.reqIdempotency,
conf.auth,
)
// Output: testing org config, testing: true, req_idempotency: true, token source: MOLLIE_ORG_TOKEN.
}

// ExampleNewAPIConfig demonstrates how to initialize a Config
// struct with testing mode disabled, token source from the default API
// token env variable (MOLLIE_API_TOKEN) and request idempotency feature
// enabled.
func ExampleNewAPIConfig() {
conf := NewAPIConfig(true)
fmt.Printf(
"testing api config, testing: %v, req_idempotency: %v, token source: %s.",
conf.testing,
conf.reqIdempotency,
conf.auth,
)
// Output: testing api config, testing: false, req_idempotency: true, token source: MOLLIE_API_TOKEN.
}

// ExampleNewOrgConfig demonstrates how to initialize a Config struct
// with testing mode disabled, token source from the default Org token
// env variable (MOLLIE_ORG_TOKEN) and request idempotency feature enabled.
func ExampleNewOrgConfig() {
conf := NewOrgConfig(true)
fmt.Printf(
"testing org config, testing: %v, req_idempotency: %v, token source: %s.",
conf.testing,
conf.reqIdempotency,
conf.auth,
)
// Output: testing org config, testing: false, req_idempotency: true, token source: MOLLIE_ORG_TOKEN.
}
Loading

0 comments on commit f304cab

Please sign in to comment.