Skip to content

mollie/mollie-api-golang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

client

Developer-friendly & type-safe Go SDK specifically catered to leverage client API.

Summary

Table of Contents

SDK Installation

To add the SDK as a dependency to your project:

go get github.com/mollie/mollie-api-golang

SDK Example Usage

Example

package main

import (
	"context"
	client "github.com/mollie/mollie-api-golang"
	"github.com/mollie/mollie-api-golang/models/components"
	"github.com/mollie/mollie-api-golang/models/operations"
	"log"
	"os"
)

func main() {
	ctx := context.Background()

	s := client.New(
		client.WithSecurity(components.Security{
			APIKey: client.String(os.Getenv("CLIENT_API_KEY")),
		}),
	)

	res, err := s.Balances.List(ctx, operations.ListBalancesRequest{
		Currency:       client.String("EUR"),
		From:           client.String("bal_gVMhHKqSSRYJyPsuoPNFH"),
		Limit:          client.Int64(50),
		Testmode:       client.Bool(false),
		IdempotencyKey: client.String("123e4567-e89b-12d3-a456-426"),
	})
	if err != nil {
		log.Fatal(err)
	}
	if res.Object != nil {
		// handle response
	}
}

Authentication

Per-Client Security Schemes

This SDK supports the following security schemes globally:

Name Type Scheme Environment Variable
APIKey http HTTP Bearer CLIENT_API_KEY
OAuth oauth2 OAuth2 token CLIENT_O_AUTH

You can set the security parameters through the WithSecurity option when initializing the SDK client instance. The selected scheme will be used by default to authenticate with the API for all operations that support it. For example:

package main

import (
	"context"
	client "github.com/mollie/mollie-api-golang"
	"github.com/mollie/mollie-api-golang/models/components"
	"github.com/mollie/mollie-api-golang/models/operations"
	"log"
	"os"
)

func main() {
	ctx := context.Background()

	s := client.New(
		client.WithSecurity(components.Security{
			APIKey: client.String(os.Getenv("CLIENT_API_KEY")),
		}),
	)

	res, err := s.Balances.List(ctx, operations.ListBalancesRequest{
		Currency:       client.String("EUR"),
		From:           client.String("bal_gVMhHKqSSRYJyPsuoPNFH"),
		Limit:          client.Int64(50),
		Testmode:       client.Bool(false),
		IdempotencyKey: client.String("123e4567-e89b-12d3-a456-426"),
	})
	if err != nil {
		log.Fatal(err)
	}
	if res.Object != nil {
		// handle response
	}
}

Available Resources and Operations

Available methods
  • List - List capabilities
  • List - List payment chargebacks
  • Get - Get payment chargeback
  • All - List all chargebacks
  • List - List clients
  • Get - Get client
  • Create - Create a delayed route
  • List - List payment routes
  • List - List invoices
  • Get - Get invoice
  • List - List payment methods
  • All - List all payment methods
  • Get - Get payment method
  • Get - Get onboarding status
  • Submit - Submit onboarding data
  • List - List permissions
  • Get - Get permission
  • Create - Create payment refund
  • List - List payment refunds
  • Get - Get payment refund
  • Cancel - Cancel payment refund
  • All - List all refunds
  • Create - Create sales invoice
  • List - List sales invoices
  • Get - Get sales invoice
  • Update - Update sales invoice
  • Delete - Delete sales invoice
  • Create - Create subscription
  • List - List customer subscriptions
  • Get - Get subscription
  • Update - Update subscription
  • Cancel - Cancel subscription
  • All - List all subscriptions
  • ListPayments - List subscription payments
  • List - List terminals
  • Get - Get terminal
  • Get - Get a Webhook Event
  • Create - Create a webhook
  • List - List all webhooks
  • Update - Update a webhook
  • Get - Get a webhook
  • Delete - Delete a webhook
  • Test - Test a webhook

Retries

