Skip to content

Commit

Permalink
copy over files
Browse files Browse the repository at this point in the history
  • Loading branch information
mikekono committed Nov 6, 2024
1 parent f7bd7e8 commit 64b5cfd
Show file tree
Hide file tree
Showing 114 changed files with 25,318 additions and 41,720 deletions.
4 changes: 4 additions & 0 deletions .fernignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# Specify files that shouldn't be modified by Fern

README.md
webhooks/client/verify_signature.go
verify_signature.go
314 changes: 314 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,314 @@
# Square Go Library

[![fern shield](https://img.shields.io/badge/%F0%9F%8C%BF-SDK%20generated%20by%20Fern-brightgreen)](https://github.com/fern-api/fern)
[![go shield](https://img.shields.io/badge/go-docs-blue)](https://pkg.go.dev/github.com/fern-demo/square-go-sdk)

The Square Go library provides convenient access to the Square API from Go.

## Requirements

This module requires Go version >= 1.18.

# Installation

Run the following command to use the Square Go library in your module:

```sh
go get github.com/fern-demo/square-go-sdk
```

## Usage

```go
import (
"github.com/fern-demo/square-go-sdk"
squareclient "github.com/fern-demo/square-go-sdk/client"
"github.com/fern-demo/square-go-sdk/option"
)

client := squareclient.NewClient(
option.WithToken("<YOUR_API_KEY>"),
)

response, err := client.Cards.Create(
context.TODO(),
&square.CreateCardRequest{
IdempotencyKey: "4935a656-a929-4792-b97c-8848be85c27c",
SourceID: "cnon:uIbfJXhXETSP197M3GB",
Card: &square.Card{
CardholderName: square.String("Amelia Earhart"),
BillingAddress: &square.Address{
AddressLine1: square.String("500 Electric Ave"),
AddressLine2: square.String("Suite 600"),
Locality: square.String("New York"),
AdministrativeDistrictLevel1: square.String("NY"),
PostalCode: square.String("10003"),
Country: square.CountryUs.Ptr(),
},
CustomerID: square.String("VDKXEEKPJN48QDG3BGGFAK05P8"),
ReferenceID: square.String("user-id-1"),
},
},
)
```

## Optional Parameters

This library models optional primitives and enum types as pointers. This is primarily meant to distinguish
default zero values from explicit values (e.g. `false` for `bool` and `""` for `string`). A collection of
helper functions are provided to easily map a primitive or enum to its pointer-equivalent (e.g. `square.String`).

For example, consider the `client.Cards.List` endpoint usage below:

```go
response, err := client.Cards.List(
context.TODO(),
&square.CardsListRequest{
IncludeDisabled: square.Bool(true),
SortOrder: square.SortOrderAsc.Ptr(),
},
)
```

## Automatic Pagination

List endpoints are paginated. The SDK provides an iterator so that you can simply loop over the items:

```go
page, err := client.Cards.List(
context.TODO(),
&square.CardsListRequest{
IncludeDisabled: square.Bool(true),
SortOrder: square.SortOrderAsc.Ptr(),
},
)
if err != nil {
return nil, err
}
iter := page.Iterator()
for iter.Next() {
card := iter.Current()
fmt.Printf("Got card: %v\n", *card.ID)
}
if err := iter.Err(); err != nil {
// Handle the error!
}
```

You can also iterate page-by-page:

```go
for page != nil {
for _, card := range page.Results {
fmt.Printf("Got card: %v\n", *card.ID)
}
page, err = page.GetNextPage()
if errors.Is(err, core.ErrNoPages) {
break
}
if err != nil {
// Handle the error!
}
}
```

## Timeouts

Setting a timeout for each individual request is as simple as using the standard
`context` library. Setting a one second timeout for an individual API call looks
like the following:

```go
ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
defer cancel()

response, err := client.Cards.List(
ctx,
&square.CardsListRequest{
IncludeDisabled: square.Bool(true),
SortOrder: square.SortOrderAsc.Ptr(),
},
)
```

## Errors

Structured error types are returned from API calls that return non-success status codes. For example,
you can check if the error was due to an unauthorized request (i.e. status code 401) with the following:

```go
response, err := client.Cards.Create(...)
if err != nil {
if apiError, ok := err.(*core.APIError); ok {
switch (apiError.StatusCode) {
case http.StatusUnauthorized:
// Do something with the unauthorized request ...
}
}
return err
}
```

These errors are also compatible with the `errors.Is` and `errors.As` APIs, so you can access the error
like so:

```go
response, err := client.Cards.Create(...)
if err != nil {
var apiError *core.APIError
if errors.As(err, apiError) {
// Do something with the API error ...
}
return err
}
```

If you'd like to wrap the errors with additional information and still retain the ability
to access the type with `errors.Is` and `errors.As`, you can use the `%w` directive:

```go
response, err := client.Cards.Create(...)
if err != nil {
return fmt.Errorf("failed to create card: %w", err)
}
```

## Webhook Signature Verification

The SDK provides a utility method that allow you to verify webhook signatures and ensure that all webhook events
originate from Square. The `client.Webhooks.VerifySignature` method will verify the signature:

```go
err := client.Webhooks.VerifySignature(
context.TODO(),
&square.VerifySignatureRequest{
RequestBody: requestBody,
SignatureHeader: header.Get("x-square-hmacsha256-signature"),
SignatureKey: "YOUR_SIGNATURE_KEY",
NotificationURL: "https://example.com/webhook", // The URL where event notifications are sent.
},
);
if err != nil {
return nil, err
}
```

## Advanced

### Request Options

A variety of request options are included to adapt the behavior of the library, which includes
configuring authorization tokens, or providing your own instrumented `*http.Client`. Both of
these options are shown below:

```go
client := squareclient.NewClient(
option.WithToken("<YOUR_API_KEY>"),
option.WithHTTPClient(
&http.Client{
Timeout: 5 * time.Second,
},
),
)
```

These request options can either be specified on the client so that they're applied on _every_
request (shown above), or for an individual request like so:

```go
response, err := client.Cards.List(
ctx,
&square.CardsListRequest{
IncludeDisabled: square.Bool(true),
SortOrder: square.SortOrderAsc.Ptr(),
},
option.WithToken("<YOUR_API_KEY>"),
)
```

> Providing your own `*http.Client` is recommended. Otherwise, the `http.DefaultClient` will be used,
> and your client will wait indefinitely for a response (unless the per-request, context-based timeout
> is used).
### Send Extra Properties

All endpoints support sending additional request body properties and query parameters that are not already
supported by the SDK. This is useful whenever you need to interact with an unreleased or hidden feature.

For example, suppose that a new feature was rolled out that allowed users to list all deactivated team members.
You could the relevant query parameters like so:

```go
response, err := client.TeamMembers.Search(
context.TODO(),
&square.SearchTeamMembersRequest{
Limit: square.Int(100),
},
option.WithQueryParameters(
url.Values{
"status": []string{"DEACTIVATED"},
},
),
)
```

### Receive Extra Properties

Every response type includes the `GetExtraProperties` method, which returns a map that contains any properties in the JSON
response that were not specified in the struct. Similar to the use case for sending additional parameters, this can be useful for
API features not present in the SDK yet.

You can receive and interact with the extra properties like so:

```go
response, err := client.Cards.Create(...)
if err != nil {
return nil, err
}
extraProperties := response.GetExtraProperties()
```

### Retries

The Square Go client is instrumented with automatic retries with exponential backoff. A request will be
retried as long as the request is deemed retriable and the number of retry attempts has not grown larger
than the configured retry limit (default: 2).

A request is deemed retriable when any of the following HTTP status codes is returned:

- [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (Timeout)
- [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (Too Many Requests)
- [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) (Internal Server Errors)

You can use the `option.WithMaxAttempts` option to configure the maximum retry limit to
your liking. For example, if you want to disable retries for the client entirely, you can
set this value to 1 like so:

```go
client := squareclient.NewClient(
option.WithMaxAttempts(1),
)
```

This can be done for an individual request, too:

```go
response, err := client.Cards.List(
ctx,
&square.CardsListRequest{
IncludeDisabled: square.Bool(true),
SortOrder: square.SortOrderAsc.Ptr(),
},
option.WithMaxAttempts(1),
)
```

## Contributing

While we value open-source contributions to this SDK, this library is generated programmatically.
Additions made directly to this library would have to be moved over to our generation code,
otherwise they would be overwritten upon the next generated release. Feel free to open a PR as
a proof of concept, but know that we will not be able to merge it as-is. We suggest opening
an issue first to discuss with us!

On the other hand, contributions to the `README.md` are always very welcome!
7 changes: 4 additions & 3 deletions applepay/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ package applepay

import (
context "context"
squaregosdk "github.com/square/square-go-sdk"
core "github.com/square/square-go-sdk/core"
option "github.com/square/square-go-sdk/option"
http "net/http"
os "os"

squaregosdk "github.com/fern-demo/square-go-sdk"
core "github.com/fern-demo/square-go-sdk/core"
option "github.com/fern-demo/square-go-sdk/option"
)

type Client struct {
Expand Down
13 changes: 7 additions & 6 deletions bankaccounts/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ package bankaccounts
import (
context "context"
fmt "fmt"
squaregosdk "github.com/square/square-go-sdk"
core "github.com/square/square-go-sdk/core"
option "github.com/square/square-go-sdk/option"
http "net/http"
os "os"

squaregosdk "github.com/fern-demo/square-go-sdk"
core "github.com/fern-demo/square-go-sdk/core"
option "github.com/fern-demo/square-go-sdk/option"
)

type Client struct {
Expand Down Expand Up @@ -38,7 +39,7 @@ func NewClient(opts ...option.RequestOption) *Client {
}
}

// Returns a list of [BankAccount](entity:BankAccount) objects linked to a Square account.
// Returns a list of [BankAccount]($m/BankAccount) objects linked to a Square account.
func (c *Client) List(
ctx context.Context,
request *squaregosdk.BankAccountsListRequest,
Expand Down Expand Up @@ -97,7 +98,7 @@ func (c *Client) List(
return pager.GetPage(ctx, request.Cursor)
}

// Returns details of a [BankAccount](entity:BankAccount) identified by V1 bank account ID.
// Returns details of a [BankAccount]($m/BankAccount) identified by V1 bank account ID.
func (c *Client) GetByV1ID(
ctx context.Context,
// Connect V1 ID of the desired `BankAccount`. For more information, see
Expand Down Expand Up @@ -137,7 +138,7 @@ func (c *Client) GetByV1ID(
return response, nil
}

// Returns details of a [BankAccount](entity:BankAccount)
// Returns details of a [BankAccount]($m/BankAccount)
// linked to a Square account.
func (c *Client) Get(
ctx context.Context,
Expand Down
7 changes: 4 additions & 3 deletions bookingcustomattributes/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ package bookingcustomattributes

import (
context "context"
squaregosdk "github.com/square/square-go-sdk"
core "github.com/square/square-go-sdk/core"
option "github.com/square/square-go-sdk/option"
http "net/http"
os "os"

squaregosdk "github.com/fern-demo/square-go-sdk"
core "github.com/fern-demo/square-go-sdk/core"
option "github.com/fern-demo/square-go-sdk/option"
)

type Client struct {
Expand Down
Loading

0 comments on commit 64b5cfd

Please sign in to comment.