Skip to content

Commit

Permalink
feat(api): update via SDK Studio (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stainless Bot committed Oct 8, 2024
1 parent f3d944b commit 11a3e7b
Show file tree
Hide file tree
Showing 29 changed files with 110 additions and 51 deletions.
36 changes: 26 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Maestro Bitcoin Go SDK
# Maestro Bitcoin Go SDK API Library

<a href="https://pkg.go.dev/github.com/maestro-org/maestro-bitcoin-go-sdk"><img src="https://pkg.go.dev/badge/github.com/maestro-org/maestro-bitcoin-go-sdk.svg" alt="Go Reference"></a>

The Maestro Bitcoin Go SDK provides convenient access to [the Maestro Bitcoin REST
The Maestro Bitcoin Go SDK library provides convenient access to [the Maestro REST

API](https://docs.gomaestro.org/) from applications written in Go. The full API of this library can be found in [api.md](api.md).

## Installation
Expand Down Expand Up @@ -49,13 +50,17 @@ import (
func main() {
client := maestrobitcoingosdk.NewClient(
option.WithAPIKey("My API Key"), // defaults to os.LookupEnv("API_KEY")
option.WithEnvironmentMainnet(), // defaults to option.WithEnvironmentMainnet()
option.WithEnvironmentMainnet(), // or option.WithEnvironmentTestnet() | option.WithEnvironmentDefault(); defaults to option.WithEnvironmentTestnet()
)
paginatedUtxo, err := client.Addresses.Utxos.List(
context.TODO(),
"REPLACE_ME",
maestrobitcoingosdk.AddressUtxoListParams{},
)
timestampedBlock, err := client.Blocks.Latest.Get(context.TODO())
if err != nil {
panic(err.Error())
}
fmt.Printf("%+v\n", timestampedBlock.Data)
fmt.Printf("%+v\n", paginatedUtxo.Data)
}

```
Expand Down Expand Up @@ -144,7 +149,7 @@ client := maestrobitcoingosdk.NewClient(
option.WithHeader("X-Some-Header", "custom_header_info"),
)

client.Blocks.Latest.Get(context.TODO(), ...,
client.Addresses.Utxos.List(context.TODO(), ...,
// Override the header
option.WithHeader("X-Some-Header", "some_other_custom_header_info"),
// Add an undocumented field to the request body, using sjson syntax
Expand Down Expand Up @@ -173,14 +178,18 @@ When the API returns a non-success status code, we return an error with type
To handle errors, we recommend that you use the `errors.As` pattern:

```go
_, err := client.Blocks.Latest.Get(context.TODO())
_, err := client.Addresses.Utxos.List(
context.TODO(),
"REPLACE_ME",
maestrobitcoingosdk.AddressUtxoListParams{},
)
if err != nil {
var apierr *maestrobitcoingosdk.Error
if errors.As(err, &apierr) {
println(string(apierr.DumpRequest(true))) // Prints the serialized HTTP request
println(string(apierr.DumpResponse(true))) // Prints the serialized HTTP response
}
panic(err.Error()) // GET "/blocks/latest": 400 Bad Request { ... }
panic(err.Error()) // GET "/addresses/{address}/utxos": 400 Bad Request { ... }
}
```

Expand All @@ -198,8 +207,10 @@ To set a per-retry timeout, use `option.WithRequestTimeout()`.
// This sets the timeout for the request, including all the retries.
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
client.Blocks.Latest.Get(
client.Addresses.Utxos.List(
ctx,
"REPLACE_ME",
maestrobitcoingosdk.AddressUtxoListParams{},
// This sets the per-retry timeout
option.WithRequestTimeout(20*time.Second),
)
Expand Down Expand Up @@ -233,7 +244,12 @@ client := maestrobitcoingosdk.NewClient(
)

// Override per-request:
client.Blocks.Latest.Get(context.TODO(), option.WithMaxRetries(5))
client.Addresses.Utxos.List(
context.TODO(),
"REPLACE_ME",
maestrobitcoingosdk.AddressUtxoListParams{},
option.WithMaxRetries(5),
)
```

### Making custom/undocumented requests
Expand Down
2 changes: 1 addition & 1 deletion address.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// AddressService contains methods and other services that help with interacting
// with the maestro API.
// with the Maestro API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
Expand Down
2 changes: 1 addition & 1 deletion addressbrc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

// AddressBrc20Service contains methods and other services that help with
// interacting with the maestro API.
// interacting with the Maestro API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
Expand Down
2 changes: 1 addition & 1 deletion addressrune.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

// AddressRuneService contains methods and other services that help with
// interacting with the maestro API.
// interacting with the Maestro API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
Expand Down
2 changes: 1 addition & 1 deletion addresstx.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

// AddressTxService contains methods and other services that help with interacting
// with the maestro API.
// with the Maestro API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
Expand Down
2 changes: 1 addition & 1 deletion addressutxo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

// AddressUtxoService contains methods and other services that help with
// interacting with the maestro API.
// interacting with the Maestro API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
Expand Down
2 changes: 1 addition & 1 deletion asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// AssetService contains methods and other services that help with interacting with
// the maestro API.
// the Maestro API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
Expand Down
2 changes: 1 addition & 1 deletion assetbrc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

// AssetBrc20Service contains methods and other services that help with interacting
// with the maestro API.
// with the Maestro API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
Expand Down
2 changes: 1 addition & 1 deletion assetbrc20holder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

// AssetBrc20HolderService contains methods and other services that help with
// interacting with the maestro API.
// interacting with the Maestro API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
Expand Down
2 changes: 1 addition & 1 deletion assetrune.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

// AssetRuneService contains methods and other services that help with interacting
// with the maestro API.
// with the Maestro API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
Expand Down
2 changes: 1 addition & 1 deletion assetruneholder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

// AssetRuneHolderService contains methods and other services that help with
// interacting with the maestro API.
// interacting with the Maestro API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
Expand Down
2 changes: 1 addition & 1 deletion assetruneutxo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

// AssetRuneUtxoService contains methods and other services that help with
// interacting with the maestro API.
// interacting with the Maestro API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
Expand Down
2 changes: 1 addition & 1 deletion block.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

// BlockService contains methods and other services that help with interacting with
// the maestro API.
// the Maestro API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
Expand Down
2 changes: 1 addition & 1 deletion blocklatest.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

// BlockLatestService contains methods and other services that help with
// interacting with the maestro API.
// interacting with the Maestro API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
Expand Down
4 changes: 2 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

// Client creates a struct with services and top level methods that help with
// interacting with the maestro API. You should not instantiate this client
// interacting with the Maestro API. You should not instantiate this client
// directly, and instead use the [NewClient] method instead.
type Client struct {
Options []option.RequestOption
Expand All @@ -30,7 +30,7 @@ type Client struct {
// default arguments, and all option will be passed down to the services and
// requests that this client makes.
func NewClient(opts ...option.RequestOption) (r *Client) {
defaults := []option.RequestOption{option.WithEnvironmentMainnet()}
defaults := []option.RequestOption{option.WithEnvironmentTestnet()}
if o, ok := os.LookupEnv("API_KEY"); ok {
defaults = append(defaults, option.WithAPIKey(o))
}
Expand Down
48 changes: 40 additions & 8 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ func TestUserAgentHeader(t *testing.T) {
},
}),
)
client.Blocks.Latest.Get(context.Background())
client.Addresses.Utxos.List(
context.Background(),
"REPLACE_ME",
maestrobitcoingosdk.AddressUtxoListParams{},
)
if userAgent != fmt.Sprintf("Maestro/Go %s", internal.PackageVersion) {
t.Errorf("Expected User-Agent to be correct, but got: %#v", userAgent)
}
Expand All @@ -60,7 +64,11 @@ func TestRetryAfter(t *testing.T) {
},
}),
)
res, err := client.Blocks.Latest.Get(context.Background())
res, err := client.Addresses.Utxos.List(
context.Background(),
"REPLACE_ME",
maestrobitcoingosdk.AddressUtxoListParams{},
)
if err == nil || res != nil {
t.Error("Expected there to be a cancel error and for the response to be nil")
}
Expand Down Expand Up @@ -94,7 +102,11 @@ func TestDeleteRetryCountHeader(t *testing.T) {
}),
option.WithHeaderDel("X-Stainless-Retry-Count"),
)
res, err := client.Blocks.Latest.Get(context.Background())
res, err := client.Addresses.Utxos.List(
context.Background(),
"REPLACE_ME",
maestrobitcoingosdk.AddressUtxoListParams{},
)
if err == nil || res != nil {
t.Error("Expected there to be a cancel error and for the response to be nil")
}
Expand Down Expand Up @@ -123,7 +135,11 @@ func TestOverwriteRetryCountHeader(t *testing.T) {
}),
option.WithHeader("X-Stainless-Retry-Count", "42"),
)
res, err := client.Blocks.Latest.Get(context.Background())
res, err := client.Addresses.Utxos.List(
context.Background(),
"REPLACE_ME",
maestrobitcoingosdk.AddressUtxoListParams{},
)
if err == nil || res != nil {
t.Error("Expected there to be a cancel error and for the response to be nil")
}
Expand Down Expand Up @@ -151,7 +167,11 @@ func TestRetryAfterMs(t *testing.T) {
},
}),
)
res, err := client.Blocks.Latest.Get(context.Background())
res, err := client.Addresses.Utxos.List(
context.Background(),
"REPLACE_ME",
maestrobitcoingosdk.AddressUtxoListParams{},
)
if err == nil || res != nil {
t.Error("Expected there to be a cancel error and for the response to be nil")
}
Expand All @@ -173,7 +193,11 @@ func TestContextCancel(t *testing.T) {
)
cancelCtx, cancel := context.WithCancel(context.Background())
cancel()
res, err := client.Blocks.Latest.Get(cancelCtx)
res, err := client.Addresses.Utxos.List(
cancelCtx,
"REPLACE_ME",
maestrobitcoingosdk.AddressUtxoListParams{},
)
if err == nil || res != nil {
t.Error("Expected there to be a cancel error and for the response to be nil")
}
Expand All @@ -192,7 +216,11 @@ func TestContextCancelDelay(t *testing.T) {
)
cancelCtx, cancel := context.WithTimeout(context.Background(), 2*time.Millisecond)
defer cancel()
res, err := client.Blocks.Latest.Get(cancelCtx)
res, err := client.Addresses.Utxos.List(
cancelCtx,
"REPLACE_ME",
maestrobitcoingosdk.AddressUtxoListParams{},
)
if err == nil || res != nil {
t.Error("expected there to be a cancel error and for the response to be nil")
}
Expand All @@ -217,7 +245,11 @@ func TestContextDeadline(t *testing.T) {
},
}),
)
res, err := client.Blocks.Latest.Get(deadlineCtx)
res, err := client.Addresses.Utxos.List(
deadlineCtx,
"REPLACE_ME",
maestrobitcoingosdk.AddressUtxoListParams{},
)
if err == nil || res != nil {
t.Error("expected there to be a deadline error and for the response to be nil")
}
Expand Down
2 changes: 1 addition & 1 deletion general.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// GeneralService contains methods and other services that help with interacting
// with the maestro API.
// with the Maestro API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
Expand Down
2 changes: 1 addition & 1 deletion generalinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

