httpclient

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ValidateResponse

func ValidateResponse(actual, expected interface{}) error

ValidateResponse validates the response against expected values Returns nil if validation passes, error otherwise

func ValidateResponseEnhanced added in v0.7.0

func ValidateResponseEnhanced(actual, expected interface{}) error

ValidateResponseEnhanced provides enhanced validation with support for: - Range validation (min/max) - Pattern matching (regex) - Type checking - Approximate float comparison - Array validation - Object validation

Example YAML usage:

expected:
  # Simple value (backward compatible)
  status: "ok"

  # Range validation
  count:
    min: 1
    max: 10

  # Pattern validation
  user_id:
    pattern: "^user_\\d+$"

  # Type validation
  timestamp:
    type: "number"

  # Approximate float
  rate:
    approx: 0.75
    tolerance: 0.01

Types

type Client

type Client struct {
	BaseURL    string
	HTTPClient *http.Client
	RequestID  int
}

Client is an HTTP client that supports both JSON-RPC 2.0 and REST API

func NewClient

func NewClient(baseURL string, timeout time.Duration) *Client

NewClient creates a new HTTP client

func (*Client) Call

func (c *Client) Call(ctx context.Context, method string, params interface{}) (*Response, error)

Call makes a JSON-RPC 2.0 call

func (*Client) CallHTTP added in v0.7.1

func (c *Client) CallHTTP(ctx context.Context, method, path string, body interface{}, headers map[string]string) (*HTTPResponse, error)

CallHTTP makes a simple HTTP request (non-JSON-RPC) Supports GET, POST, PUT, DELETE methods

func (*Client) CallHTTPDelete added in v0.7.1

func (c *Client) CallHTTPDelete(ctx context.Context, path string, headers map[string]string) (*HTTPResponse, error)

CallHTTPDelete makes a simple HTTP DELETE request

func (*Client) CallHTTPGet added in v0.7.1

func (c *Client) CallHTTPGet(ctx context.Context, path string, headers map[string]string) (*HTTPResponse, error)

CallHTTPGet makes a simple HTTP GET request

func (*Client) CallHTTPPost added in v0.7.1

func (c *Client) CallHTTPPost(ctx context.Context, path string, body interface{}, headers map[string]string) (*HTTPResponse, error)

CallHTTPPost makes a simple HTTP POST request

func (*Client) CallHTTPPut added in v0.7.1

func (c *Client) CallHTTPPut(ctx context.Context, path string, body interface{}, headers map[string]string) (*HTTPResponse, error)

CallHTTPPut makes a simple HTTP PUT request

type Error

type Error struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data,omitempty"`
}

Error represents a JSON-RPC 2.0 error

func (*Error) Error

func (e *Error) Error() string

type HTTPResponse added in v0.7.1

type HTTPResponse struct {
	StatusCode int
	Body       interface{}
	Headers    map[string]string
}

HTTPResponse represents a simple HTTP response (non-JSON-RPC)

type Request

type Request struct {
	JSONRPC string      `json:"jsonrpc"`
	Method  string      `json:"method"`
	Params  interface{} `json:"params,omitempty"`
	ID      interface{} `json:"id"`
}

Request represents a JSON-RPC 2.0 request

type Response

type Response struct {
	JSONRPC string      `json:"jsonrpc"`
	Result  interface{} `json:"result,omitempty"`
	Error   *Error      `json:"error,omitempty"`
	ID      interface{} `json:"id"`
}

Response represents a JSON-RPC 2.0 response

type ValidatorConfig added in v0.7.0

type ValidatorConfig struct {
	// Range validation
	Min *float64 `json:"min,omitempty"`
	Max *float64 `json:"max,omitempty"`

	// Pattern validation (regex)
	Pattern *string `json:"pattern,omitempty"`

	// Type validation
	Type *string `json:"type,omitempty"`

	// Approximate float comparison
	Approx    *float64 `json:"approx,omitempty"`
	Tolerance *float64 `json:"tolerance,omitempty"`

	// Array validation
	Length    *int          `json:"length,omitempty"`
	MinLength *int          `json:"minLength,omitempty"`
	MaxLength *int          `json:"maxLength,omitempty"`
	Contains  []interface{} `json:"contains,omitempty"`

	// Object validation
	HasFields     []string `json:"hasFields,omitempty"`
	MissingFields []string `json:"missingFields,omitempty"`
}

ValidatorConfig represents extended validation configuration

Jump to

Keyboard shortcuts

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