Some of the endpoints in this SDK support retries. If you use the SDK without any configuration, it will fall back to the default retry strategy provided by the API. However, the default retry strategy can be overridden on a per-operation basis, or across the entire SDK.

To change the default retry strategy for a single API call, simply provide a retry.Config object to the call by using the WithRetries option:

package main

import (
	"context"
	client "github.com/mollie/mollie-api-golang"
	"github.com/mollie/mollie-api-golang/models/components"
	"github.com/mollie/mollie-api-golang/models/operations"
	"github.com/mollie/mollie-api-golang/retry"
	"log"
	"models/operations"
	"os"
)

func main() {
	ctx := context.Background()

	s := client.New(
		client.WithSecurity(components.Security{
			APIKey: client.String(os.Getenv("CLIENT_API_KEY")),
		}),
	)

	res, err := s.Balances.List(ctx, operations.ListBalancesRequest{
		Currency:       client.String("EUR"),
		From:           client.String("bal_gVMhHKqSSRYJyPsuoPNFH"),
		Limit:          client.Int64(50),
		Testmode:       client.Bool(false),
		IdempotencyKey: client.String("123e4567-e89b-12d3-a456-426"),
	}, operations.WithRetries(
		retry.Config{
			Strategy: "backoff",
			Backoff: &retry.BackoffStrategy{
				InitialInterval: 1,
				MaxInterval:     50,
				Exponent:        1.1,
				MaxElapsedTime:  100,
			},
			RetryConnectionErrors: false,
		}))
	if err != nil {
		log.Fatal(err)
	}
	if res.Object != nil {
		// handle response
	}
}

If you'd like to override the default retry strategy for all operations that support retries, you can use the WithRetryConfig option at SDK initialization:

package main

import (
	"context"
	client "github.com/mollie/mollie-api-golang"
	"github.com/mollie/mollie-api-golang/models/components"
	"github.com/mollie/mollie-api-golang/models/operations"
	"github.com/mollie/mollie-api-golang/retry"
	"log"
	"os"
)

func main() {
	ctx := context.Background()

	s := client.New(
		client.WithRetryConfig(
			retry.Config{
				Strategy: "backoff",
				Backoff: &retry.BackoffStrategy{
					InitialInterval: 1,
					MaxInterval:     50,
					Exponent:        1.1,
					MaxElapsedTime:  100,
				},
				RetryConnectionErrors: false,
			}),
		client.WithSecurity(components.Security{
			APIKey: client.String(os.Getenv("CLIENT_API_KEY")),
		}),
	)

	res, err := s.Balances.List(ctx, operations.ListBalancesRequest{
		Currency:       client.String("EUR"),
		From:           client.String("bal_gVMhHKqSSRYJyPsuoPNFH"),
		Limit:          client.Int64(50),
		Testmode:       client.Bool(false),
		IdempotencyKey: client.String("123e4567-e89b-12d3-a456-426"),
	})
	if err != nil {
		log.Fatal(err)
	}
	if res.Object != nil {
		// handle response
	}
}

Error Handling

Handling errors in this SDK should largely match your expectations. All operations return a response object or an error, they will never return both.

By Default, an API error will return apierrors.APIError. When custom error responses are specified for an operation, the SDK may also return their associated error. You can refer to respective Errors tables in SDK docs for more details on possible error types for each operation.

For example, the List function may return the following errors:

Error Type Status Code Content Type
apierrors.ErrorResponse 400, 404 application/hal+json
apierrors.APIError 4XX, 5XX */*

Example

package main

import (
	"context"
	"errors"
	client "github.com/mollie/mollie-api-golang"
	"github.com/mollie/mollie-api-golang/models/apierrors"
	"github.com/mollie/mollie-api-golang/models/components"
	"github.com/mollie/mollie-api-golang/models/operations"
	"log"
	"os"
)

func main() {
	ctx := context.Background()

	s := client.New(
		client.WithSecurity(components.Security{
			APIKey: client.String(os.Getenv("CLIENT_API_KEY")),
		}),
	)

	res, err := s.Balances.List(ctx, operations.ListBalancesRequest{
		Currency:       client.String("EUR"),
		From:           client.String("bal_gVMhHKqSSRYJyPsuoPNFH"),
		Limit:          client.Int64(50),
		Testmode:       client.Bool(false),
		IdempotencyKey: client.String("123e4567-e89b-12d3-a456-426"),
	})
	if err != nil {

		var e *apierrors.ErrorResponse
		if errors.As(err, &e) {
			// handle error
			log.Fatal(e.Error())
		}

		var e *apierrors.APIError
		if errors.As(err, &e) {
			// handle error
			log.Fatal(e.Error())
		}
	}
}

Server Selection

Override Server URL Per-Client

The default server can be overridden globally using the WithServerURL(serverURL string) option when initializing the SDK client instance. For example:

package main

import (
	"context"
	client "github.com/mollie/mollie-api-golang"
	"github.com/mollie/mollie-api-golang/models/components"
	"github.com/mollie/mollie-api-golang/models/operations"
	"log"
	"os"
)

func main() {
	ctx := context.Background()

	s := client.New(
		client.WithServerURL("https://api.mollie.com/v2"),
		client.WithSecurity(components.Security{
			APIKey: client.String(os.Getenv("CLIENT_API_KEY")),
		}),
	)

	res, err := s.Balances.List(ctx, operations.ListBalancesRequest{
		Currency:       client.String("EUR"),
		From:           client.String("bal_gVMhHKqSSRYJyPsuoPNFH"),
		Limit:          client.Int64(50),
		Testmode:       client.Bool(false),
		IdempotencyKey: client.String("123e4567-e89b-12d3-a456-426"),
	})
	if err != nil {
		log.Fatal(err)
	}
	if res.Object != nil {
		// handle response
	}
}

Custom HTTP Client

The Go SDK makes API calls that wrap an internal HTTP client. The requirements for the HTTP client are very simple. It must match this interface:

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

The built-in net/http client satisfies this interface and a default client based on the built-in is provided by default. To replace this default with a client of your own, you can implement this interface yourself or provide your own client configured as desired. Here's a simple example, which adds a client with a 30 second timeout.

import (
	"net/http"
	"time"

	"github.com/mollie/mollie-api-golang"
)

var (
	httpClient = &http.Client{Timeout: 30 * time.Second}
	sdkClient  = client.New(client.WithClient(httpClient))
)

This can be a convenient way to configure timeouts, cookies, proxies, custom headers, and other low-level configuration.

Special Types

This SDK defines the following custom types to assist with marshalling and unmarshalling data.

Date

types.Date is a wrapper around time.Time that allows for JSON marshaling a date string formatted as "2006-01-02".

Usage

d1 := types.NewDate(time.Now()) // returns *types.Date

d2 := types.DateFromTime(time.Now()) // returns types.Date

d3, err := types.NewDateFromString("2019-01-01") // returns *types.Date, error

d4, err := types.DateFromString("2019-01-01") // returns types.Date, error

d5 := types.MustNewDateFromString("2019-01-01") // returns *types.Date and panics on error

d6 := types.MustDateFromString("2019-01-01") // returns types.Date and panics on error

Development

Maturity

This SDK is in beta, and there may be breaking changes between versions without a major version update. Therefore, we recommend pinning usage to a specific package version. This way, you can install the same version each time without breaking changes unless you are intentionally looking for the latest version.

Contributions

While we value open-source contributions to this SDK, this library is generated programmatically. Any manual changes added to internal files will be overwritten on the next generation. We look forward to hearing your feedback. Feel free to open a PR or an issue with a proof of concept and we'll do our best to include it in a future release.

SDK Created by Speakeasy

About

Mollie's SDK for Go (alpha version)

Resources

License

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages