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
96 lines
3.2 KiB
Go
96 lines
3.2 KiB
Go
// 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 (
|
|
"git.dragonchain.com/dragonchain/dragonchain-prime-sdk-go/block"
|
|
"git.dragonchain.com/dragonchain/dragonchain-prime-sdk-go/client"
|
|
"git.dragonchain.com/dragonchain/dragonchain-prime-sdk-go/contract"
|
|
"git.dragonchain.com/dragonchain/dragonchain-prime-sdk-go/system"
|
|
"git.dragonchain.com/dragonchain/dragonchain-prime-sdk-go/transaction"
|
|
"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
|
|
TransactionType *transactiontype.TransactionTypeClient
|
|
Contract *contract.ContractClient
|
|
Block *block.BlockClient
|
|
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{
|
|
client: c,
|
|
Transaction: transaction.NewTransactionClient(c),
|
|
TransactionType: transactiontype.NewTransactionTypeClient(c),
|
|
Contract: contract.NewContractClient(c),
|
|
Block: block.NewBlockClient(c),
|
|
System: system.NewSystemClient(c),
|
|
}
|
|
}
|
|
|
|
// 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
|
|
}
|