client

package
v1.0.0-beta Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 25, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package client 提供 STX CLI 访问远端 API 的最小 HTTP 客户端。 Package client provides the minimal HTTP client used by the STX CLI.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIError

type APIError struct {
	StatusCode int
	ErrorCode  string
	Message    string
	RequestID  string
	Data       json.RawMessage
}

APIError 保存服务端错误的 HTTP 状态和请求编号。 APIError stores the HTTP status and request ID returned by the server.

func (*APIError) Error

func (e *APIError) Error() string

type CapabilityData

type CapabilityData struct {
	APIVersion       string                `json:"api_version"`
	ServerVersion    string                `json:"server_version"`
	MinCLIVersion    string                `json:"min_cli_version"`
	RegistryRevision string                `json:"registry_revision"`
	Operations       []CapabilityOperation `json:"operations"`
}

CapabilityData 是服务端能力查询结果。 CapabilityData is the server capability discovery result.

type CapabilityOperation

type CapabilityOperation struct {
	OperationID string         `json:"operation_id"`
	Revision    int            `json:"revision"`
	Allowed     bool           `json:"allowed"`
	DenialCode  string         `json:"denial_code,omitempty"`
	Mode        string         `json:"mode"`
	Risk        string         `json:"risk"`
	Impact      map[string]any `json:"impact,omitempty"`
}

CapabilityOperation 描述当前用户是否可调用一个服务端操作。 CapabilityOperation describes whether the current user may invoke a server operation.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client 是带有命名空间配置的远端 STX API 客户端。 Client is a remote STX API client backed by a resolved namespace configuration.

func New

func New(resolved cliConfig.Resolved, httpClient *http.Client) (*Client, error)

New 创建远端 STX API 客户端,并校验地址和超时。 New creates a remote STX API client and validates the URL and timeout.

func (*Client) CancelExecution

func (c *Client) CancelExecution(ctx context.Context, executionID, idempotencyKey string, confirmed bool) (string, ExecutionData, error)

CancelExecution 请求取消公共执行记录。 CancelExecution requests cancellation of a shared execution record.

func (*Client) Capabilities

func (c *Client) Capabilities(ctx context.Context) (string, CapabilityData, error)

Capabilities 查询当前服务端及用户可用的操作。 Capabilities queries operations available on the server for the current user.

func (*Client) Download added in v1.0.0

func (c *Client) Download(ctx context.Context, path string, destination io.Writer) (string, error)

Download streams a successful response body to the supplied writer. Download 将成功响应正文流式写入调用方提供的 writer。

func (*Client) GetExecution

func (c *Client) GetExecution(ctx context.Context, executionID string) (string, ExecutionData, error)

GetExecution 查询公共执行记录。 GetExecution retrieves a shared execution record.

func (*Client) Health

func (c *Client) Health(ctx context.Context) (string, map[string]any, error)

Health 查询 STX 服务健康状态。 Health queries the STX server health status.

func (*Client) Login

func (c *Client) Login(ctx context.Context, username, password, expiresIn string) (string, LoginData, error)

Login 调用 CLI 登录接口。 Login calls the CLI login endpoint.

func (*Client) Logout

func (c *Client) Logout(ctx context.Context) (string, error)

Logout 撤销当前令牌。 Logout revokes the current token.

func (*Client) Request

func (c *Client) Request(ctx context.Context, method, path string, requestBody any, result any) (string, error)

Request 向远端 STX API 发起 JSON 请求。 Request sends a JSON request to the remote STX API.

func (*Client) RequestMultipart added in v1.0.0

func (c *Client) RequestMultipart(ctx context.Context, method, path string, fields map[string]string, fileField, fileName string, file io.Reader, headers map[string]string, result any) (string, error)

RequestMultipart streams a multipart request without buffering the uploaded file in memory. RequestMultipart 以流式方式发送 multipart 请求,不把上传文件完整读入内存。

func (*Client) RequestMultipartFiles added in v1.0.0

func (c *Client) RequestMultipartFiles(ctx context.Context, method, path string, fields map[string]string, files []MultipartFile, headers map[string]string, result any) (string, error)

RequestMultipartFiles streams multiple file fields without buffering complete files in memory. RequestMultipartFiles 流式发送多个文件字段,不把完整文件读入内存。