// GeneralInfoService contains methods and other services that help with
// interacting with the maestro API.
// interacting with the Maestro API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
Expand Down
17 changes: 12 additions & 5 deletions option/requestoption.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/tidwall/sjson"
)

// RequestOption is an option for the requests made by the maestro API Client
// RequestOption is an option for the requests made by the Maestro API Client
// which can be supplied to clients, services, and methods. You can read more about this functional
// options pattern in our [README].
//
Expand Down Expand Up @@ -221,18 +221,25 @@ func WithRequestTimeout(dur time.Duration) RequestOption {
}
}

// WithEnvironmentTestnet returns a RequestOption that sets the current
// environment to be the "testnet" environment. An environment specifies which base URL
// to use by default.
func WithEnvironmentTestnet() RequestOption {
return WithBaseURL("https://xbt-testnet.gomaestro-api.org/v0/")
}

// WithEnvironmentMainnet returns a RequestOption that sets the current
// environment to be the "mainnet" environment. An environment specifies which base URL
// to use by default.
func WithEnvironmentMainnet() RequestOption {
return WithBaseURL("https://xbt-mainnet.gomaestro-api.org/v0/")
}

// WithEnvironmentTestnet returns a RequestOption that sets the current
// environment to be the "testnet" environment. An environment specifies which base URL
// WithEnvironmentDefault returns a RequestOption that sets the current
// environment to be the "default" environment. An environment specifies which base URL
// to use by default.
func WithEnvironmentTestnet() RequestOption {
return WithBaseURL("https://xbt-testnet.gomaestro-api.org/v0/")
func WithEnvironmentDefault() RequestOption {
return WithBaseURL("mainnet/")
}

// WithAPIKey returns a RequestOption that sets the client setting "api_key".
Expand Down
2 changes: 1 addition & 1 deletion rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// RpcService contains methods and other services that help with interacting with
// the maestro API.
// the Maestro API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
Expand Down
Loading

0 comments on commit 11a3e7b

Please sign in to comment.