Add context.Context parameter to all API methods
Enable request timeout and cancellation control by adding context.Context as the first parameter to all SDK API methods. This allows users to: - Set per-request timeouts - Cancel in-flight requests - Pass request-scoped values
This commit is contained in:
@@ -1,3 +1,49 @@
|
||||
// Package sdk provides a Go SDK for interacting with Dragonchain nodes.
|
||||
//
|
||||
// All API methods require a context.Context parameter for timeout and cancellation control.
|
||||
//
|
||||
// Example usage:
|
||||
//
|
||||
// package main
|
||||
//
|
||||
// import (
|
||||
// "context"
|
||||
// "fmt"
|
||||
// "log"
|
||||
// "time"
|
||||
//
|
||||
// "git.dragonchain.com/dragonchain/dragonchain-prime-sdk-go"
|
||||
// "git.dragonchain.com/dragonchain/dragonchain-prime-sdk-go/models"
|
||||
// )
|
||||
//
|
||||
// func main() {
|
||||
// client := sdk.NewDragonchainSDK(
|
||||
// "your-public-id",
|
||||
// "your-auth-key-id",
|
||||
// "your-auth-key",
|
||||
// "https://your-dragonchain-endpoint.com",
|
||||
// )
|
||||
//
|
||||
// ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
// defer cancel()
|
||||
//
|
||||
// // Check system health
|
||||
// if err := client.System.Health(ctx); err != nil {
|
||||
// log.Fatal(err)
|
||||
// }
|
||||
//
|
||||
// // Create a transaction
|
||||
// txn := &models.TransactionCreateRequest{
|
||||
// TxnType: "my-transaction-type",
|
||||
// Payload: map[string]interface{}{"message": "Hello Dragonchain"},
|
||||
// }
|
||||
//
|
||||
// resp, err := client.Transaction.Create(ctx, txn)
|
||||
// if err != nil {
|
||||
// log.Fatal(err)
|
||||
// }
|
||||
// fmt.Printf("Created transaction: %s\n", resp.TransactionID)
|
||||
// }
|
||||
package sdk
|
||||
|
||||
import (
|
||||
@@ -9,6 +55,8 @@ import (
|
||||
"git.dragonchain.com/dragonchain/dragonchain-prime-sdk-go/transactiontype"
|
||||
)
|
||||
|
||||
// DragonchainSDK is the main SDK client for interacting with Dragonchain nodes.
|
||||
// It provides access to all API endpoints through specialized client instances.
|
||||
type DragonchainSDK struct {
|
||||
client *client.Client
|
||||
Transaction *transaction.TransactionClient
|
||||
@@ -18,6 +66,16 @@ type DragonchainSDK struct {
|
||||
System *system.SystemClient
|
||||
}
|
||||
|
||||
// NewDragonchainSDK creates a new Dragonchain SDK client.
|
||||
//
|
||||
// Parameters:
|
||||
// - publicID: Your Dragonchain public ID
|
||||
// - authKeyID: Your authentication key ID
|
||||
// - authKey: Your authentication key
|
||||
// - baseURL: The base URL of your Dragonchain node (e.g., "https://mychain.dragonchain.com")
|
||||
//
|
||||
// Returns a configured SDK client ready to make API calls.
|
||||
// All API methods on the returned client require a context.Context parameter.
|
||||
func NewDragonchainSDK(publicID, authKeyID, authKey, baseURL string) *DragonchainSDK {
|
||||
c := client.NewClient(publicID, authKeyID, authKey, baseURL)
|
||||
return &DragonchainSDK{
|
||||
@@ -30,6 +88,8 @@ func NewDragonchainSDK(publicID, authKeyID, authKey, baseURL string) *Dragonchai
|
||||
}
|
||||
}
|
||||
|
||||
// GetClient returns the underlying HTTP client used by the SDK.
|
||||
// This can be useful for advanced use cases or custom implementations.
|
||||
func (sdk *DragonchainSDK) GetClient() *client.Client {
|
||||
return sdk.client
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user