Files
dragonchain-prime-sdk-go/contract/contract.go
Andrew Miller eb6100b736 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
2025-12-29 11:00:13 -05:00

82 lines
2.3 KiB
Go

package contract
import (
"context"
"fmt"
"os"
"git.dragonchain.com/dragonchain/dragonchain-prime-sdk-go/client"
"git.dragonchain.com/dragonchain/dragonchain-prime-sdk-go/models"
)
type ContractClient struct {
client *client.Client
}
func NewContractClient(c *client.Client) *ContractClient {
return &ContractClient{client: c}
}
func (cc *ContractClient) Create(ctx context.Context, req *models.SmartContractCreateRequest) (*models.SmartContract, error) {
var resp models.SmartContract
err := cc.client.Post(ctx, "/api/v1/contract", models.ContentTypeJSON, req, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
func (cc *ContractClient) Get(ctx context.Context, contractID string) (*models.SmartContract, error) {
var resp models.SmartContract
path := fmt.Sprintf("/api/v1/contract/%s", contractID)
err := cc.client.Get(ctx, path, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
func (cc *ContractClient) List(ctx context.Context) (*models.ListResponse, error) {
var resp models.ListResponse
err := cc.client.Get(ctx, "/api/v1/contract", &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
func (cc *ContractClient) Update(ctx context.Context, contractID string, req *models.SmartContractUpdateRequest) (*models.SuccessResponse, error) {
var resp models.SuccessResponse
path := fmt.Sprintf("/api/v1/contract/%s", contractID)
err := cc.client.Put(ctx, path, models.ContentTypeJSON, req, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
func (cc *ContractClient) Upload(ctx context.Context, contractID string, filepath string) (*models.SuccessResponse, error) {
fileContent, err := os.ReadFile(filepath)
if err != nil {
return nil, fmt.Errorf("failed to read file: %w", err)
}
var resp models.SuccessResponse
path := fmt.Sprintf("/api/v1/contract/%s/upload", contractID)
err = cc.client.Put(ctx, path, "application/octet-stream", fileContent, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
func (cc *ContractClient) Delete(ctx context.Context, contractID string) (*models.SuccessResponse, error) {
var resp models.SuccessResponse
path := fmt.Sprintf("/api/v1/contract/%s", contractID)
err := cc.client.Delete(ctx, path, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}