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:
2025-12-29 11:00:13 -05:00
parent 5576cced09
commit eb6100b736
8 changed files with 140 additions and 64 deletions

View File

@@ -1,6 +1,7 @@
package contract
import (
"context"
"fmt"
"os"
@@ -16,45 +17,45 @@ func NewContractClient(c *client.Client) *ContractClient {
return &ContractClient{client: c}
}
func (cc *ContractClient) Create(req *models.SmartContractCreateRequest) (*models.SmartContract, error) {
func (cc *ContractClient) Create(ctx context.Context, req *models.SmartContractCreateRequest) (*models.SmartContract, error) {
var resp models.SmartContract
err := cc.client.Post("/api/v1/contract", models.ContentTypeJSON, req, &resp)
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(contractID string) (*models.SmartContract, error) {
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(path, &resp)
err := cc.client.Get(ctx, path, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
func (cc *ContractClient) List() (*models.ListResponse, error) {
func (cc *ContractClient) List(ctx context.Context) (*models.ListResponse, error) {
var resp models.ListResponse
err := cc.client.Get("/api/v1/contract", &resp)
err := cc.client.Get(ctx, "/api/v1/contract", &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
func (cc *ContractClient) Update(contractID string, req *models.SmartContractUpdateRequest) (*models.SuccessResponse, error) {
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(path, models.ContentTypeJSON, req, &resp)
err := cc.client.Put(ctx, path, models.ContentTypeJSON, req, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
func (cc *ContractClient) Upload(contractID string, filepath string) (*models.SuccessResponse, error) {
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)
@@ -62,17 +63,17 @@ func (cc *ContractClient) Upload(contractID string, filepath string) (*models.Su
var resp models.SuccessResponse
path := fmt.Sprintf("/api/v1/contract/%s/upload", contractID)
err = cc.client.Put(path, "application/octet-stream", fileContent, &resp)
err = cc.client.Put(ctx, path, "application/octet-stream", fileContent, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
func (cc *ContractClient) Delete(contractID string) (*models.SuccessResponse, error) {
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(path, &resp)
err := cc.client.Delete(ctx, path, &resp)
if err != nil {
return nil, err
}