Documentation ¶
Index ¶
- Constants
- Variables
- func NewMultipartFile(initCap int) *multipartFile
- type Config
- type Performance
- type QuerySerializer
- type RequestConfig
- func (rc *RequestConfig) AppendRequestInterceptors(interceptors ...RequestInterceptor) *RequestConfig
- func (rc *RequestConfig) AppendResponseInterceptors(interceptors ...ResponseInterceptor) *RequestConfig
- func (rc *RequestConfig) BuildQuery() string
- func (rc *RequestConfig) BuildURL() string
- func (rc *RequestConfig) PrependRequestInterceptors(interceptors ...RequestInterceptor) *RequestConfig
- func (rc *RequestConfig) PrependResponseInterceptors(interceptors ...ResponseInterceptor) *RequestConfig
- func (rc *RequestConfig) SetBody(body interface{}) *RequestConfig
- func (rc *RequestConfig) SetContext(ctx context.Context) *RequestConfig
- func (rc *RequestConfig) SetCookie(cookie *http.Cookie) *RequestConfig
- func (rc *RequestConfig) SetHeader(key, value string) *RequestConfig
- func (rc *RequestConfig) SetMethod(method string) *RequestConfig
- func (rc *RequestConfig) SetParam(key, value string) *RequestConfig
- func (rc *RequestConfig) SetQuery(key, value string) *RequestConfig
- func (rc *RequestConfig) SetUrl(url string) *RequestConfig
- type RequestInterceptor
- type RequestInterceptorChain
- type Response
- func (r *Response) Body() []byte
- func (r *Response) BodyReader() io.Reader
- func (r *Response) Config() *RequestConfig
- func (r *Response) ContentEncoding() string
- func (r *Response) Cookies() []*http.Cookie
- func (r *Response) Failed() bool
- func (r *Response) Headers() http.Header
- func (r *Response) Json(v interface{}) error
- func (r *Response) Ok() bool
- func (r *Response) OriginalResponse() *http.Response
- func (r *Response) Request() *http.Request
- func (r *Response) SaveToFile(filename string) error
- func (r *Response) Status() int
- func (r *Response) StatusText() string
- func (r *Response) Text() string
- func (r *Response) XML(v interface{}) error
- type ResponseInterceptor
- type ResponseInterceptorChain
- type Surf
- func (s *Surf) CloneDefaultConfig() *Config
- func (s *Surf) Connect(url string, args ...WithRequestConfig) (*Response, error)
- func (s *Surf) Delete(url string, args ...WithRequestConfig) (*Response, error)
- func (s *Surf) Get(url string, args ...WithRequestConfig) (*Response, error)
- func (s *Surf) Head(url string, args ...WithRequestConfig) (*Response, error)
- func (s *Surf) Options(url string, args ...WithRequestConfig) (*Response, error)
- func (s *Surf) Patch(url string, args ...WithRequestConfig) (*Response, error)
- func (s *Surf) Post(url string, args ...WithRequestConfig) (*Response, error)
- func (s *Surf) Put(url string, args ...WithRequestConfig) (*Response, error)
- func (s *Surf) Request(config *RequestConfig) (*Response, error)
- func (s *Surf) Trace(url string, args ...WithRequestConfig) (*Response, error)
- func (s *Surf) Upload(url string, file *multipartFile, args ...WithRequestConfig) (resp *Response, err error)
- type WithRequestConfig
- func WithBaseURL(url string) WithRequestConfig
- func WithBody(body interface{}) WithRequestConfig
- func WithContext(ctx context.Context) WithRequestConfig
- func WithCookies(cookies []*http.Cookie) WithRequestConfig
- func WithHeader(header http.Header) WithRequestConfig
- func WithParams(params map[string]string) WithRequestConfig
- func WithQuery(values url.Values) WithRequestConfig
- func WithRequestInterceptor(handler RequestInterceptor) WithRequestConfig
- func WithResponseInterceptor(handler ResponseInterceptor) WithRequestConfig
- func WithSetCookie(cookie *http.Cookie) WithRequestConfig
- func WithSetHeader(headers http.Header) WithRequestConfig
- func WithSetParam(key, value string) WithRequestConfig
- func WithSetQuery(key, value string) WithRequestConfig
- func WithTimeoutContext(ctx context.Context, timeout time.Duration) WithRequestConfig
- type WithRequestConfigChain
Constants ¶
const (
UserAgent = "surf/" + Version + " (https://github.com/fupengl/surf)"
)
const Version = "0.0.1"
Variables ¶
var ( ErrRequestDataTypeInvalid = errors.New("request data type is not supported") ErrRedirectMissingLocation = errors.New("redirect missing location header") )
var Default = &Surf{Config: DefaultConfig}
Default is the default Surf instance with the default configuration.
var DefaultConfig = &Config{ Client: http.DefaultClient, }
DefaultConfig is the default configuration for Surf.
Functions ¶
func NewMultipartFile ¶
func NewMultipartFile(initCap int) *multipartFile
NewMultipartFile creates a multipart file writer with optional initial capacity
Types ¶
type Config ¶
type Config struct { BaseURL string Header http.Header Timeout time.Duration Cookies []*http.Cookie CookieJar *http.CookieJar Params map[string]string Query url.Values QuerySerializer *QuerySerializer RequestInterceptors []RequestInterceptor ResponseInterceptors []ResponseInterceptor MaxBodyLength int MaxRedirects int Client *http.Client JSONMarshal func(v interface{}) ([]byte, error) JSONUnmarshal func(data []byte, v interface{}) error XMLMarshal func(v interface{}) ([]byte, error) XMLUnmarshal func(data []byte, v interface{}) error // contains filtered or unexported fields }
Config holds the configuration for Surf.
type Performance ¶
type Performance struct { // DNSLookup is a duration that transport took to perform DNSLookup time.Duration // ConnTime is a duration that took to obtain a successful connection. ConnTime time.Duration // TCPConnTime is a duration that took to obtain the TCP connection. TCPConnTime time.Duration // TLSHandshake is a duration that TLS handshake took place. TLSHandshake time.Duration // ServerTime is a duration that server took to respond first byte. ServerTime time.Duration // ResponseTime is a duration since first response byte from server to ResponseTime time.Duration // TotalTime is a duration that total request took end-to-end. TotalTime time.Duration // IsConnReused is whether this connection has been previously IsConnReused bool // IsConnWasIdle is whether this connection was obtained from an IsConnWasIdle bool // ConnIdleTime is a duration how long the connection was previously ConnIdleTime time.Duration // contains filtered or unexported fields }
Performance represents the response performance metrics.
type QuerySerializer ¶
QuerySerializer is responsible for encoding URL query parameters.
type RequestConfig ¶
type RequestConfig struct { BaseURL string Url string Header http.Header Method string Cookies []*http.Cookie Timeout time.Duration Context context.Context Params map[string]string Query url.Values QuerySerializer *QuerySerializer RequestInterceptors []RequestInterceptor ResponseInterceptors []ResponseInterceptor // Body Request body, the request body type will automatically set the content-type. // When processing file uploads, you can pass in the structure returned by NewMultipartFile. Body interface{} MaxBodyLength int MaxRedirects int Client *http.Client Request *http.Request JSONMarshal func(v interface{}) ([]byte, error) JSONUnmarshal func(data []byte, v interface{}) error XMLMarshal func(v interface{}) ([]byte, error) XMLUnmarshal func(data []byte, v interface{}) error // contains filtered or unexported fields }
RequestConfig holds the configuration for a specific HTTP request.
func (*RequestConfig) AppendRequestInterceptors ¶
func (rc *RequestConfig) AppendRequestInterceptors(interceptors ...RequestInterceptor) *RequestConfig
AppendRequestInterceptors appends request interceptors to the interceptor list.
func (*RequestConfig) AppendResponseInterceptors ¶
func (rc *RequestConfig) AppendResponseInterceptors(interceptors ...ResponseInterceptor) *RequestConfig
AppendResponseInterceptors appends response interceptors to the interceptor list.
func (*RequestConfig) BuildQuery ¶
func (rc *RequestConfig) BuildQuery() string
BuildQuery constructs the query string based on the configuration.
func (*RequestConfig) BuildURL ¶
func (rc *RequestConfig) BuildURL() string
BuildURL constructs the full URL based on the configuration.
func (*RequestConfig) PrependRequestInterceptors ¶
func (rc *RequestConfig) PrependRequestInterceptors(interceptors ...RequestInterceptor) *RequestConfig
PrependRequestInterceptors prepends request interceptors to the interceptor list.
func (*RequestConfig) PrependResponseInterceptors ¶
func (rc *RequestConfig) PrependResponseInterceptors(interceptors ...ResponseInterceptor) *RequestConfig
PrependResponseInterceptors prepends response interceptors to the interceptor list.
func (*RequestConfig) SetBody ¶
func (rc *RequestConfig) SetBody(body interface{}) *RequestConfig
SetBody sets a body in the request configuration.
func (*RequestConfig) SetContext ¶
func (rc *RequestConfig) SetContext(ctx context.Context) *RequestConfig
SetContext set ctx to the request configuration.
func (*RequestConfig) SetCookie ¶
func (rc *RequestConfig) SetCookie(cookie *http.Cookie) *RequestConfig
SetCookie adds a cookie to the request configuration.
func (*RequestConfig) SetHeader ¶
func (rc *RequestConfig) SetHeader(key, value string) *RequestConfig
SetHeader sets a header in the request configuration.
func (*RequestConfig) SetMethod ¶
func (rc *RequestConfig) SetMethod(method string) *RequestConfig
SetMethod set http.Method to the request configuration.
func (*RequestConfig) SetParam ¶
func (rc *RequestConfig) SetParam(key, value string) *RequestConfig
SetParam sets a parameter in the request configuration.
func (*RequestConfig) SetQuery ¶
func (rc *RequestConfig) SetQuery(key, value string) *RequestConfig
SetQuery sets a query parameter in the request configuration.
func (*RequestConfig) SetUrl ¶
func (rc *RequestConfig) SetUrl(url string) *RequestConfig
SetUrl set url to the request configuration.
type RequestInterceptor ¶
type RequestInterceptor func(config *RequestConfig) error
RequestInterceptor defines a function signature for request interceptors.
type RequestInterceptorChain ¶
type RequestInterceptorChain []RequestInterceptor
RequestInterceptorChain alias for RequestInterceptors
type Response ¶
type Response struct { Performance *Performance // contains filtered or unexported fields }
Response represents the HTTP response received after sending a request.
func (*Response) BodyReader ¶
BodyReader returns the response body as an io.Reader.
func (*Response) Config ¶
func (r *Response) Config() *RequestConfig
Config returns the request configuration associated with the response.
func (*Response) ContentEncoding ¶
ContentEncoding returns the content encoding specified in the response header. It retrieves the value of the "Content-Encoding" header, indicating the encoding transformation that has been applied to the response body, such as "gzip" or "deflate". If the header is not present, an empty string is returned.
func (*Response) Failed ¶
Failed checks if the HTTP response status code indicates a failure (status code >= 400).
func (*Response) Json ¶
Json parses the JSON response body and stores the result in the provided variable (v).
func (*Response) OriginalResponse ¶
OriginalResponse returns the original HTTP response.
func (*Response) SaveToFile ¶
SaveToFile saves the response body to a file with the specified filename.
func (*Response) StatusText ¶
StatusText returns the status text part of the HTTP status code and reason.
type ResponseInterceptor ¶
ResponseInterceptor defines a function signature for response interceptors.
type ResponseInterceptorChain ¶
type ResponseInterceptorChain []ResponseInterceptor
ResponseInterceptorChain alias for ResponseInterceptors
type Surf ¶
Surf represents the main Surf client configuration.
func (*Surf) CloneDefaultConfig ¶
CloneDefaultConfig creates a deep copy of the default configuration.
func (*Surf) Connect ¶
func (s *Surf) Connect(url string, args ...WithRequestConfig) (*Response, error)
func (*Surf) Delete ¶
func (s *Surf) Delete(url string, args ...WithRequestConfig) (*Response, error)
func (*Surf) Options ¶
func (s *Surf) Options(url string, args ...WithRequestConfig) (*Response, error)
type WithRequestConfig ¶
type WithRequestConfig func(c *RequestConfig)
WithRequestConfig is a function signature for configuring request options.
func WithBaseURL ¶
func WithBaseURL(url string) WithRequestConfig
WithBaseURL sets the BaseURL parameters in the request configuration.
func WithBody ¶
func WithBody(body interface{}) WithRequestConfig
WithBody sets the request body in the request configuration.
func WithContext ¶
func WithContext(ctx context.Context) WithRequestConfig
WithContext sets the context in the request configuration.
func WithCookies ¶
func WithCookies(cookies []*http.Cookie) WithRequestConfig
WithCookies sets the cookies in the request configuration.
func WithHeader ¶
func WithHeader(header http.Header) WithRequestConfig
WithHeader sets the request header in the request configuration.
func WithParams ¶
func WithParams(params map[string]string) WithRequestConfig
WithParams sets the parameters in the request configuration.
func WithQuery ¶
func WithQuery(values url.Values) WithRequestConfig
WithQuery sets the query parameters in the request configuration.
func WithRequestInterceptor ¶
func WithRequestInterceptor(handler RequestInterceptor) WithRequestConfig
WithRequestInterceptor append RequestInterceptor in the request configuration.
func WithResponseInterceptor ¶
func WithResponseInterceptor(handler ResponseInterceptor) WithRequestConfig
WithResponseInterceptor append ResponseInterceptor in the request configuration.
func WithSetCookie ¶
func WithSetCookie(cookie *http.Cookie) WithRequestConfig
WithSetCookie adds a cookie in the request configuration.
func WithSetHeader ¶
func WithSetHeader(headers http.Header) WithRequestConfig
WithSetHeader adds a header in the request configuration.
func WithSetParam ¶
func WithSetParam(key, value string) WithRequestConfig
WithSetParam adds a parameter in the request configuration.
func WithSetQuery ¶
func WithSetQuery(key, value string) WithRequestConfig
WithSetQuery adds a query parameter in the request configuration.
func WithTimeoutContext ¶
func WithTimeoutContext(ctx context.Context, timeout time.Duration) WithRequestConfig
WithTimeoutContext sets the context and timeout in the request configuration.
type WithRequestConfigChain ¶
type WithRequestConfigChain []WithRequestConfig