client

package
v0.12.19 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package client is to manage internal HTTP requests and caching

Index

Constants

View Source
const (
	// VeryShortDuration is a duration for very short-term cache
	VeryShortDuration = 1 * time.Second
	// ShortDuration is a duration for short-term cache
	ShortDuration = 2 * time.Second
	// MediumDuration is a duration for medium-term cache
	MediumDuration = 5 * time.Second
	// LongDuration is a duration for long-term cache
	LongDuration = 10 * time.Second
	// VeryLongDuration is a duration for very long-term cache
	VeryLongDuration = 300 * time.Second

	// AvailabilityCheckTimeout is the per-request HTTP timeout used when probing
	// whether a connection config is reachable via Spider. Kept separate from the
	// cache-duration constants above because it controls network I/O, not caching.
	// 60 s is generous enough for slow CSPs while preventing indefinite hangs.
	AvailabilityCheckTimeout = 60 * time.Second
)
View Source
const MaxDebugBodyLength = 50000

MaxDebugBodyLength is the maximum length of response body for debug logs

View Source
const NoBody = "NOBODY"

NoBody is a constant for empty body

Variables

View Source
var RequestMap = sync.Map{}

RequestMap is a map for request details

Functions

func DecrementRequestMapCount added in v0.12.3

func DecrementRequestMapCount()

DecrementRequestMapCount decrements the RequestMap entry counter.

func EndRequestWithLog

func EndRequestWithLog(c echo.Context, err error, responseData any) error

EndRequestWithLog updates the request details and sends the final response.

func ExecuteHttpRequest

func ExecuteHttpRequest[B any, T any](
	client *resty.Client,
	method string,
	url string,
	headers map[string]string,
	useBody bool,
	body *B,
	result *T,
	cacheDuration time.Duration,
) (*resty.Response, error)

ExecuteHttpRequest performs the HTTP request and fills the result (var requestBody interface{} = nil for empty body)

func ForwardRequestToAny

func ForwardRequestToAny(reqPath string, method string, requestBody any) (any, error)

ForwardRequestToAny forwards the given request to the specified path

func GetRequestMapCount added in v0.12.3

func GetRequestMapCount() int64

GetRequestMapCount returns the current approximate entry count.

func HandleHttpResponse added in v0.12.10

func HandleHttpResponse(resp *resty.Response, err error) error

HandleHttpResponse logs the HTTP error with status code (if available) and returns it wrapped as a *apierr.StatusError so callers can recover the exact status code via errors.As. Returns nil when err is nil.

func IncrementRequestMapCount added in v0.12.3

func IncrementRequestMapCount()

IncrementRequestMapCount increments the RequestMap entry counter. Call this when adding a new entry to RequestMap.

func NewHttpClient added in v0.12.2

func NewHttpClient() *resty.Client

NewHttpClient creates a new HTTP client with Spider Basic Auth configured. It uses SpiderAPIUsername and SpiderAPIPassword from model package (env: TB_SPIDER_USERNAME/PASSWORD). Note: SetDisableWarn(true) suppresses the "Using Basic Auth in HTTP mode" warning since internal service communication is within trusted network.

func NewHttpClientBasic added in v0.12.2

func NewHttpClientBasic() *resty.Client

NewHttpClientBasic creates a new HTTP client without any authentication.

func SetUseBody

func SetUseBody(requestBody any) bool

SetUseBody returns false if the given body is NoBody

func UpdateRequestProgress

func UpdateRequestProgress(reqID string, progressData any)

UpdateRequestProgress updates the handling status of the request.

Types

type CacheItem

type CacheItem[T any] struct {
	Response  T
	ExpiresAt time.Time
}

CacheItem is a struct to store cached item

type CircuitBreakerState added in v0.11.10

type CircuitBreakerState struct {
	FailureCount int
	LastFailure  time.Time
	IsOpen       bool
}

CircuitBreakerState represents the state of a circuit breaker for a specific request

type ProgressInfo

type ProgressInfo struct {
	Title string    `json:"title"`
	Info  any       `json:"info"`
	Time  time.Time `json:"time"`
}

ProgressInfo contains the progress information of a request.

type RequestDetails

type RequestDetails struct {
	StartTime     time.Time   `json:"startTime"`     // The time when the request was received by the server.
	EndTime       time.Time   `json:"endTime"`       // The time when the request was fully processed.
	Status        string      `json:"status"`        // The current status of the request (e.g., "Handling", "Error", "Success").
	RequestInfo   RequestInfo `json:"requestInfo"`   // Extracted information about the request.
	ResponseData  any         `json:"responseData"`  // The data sent back in response to the request.
	ErrorResponse string      `json:"errorResponse"` // A message describing any error that occurred during request processing.
}

RequestDetails contains detailed information about an HTTP request and its processing status.

type RequestInfo

type RequestInfo struct {
	Method string            `json:"method"`         // HTTP method (GET, POST, etc.), indicating the request's action type.
	URL    string            `json:"url"`            // The URL the request is made to.
	Header map[string]string `json:"header"`         // Key-value pairs of the request headers.
	Body   any               `json:"body,omitempty"` // Optional: request body
}

RequestInfo stores the essential details of an HTTP request.

func ExtractRequestInfo

func ExtractRequestInfo(r *http.Request) RequestInfo

ExtractRequestInfo extracts necessary information from http.Request

Jump to

Keyboard shortcuts

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