Skip to content

Commit

Permalink
v2.0.0 (#4)
Browse files Browse the repository at this point in the history
* Changes generated by 90afb7b0bfe8400aa172a0c41d12cc89cab3a0cb

* Changes generated by d03f550064463c26c11345d6f913b82145c463e0

* Changes generated by 761a5f396ff8e06739254ec3d5994647745a2fa5

* Changes generated by 3f7e469a9ed21afa7425ef1f6ce0682c66f9ee84

* Changes generated by 9673ff362be6a2c82b88a34e52b61b6d0603c461

* Changes generated by 89d28f103fc65da5c9f4677e2b85b06c9c07b799

* Changes generated by 73fa74b77ef5f5b5e400b81a3563af6e74c2ac85

* Changes generated by 61d47cd4272cbfc368540ec736da14486b76b71c

* Changes generated by 1ed5a6598f0c9a12807f982f379311f20ff11ef8

* Changes generated by 3331ffcbdfa84b6ac199045b0fb50f60c3641426

* Changes generated by d2ff61a58e5ba09526bcb19a0664e0d1442e91b3

* Changes generated by 976187b15a5e307bbe6d7c588f36530a45139186

* Changes generated by 79077ca0952a7a942c9316d617e923b73ddd1aa1

* Changes generated by 750627fba2f3ef092a26bd8227b6f4fa2f6bba96

* Changes generated by c59277736a2d0e40ac0f9d8a42e1b9463a39edef

* Changes generated by 6d1a66b2decd2844457e4f635aa396041d4415dd

* Changes generated by 96427efe0958d47a42deeed61d74dd839ff3b801

* Changes generated by 86653b3d64a2ebc708cad205f692c4ef4f6ff502

* Changes generated by 957c7b6a278d7bbc180d245e31cc075042869e54

* Changes generated by e5377575d33701f1530977ced9f07b992bece019

* Changes generated by b0985abebd47ede4bc75cec926b0c9cc5b5a7384

* Changes generated by 813e6acfab0094225dd4820af515683eed3bacb3

* Changes generated by 7f472b1ae11ec581267eca9b6564d29c2cd670fe

* Changes generated by e4a1f03495fe9b2018c66096ede365a866b439ff

* Changes generated by 5a05b83be196c2f03480373c45c6ec522bc55511

* Changes generated by 42926f644eb35ecfccc50067b295ee652e5eaf4d

* Changes generated by 5a05b83be196c2f03480373c45c6ec522bc55511

Co-authored-by: Robot <[email protected]>
  • Loading branch information
1 parent 8dc9253 commit 1ef31e7
Show file tree
Hide file tree
Showing 63 changed files with 2,647 additions and 2,190 deletions.
86 changes: 64 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Go Client Library for GoCardless Pro Api
# Go Client Library for GoCardless Pro API [![CircleCI](https://circleci.com/gh/gocardless/gocardless-pro-go-template/tree/master.svg?style=svg&circle-token=68c31e704d9b0020a5f42b4b89b0a77a17bdac6c)](https://circleci.com/gh/gocardless/gocardless-pro-go-template/tree/master)

This library provides a simple wrapper around the [GoCardless API](http://developer.gocardless.com/api-reference).

Expand All @@ -18,7 +18,7 @@ go mod tidy
Then, reference gocardless-pro-go in a Go program with `import`:
``` go
import (
gocardless "github.com/gocardless/gocardless-pro-go"
gocardless "github.com/gocardless/gocardless-pro-go/v2"
)
```

Expand All @@ -29,7 +29,7 @@ toolchain will resolve and fetch the gocardless-pro-go module automatically.
Alternatively, you can also explicitly `go get` the package into a project:

```
go get -u github.com/gocardless/gocardless-pro-go/v1.0.0
go get -u github.com/gocardless/gocardless-pro-go@v2.0.0
```

## Initializing the client
Expand All @@ -38,23 +38,48 @@ The client is initialised with an access token, and is configured to use GoCardl

```go
token := "your_access_token"
client, err := gocardless.New(token)
config, err := gocardless.NewConfig(token)
if err != nil {
fmt.Printf("got err in initialising config: %s", err.Error())
return
}
client, err := gocardless.New(config)
if err != nil {
fmt.Printf("error in initialisating client: %s", err.Error())
return
}
```


Optionally, the client can be customised with endpoint, for ex: sandbox environment
```go
opts := gocardless.WithEndpoint(gocardless.SandboxEndpoint)
client, err := gocardless.New(token, opts)
config, err := gocardless.NewConfig(token, gocardless.WithEndpoint(gocardless.SandboxEndpoint))
if err != nil {
fmt.Printf("got err in initialising config: %s", err.Error())
return
}
client, err := gocardless.New(config)
if err != nil {
fmt.Printf("error in initialisating client: %s", err.Error())
return
}
```

the client can also be initialised with a customised http client, for ex;
```go
customHttpClient := &http.Client{
Timeout: time.Second * 10,
}
opts := gocardless.WithClient(customHttpClient)
client, err := gocardless.New(token, opts)
config, err := gocardless.NewConfig(token, gocardless.WithClient(customHttpClient))
if err != nil {
fmt.Printf("got err in initialising config: %s", err.Error())
return
}
client, err := gocardless.New(config)
if err != nil {
fmt.Printf("error in initialisating client: %s", err.Error())
return
}
```

## Examples
Expand Down Expand Up @@ -92,7 +117,7 @@ To fetch items in a collection, there are two options:
```go
ctx := context.TODO()
customerListParams := gocardless.CustomerListParams{}
customerListIterator := service.Customers.All(ctx, customerListParams)
customerListIterator := client.Customers.All(ctx, customerListParams)
for customerListIterator.Next() {
customerListResult, err := customerListIterator.Value(ctx)
if err != nil {
Expand All @@ -109,14 +134,15 @@ Resources can be created with the `Create` method:

```go
ctx := context.TODO()
customerCreateParams := CustomerCreateParams{}
customerCreateParams.AddressLine1 = "9 Acer Gardens"
customerCreateParams.City = "Birmingham"
customerCreateParams.CountryCode = "GB"
customerCreateParams.Email = "[email protected]"
customerCreateParams.FamilyName = "Rodgriguez"
customerCreateParams.GivenName = "Bender Bending"
customerCreateParams.PostalCode = "B4 7NJ"
customerCreateParams := gocardless.CustomerCreateParams{
AddressLine1: "9 Acer Gardens"
City: "Birmingham",
PostalCode: "B4 7NJ",
CountryCode: "GB",
Email: "[email protected]",
GivenName: "Bender Bending",
FamilyName: "Rodgriguez",
}

customer, err := client.Customers.Create(ctx, customerCreateParams)
```
Expand All @@ -127,8 +153,9 @@ Resources can be updates with the `Update` method:

```go
ctx := context.TODO()
customerUpdateParams := CustomerUpdateParams{}
customerUpdateParams.GivenName = "New name"
customerUpdateParams := CustomerUpdateParams{
GivenName: "New Name",
}

customer, err := client.Customers.Update(ctx, "CU123", customerUpdateParams)
```
Expand All @@ -152,7 +179,7 @@ The library will attempt to retry most failing requests automatically (with the

```go
requestOption := gocardless.WithoutRetries()
customersCreateResult, err := service.Customers.Create(ctx, customerCreateParams, requestOption)
customersCreateResult, err := client.Customers.Create(ctx, customerCreateParams, requestOption)
```

### Setting custom headers
Expand All @@ -163,15 +190,15 @@ in some cases (for example if you want to send an `Accept-Language` header when
headers := make(map[string]string)
headers["Accept-Language"] = "fr"
requestOption := gocardless.WithHeaders(headers)
customersCreateResult, err := service.Customers.Create(ctx, customerCreateParams, requestOption)
customersCreateResult, err := client.Customers.Create(ctx, customerCreateParams, requestOption)
```

Custom headers you specify will override any headers generated by the library itself (for
example, an `Idempotency-Key` header with a randomly-generated value or one you've configured
manually). Custom headers always take precedence.
```go
requestOption := gocardless.WithIdempotencyKey("test-idemptency-key-123")
customersCreateResult, err := service.Customers.Create(ctx, customerCreateParams, requestOption)
customersCreateResult, err := client.Customers.Create(ctx, customerCreateParams, requestOption)
```

### Handling webhooks
Expand All @@ -197,6 +224,21 @@ The client allows you to validate that a webhook you receive is genuinely from G
}
```

### Error Handling

When the library returns an `error` defined by us rather than the stdlib, it can be converted into a `gocardless.APIError` using `errors.As`:

```go
billingRequest, err := client.BillingRequests.Create(ctx, billingRequestCreateParams)
if err != nil {
var apiErr *gocardless.APIError
if errors.As(err, &apiErr) {
fmt.Printf("got err: %v", apiErr.Message)
}
return nil, err
}
```

## Compatibility

This library requires go 1.16 and above.
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.0
2.0.0
71 changes: 39 additions & 32 deletions bank_authorisation_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,37 @@ var _ = json.NewDecoder
var _ = errors.New

// BankAuthorisationService manages bank_authorisations
type BankAuthorisationService struct {
endpoint string
token string
client *http.Client
type BankAuthorisationServiceImpl struct {
config Config
}

type BankAuthorisationLinks struct {
BillingRequest string `url:"billing_request,omitempty" json:"billing_request,omitempty"`
Institution string `url:"institution,omitempty" json:"institution,omitempty"`
}

// BankAuthorisation model
type BankAuthorisation struct {
AuthorisationType string `url:"authorisation_type,omitempty" json:"authorisation_type,omitempty"`
AuthorisedAt string `url:"authorised_at,omitempty" json:"authorised_at,omitempty"`
CreatedAt string `url:"created_at,omitempty" json:"created_at,omitempty"`
ExpiresAt string `url:"expires_at,omitempty" json:"expires_at,omitempty"`
Id string `url:"id,omitempty" json:"id,omitempty"`
LastVisitedAt string `url:"last_visited_at,omitempty" json:"last_visited_at,omitempty"`
Links struct {
BillingRequest string `url:"billing_request,omitempty" json:"billing_request,omitempty"`
Institution string `url:"institution,omitempty" json:"institution,omitempty"`
} `url:"links,omitempty" json:"links,omitempty"`
RedirectUri string `url:"redirect_uri,omitempty" json:"redirect_uri,omitempty"`
Url string `url:"url,omitempty" json:"url,omitempty"`
AuthorisationType string `url:"authorisation_type,omitempty" json:"authorisation_type,omitempty"`
AuthorisedAt string `url:"authorised_at,omitempty" json:"authorised_at,omitempty"`
CreatedAt string `url:"created_at,omitempty" json:"created_at,omitempty"`
ExpiresAt string `url:"expires_at,omitempty" json:"expires_at,omitempty"`
Id string `url:"id,omitempty" json:"id,omitempty"`
LastVisitedAt string `url:"last_visited_at,omitempty" json:"last_visited_at,omitempty"`
Links *BankAuthorisationLinks `url:"links,omitempty" json:"links,omitempty"`
RedirectUri string `url:"redirect_uri,omitempty" json:"redirect_uri,omitempty"`
Url string `url:"url,omitempty" json:"url,omitempty"`
}

type BankAuthorisationService interface {
Get(ctx context.Context, identity string, opts ...RequestOption) (*BankAuthorisation, error)
Create(ctx context.Context, p BankAuthorisationCreateParams, opts ...RequestOption) (*BankAuthorisation, error)
}

// Get
// Fetches a bank authorisation
func (s *BankAuthorisationService) Get(ctx context.Context, identity string, opts ...RequestOption) (*BankAuthorisation, error) {
uri, err := url.Parse(fmt.Sprintf(s.endpoint+"/bank_authorisations/%v",
func (s *BankAuthorisationServiceImpl) Get(ctx context.Context, identity string, opts ...RequestOption) (*BankAuthorisation, error) {
uri, err := url.Parse(fmt.Sprintf(s.config.Endpoint()+"/bank_authorisations/%v",
identity))
if err != nil {
return nil, err
Expand All @@ -67,17 +72,17 @@ func (s *BankAuthorisationService) Get(ctx context.Context, identity string, opt
return nil, err
}
req.WithContext(ctx)
req.Header.Set("Authorization", "Bearer "+s.token)
req.Header.Set("Authorization", "Bearer "+s.config.Token())
req.Header.Set("GoCardless-Version", "2015-07-06")
req.Header.Set("GoCardless-Client-Library", "gocardless-pro-go")
req.Header.Set("GoCardless-Client-Version", "1.0.0")
req.Header.Set("GoCardless-Client-Version", "2.0.0")
req.Header.Set("User-Agent", userAgent)

for key, value := range o.headers {
req.Header.Set(key, value)
}

client := s.client
client := s.config.Client()
if client == nil {
client = http.DefaultClient
}
Expand Down Expand Up @@ -121,20 +126,22 @@ func (s *BankAuthorisationService) Get(ctx context.Context, identity string, opt
return result.BankAuthorisation, nil
}

type BankAuthorisationCreateParamsLinks struct {
BillingRequest string `url:"billing_request,omitempty" json:"billing_request,omitempty"`
Institution string `url:"institution,omitempty" json:"institution,omitempty"`
}

// BankAuthorisationCreateParams parameters
type BankAuthorisationCreateParams struct {
AuthorisationType string `url:"authorisation_type,omitempty" json:"authorisation_type,omitempty"`
Links struct {
BillingRequest string `url:"billing_request,omitempty" json:"billing_request,omitempty"`
Institution string `url:"institution,omitempty" json:"institution,omitempty"`
} `url:"links,omitempty" json:"links,omitempty"`
RedirectUri string `url:"redirect_uri,omitempty" json:"redirect_uri,omitempty"`
AuthorisationType string `url:"authorisation_type,omitempty" json:"authorisation_type,omitempty"`
Links BankAuthorisationCreateParamsLinks `url:"links,omitempty" json:"links,omitempty"`
RedirectUri string `url:"redirect_uri,omitempty" json:"redirect_uri,omitempty"`
}

// Create
// Create a Bank Authorisation.
func (s *BankAuthorisationService) Create(ctx context.Context, p BankAuthorisationCreateParams, opts ...RequestOption) (*BankAuthorisation, error) {
uri, err := url.Parse(fmt.Sprintf(s.endpoint + "/bank_authorisations"))
func (s *BankAuthorisationServiceImpl) Create(ctx context.Context, p BankAuthorisationCreateParams, opts ...RequestOption) (*BankAuthorisation, error) {
uri, err := url.Parse(fmt.Sprintf(s.config.Endpoint() + "/bank_authorisations"))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -168,10 +175,10 @@ func (s *BankAuthorisationService) Create(ctx context.Context, p BankAuthorisati
return nil, err
}
req.WithContext(ctx)
req.Header.Set("Authorization", "Bearer "+s.token)
req.Header.Set("Authorization", "Bearer "+s.config.Token())
req.Header.Set("GoCardless-Version", "2015-07-06")
req.Header.Set("GoCardless-Client-Library", "gocardless-pro-go")
req.Header.Set("GoCardless-Client-Version", "1.0.0")
req.Header.Set("GoCardless-Client-Version", "2.0.0")
req.Header.Set("User-Agent", userAgent)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Idempotency-Key", o.idempotencyKey)
Expand All @@ -180,7 +187,7 @@ func (s *BankAuthorisationService) Create(ctx context.Context, p BankAuthorisati
req.Header.Set(key, value)
}

client := s.client
client := s.config.Client()
if client == nil {
client = http.DefaultClient
}
Expand Down
20 changes: 11 additions & 9 deletions bank_details_lookup_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ var _ = json.NewDecoder
var _ = errors.New

// BankDetailsLookupService manages bank_details_lookups
type BankDetailsLookupService struct {
endpoint string
token string
client *http.Client
type BankDetailsLookupServiceImpl struct {
config Config
}

// BankDetailsLookup model
Expand All @@ -32,6 +30,10 @@ type BankDetailsLookup struct {
Bic string `url:"bic,omitempty" json:"bic,omitempty"`
}

type BankDetailsLookupService interface {
Create(ctx context.Context, p BankDetailsLookupCreateParams, opts ...RequestOption) (*BankDetailsLookup, error)
}

// BankDetailsLookupCreateParams parameters
type BankDetailsLookupCreateParams struct {
AccountNumber string `url:"account_number,omitempty" json:"account_number,omitempty"`
Expand Down Expand Up @@ -60,8 +62,8 @@ type BankDetailsLookupCreateParams struct {
// GoCardless for
// modulus or reachability checking but not for payment collection, please get
// in touch.
func (s *BankDetailsLookupService) Create(ctx context.Context, p BankDetailsLookupCreateParams, opts ...RequestOption) (*BankDetailsLookup, error) {
uri, err := url.Parse(fmt.Sprintf(s.endpoint + "/bank_details_lookups"))
func (s *BankDetailsLookupServiceImpl) Create(ctx context.Context, p BankDetailsLookupCreateParams, opts ...RequestOption) (*BankDetailsLookup, error) {
uri, err := url.Parse(fmt.Sprintf(s.config.Endpoint() + "/bank_details_lookups"))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -95,10 +97,10 @@ func (s *BankDetailsLookupService) Create(ctx context.Context, p BankDetailsLook
return nil, err
}
req.WithContext(ctx)
req.Header.Set("Authorization", "Bearer "+s.token)
req.Header.Set("Authorization", "Bearer "+s.config.Token())
req.Header.Set("GoCardless-Version", "2015-07-06")
req.Header.Set("GoCardless-Client-Library", "gocardless-pro-go")
req.Header.Set("GoCardless-Client-Version", "1.0.0")
req.Header.Set("GoCardless-Client-Version", "2.0.0")
req.Header.Set("User-Agent", userAgent)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Idempotency-Key", o.idempotencyKey)
Expand All @@ -107,7 +109,7 @@ func (s *BankDetailsLookupService) Create(ctx context.Context, p BankDetailsLook
req.Header.Set(key, value)
}

client := s.client
client := s.config.Client()
if client == nil {
client = http.DefaultClient
}
Expand Down
Loading

0 comments on commit 1ef31e7

Please sign in to comment.