func (*Client) RequestWithHeaders

func (c *Client) RequestWithHeaders(ctx context.Context, method, path string, requestBody any, headers map[string]string, result any) (string, error)

RequestWithHeaders 向远端 STX API 发起带安全执行请求头的 JSON 请求。 RequestWithHeaders sends a JSON request with safe-execution headers to the remote STX API.

func (*Client) Server

func (c *Client) Server() string

Server 返回客户端当前使用的服务地址,不包含令牌。 Server returns the server URL used by this client without exposing the token.

func (*Client) WaitExecution

func (c *Client) WaitExecution(ctx context.Context, executionID string, timeoutSeconds int) (string, ExecutionWaitData, error)

WaitExecution 在服务端等待一段时间并返回最新公共执行状态。 WaitExecution waits on the server for a bounded period and returns the latest shared execution state.

func (*Client) WhoAmI

func (c *Client) WhoAmI(ctx context.Context) (string, UserInfo, error)

WhoAmI 查询当前令牌对应的用户。 WhoAmI returns the user associated with the current token.

func (*Client) WithTimeout added in v1.0.0

func (c *Client) WithTimeout(timeout time.Duration) *Client

WithTimeout returns an isolated client copy for one long-running command. WithTimeout 返回仅供单个长耗时命令使用的独立客户端副本。

type ExecutionData

type ExecutionData struct {
	ExecutionID       string     `json:"execution_id"`
	OperationID       string     `json:"operation_id"`
	OwnerUserID       uint64     `json:"owner_user_id"`
	ActorType         string     `json:"actor_type"`
	Module            string     `json:"module"`
	ModuleRef         string     `json:"module_ref"`
	RequestID         string     `json:"request_id"`
	RiskLevel         string     `json:"risk_level"`
	Status            string     `json:"status"`
	Cancellable       bool       `json:"cancellable"`
	CancellableReason string     `json:"cancellable_reason,omitempty"`
	Progress          int        `json:"progress"`
	ResultRef         string     `json:"result_ref,omitempty"`
	ErrorCode         string     `json:"error_code,omitempty"`
	ErrorMessage      string     `json:"error_message,omitempty"`
	StartedAt         *time.Time `json:"started_at,omitempty"`
	FinishedAt        *time.Time `json:"finished_at,omitempty"`
	CreatedAt         time.Time  `json:"created_at"`
	UpdatedAt         time.Time  `json:"updated_at"`
}

ExecutionData 是 CLI 使用的公共执行记录。 ExecutionData is the shared execution record consumed by the CLI.

type ExecutionWaitData

type ExecutionWaitData struct {
	Execution ExecutionData `json:"execution"`
	TimedOut  bool          `json:"wait_timed_out"`
}

ExecutionWaitData 是公共等待接口的返回结果。 ExecutionWaitData is returned by the shared execution wait endpoint.

type LoginData

type LoginData struct {
	Token     string    `json:"token"`
	TokenType string    `json:"token_type"`
	ExpiresAt time.Time `json:"expires_at"`
	User      UserInfo  `json:"user"`
}

LoginData 是 CLI 登录接口返回的数据。 LoginData is returned by the CLI login endpoint.

type MultipartFile added in v1.0.0

type MultipartFile struct {
	FieldName string
	FileName  string
	Reader    io.Reader
}

MultipartFile describes one streamed file part in a multipart request. MultipartFile 描述 multipart 请求中的一个流式文件字段。

type Response

type Response struct {
	ErrorCode string          `json:"error_code"`
	ErrorMsg  string          `json:"error_msg"`
	Data      json.RawMessage `json:"data"`
}

Response 是 STX API 的通用响应封装。 Response is the generic STX API response envelope.

type UserInfo

type UserInfo struct {
	ID       uint64 `json:"id"`
	Username string `json:"username"`
	Nickname string `json:"nickname"`
	Email    string `json:"email"`
	Language string `json:"language"`
	IsActive bool   `json:"is_active"`
	IsAdmin  bool   `json:"is_admin"`
}

UserInfo 是 CLI 所需的用户信息子集。 UserInfo is the user information subset needed by the CLI.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL