Developer-friendly & type-safe Go SDK specifically catered to leverage github.com/formancehq/stack/ledger/client API.
It has been generated successfully based on your OpenAPI spec. However, it is not yet ready for production use. Here are some next steps:
- 🛠 Make your SDK feel handcrafted by customizing it
- ♻️ Refine your SDK quickly by iterating locally with the Speakeasy CLI
- 🎁 Publish your SDK to package managers by configuring automatic publishing
- ✨ When ready to productionize, delete this section from the README
- SDK Installation
- SDK Example Usage
- Available Resources and Operations
- Retries
- Error Handling
- Server Selection
- Custom HTTP Client
- Special Types
go get github.com/formancehq/ledger/pkg/client
package main
import (
"context"
"github.com/formancehq/ledger/pkg/client"
"github.com/formancehq/ledger/pkg/client/models/components"
"log"
)
func main() {
s := client.New(
client.WithSecurity(components.Security{
ClientID: "",
ClientSecret: "",
}),
)
ctx := context.Background()
res, err := s.Ledger.GetInfo(ctx)
if err != nil {
log.Fatal(err)
}
if res.V2ConfigInfoResponse != nil {
// handle response
}
}
- GetInfo - Show server information
- GetMetrics - Read in memory metrics
- GetInfo - Show server information
- GetLedgerInfo - Get information about a ledger
- CountAccounts - Count the accounts from a ledger
- ListAccounts - List accounts from a ledger
- GetAccount - Get account by its address
- AddMetadataToAccount - Add metadata to an account
- GetMapping - Get the mapping of a ledger
- UpdateMapping - Update the mapping of a ledger
RunScript- Execute a Numscript⚠️ Deprecated- ReadStats - Get statistics from a ledger
- CountTransactions - Count the transactions from a ledger
- ListTransactions - List transactions from a ledger
- CreateTransaction - Create a new transaction to a ledger
- GetTransaction - Get transaction from a ledger by its ID
- AddMetadataOnTransaction - Set the metadata of a transaction by its ID
- RevertTransaction - Revert a ledger transaction by its ID
- CreateTransactions - Create a new batch of transactions to a ledger
- GetBalances - Get the balances from a ledger's account
- GetBalancesAggregated - Get the aggregated balances from selected accounts
- ListLogs - List the logs from a ledger
- ListLedgers - List ledgers
- GetLedger - Get a ledger
- CreateLedger - Create a ledger
- UpdateLedgerMetadata - Update ledger metadata
- DeleteLedgerMetadata - Delete ledger metadata by key
- GetLedgerInfo - Get information about a ledger
- CreateBulk - Bulk request
- CountAccounts - Count the accounts from a ledger
- ListAccounts - List accounts from a ledger
- GetAccount - Get account by its address
- AddMetadataToAccount - Add metadata to an account
- DeleteAccountMetadata - Delete metadata by key
- ReadStats - Get statistics from a ledger
- CountTransactions - Count the transactions from a ledger
- ListTransactions - List transactions from a ledger
- CreateTransaction - Create a new transaction to a ledger
- GetTransaction - Get transaction from a ledger by its ID
- AddMetadataOnTransaction - Set the metadata of a transaction by its ID
- DeleteTransactionMetadata - Delete metadata by key
- RevertTransaction - Revert a ledger transaction by its ID
- GetBalancesAggregated - Get the aggregated balances from selected accounts
- GetVolumesWithBalances - Get list of volumes with balances for (account/asset)
- ListLogs - List the logs from a ledger
- ImportLogs
- ExportLogs - Export logs
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"
"github.com/formancehq/ledger/pkg/client"
"github.com/formancehq/ledger/pkg/client/models/components"
"github.com/formancehq/ledger/pkg/client/retry"
"log"
"models/operations"
)
func main() {
s := client.New(
client.WithSecurity(components.Security{
ClientID: "",
ClientSecret: "",
}),
)
ctx := context.Background()
res, err := s.Ledger.GetInfo(ctx, 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.V2ConfigInfoResponse != 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"
"github.com/formancehq/ledger/pkg/client"
"github.com/formancehq/ledger/pkg/client/models/components"
"github.com/formancehq/ledger/pkg/client/retry"
"log"
)
func main() {
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{
ClientID: "",
ClientSecret: "",
}),
)
ctx := context.Background()
res, err := s.Ledger.GetInfo(ctx)
if err != nil {
log.Fatal(err)
}
if res.V2ConfigInfoResponse != nil {
// handle response
}
}
Handling errors in this SDK should largely match your expectations. All operations return a response object or an error, they will never return both. When specified by the OpenAPI spec document, the SDK will return the appropriate subclass.
Error Object | Status Code | Content Type |
---|---|---|
sdkerrors.V2ErrorResponse | default | application/json |
sdkerrors.SDKError | 4xx-5xx | / |
package main
import (
"context"
"errors"
"github.com/formancehq/ledger/pkg/client"
"github.com/formancehq/ledger/pkg/client/models/components"
"github.com/formancehq/ledger/pkg/client/models/sdkerrors"
"log"
)
func main() {
s := client.New(
client.WithSecurity(components.Security{
ClientID: "",
ClientSecret: "",
}),
)
ctx := context.Background()
res, err := s.Ledger.GetInfo(ctx)
if err != nil {
var e *sdkerrors.V2ErrorResponse
if errors.As(err, &e) {
// handle error
log.Fatal(e.Error())
}
var e *sdkerrors.SDKError
if errors.As(err, &e) {
// handle error
log.Fatal(e.Error())
}
}
}
You can override the default server globally using the WithServerIndex
option when initializing the SDK client instance. The selected server will then be used as the default on the operations that use it. This table lists the indexes associated with the available servers:
# | Server | Variables |
---|---|---|
0 | http://localhost:8080/ |
None |
package main
import (
"context"
"github.com/formancehq/ledger/pkg/client"
"github.com/formancehq/ledger/pkg/client/models/components"
"log"
)
func main() {
s := client.New(
client.WithServerIndex(0),
client.WithSecurity(components.Security{
ClientID: "",
ClientSecret: "",
}),
)
ctx := context.Background()
res, err := s.Ledger.GetInfo(ctx)
if err != nil {
log.Fatal(err)
}
if res.V2ConfigInfoResponse != nil {
// handle response
}
}
The default server can also be overridden globally using the WithServerURL
option when initializing the SDK client instance. For example:
package main
import (
"context"
"github.com/formancehq/ledger/pkg/client"
"github.com/formancehq/ledger/pkg/client/models/components"
"log"
)
func main() {
s := client.New(
client.WithServerURL("http://localhost:8080/"),
client.WithSecurity(components.Security{
ClientID: "",
ClientSecret: "",
}),
)
ctx := context.Background()
res, err := s.Ledger.GetInfo(ctx)
if err != nil {
log.Fatal(err)
}
if res.V2ConfigInfoResponse != nil {
// handle response
}
}
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/myorg/your-go-sdk"
)
var (
httpClient = &http.Client{Timeout: 30 * time.Second}
sdkClient = sdk.New(sdk.WithClient(httpClient))
)
This can be a convenient way to configure timeouts, cookies, proxies, custom headers, and other low-level configuration.
This SDK supports the following security schemes globally:
Name | Type | Scheme |
---|---|---|
ClientID |
oauth2 | OAuth2 token |
ClientSecret |
oauth2 | OAuth2 token |
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"
"github.com/formancehq/ledger/pkg/client"
"github.com/formancehq/ledger/pkg/client/models/components"
"log"
)
func main() {
s := client.New(
client.WithSecurity(components.Security{
ClientID: "",
ClientSecret: "",
}),
)
ctx := context.Background()
res, err := s.Ledger.GetInfo(ctx)
if err != nil {
log.Fatal(err)
}
if res.V2ConfigInfoResponse != nil {
// handle response
}
}
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.
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.