surf

package module
v0.0.0-...-9618751 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2023 License: MIT Imports: 23 Imported by: 2

Documentation

Index

Constants

View Source
const (
	UserAgent = "surf/" + Version + " (https://github.com/fupengl/surf)"
)
View Source
const Version = "0.0.1"

Variables

View Source
var (
	ErrRequestDataTypeInvalid  = errors.New("request data type is not supported")
	ErrRedirectMissingLocation = errors.New("redirect missing location header")
)
View Source
var Default = &Surf{Config: DefaultConfig}

Default is the default Surf instance with the default configuration.

View Source
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

type QuerySerializer struct {
	Encode func(values url.Values) string
}

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) Body

func (r *Response) Body() []byte

Body returns the raw body of the HTTP response.

func (*Response) BodyReader

func (r *Response) BodyReader() io.Reader

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

func (r *Response) ContentEncoding() string

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) Cookies

func (r *Response) Cookies() []*http.Cookie

Cookies returns the cookies set in the HTTP response.

func (*Response) Failed

func (r *Response) Failed() bool

Failed checks if the HTTP response status code indicates a failure (status code >= 400).

func (*Response) Headers

func (r *Response) Headers() http.Header

Headers returns the HTTP headers of the response.

func (*Response) Json

func (r *Response) Json(v interface{}) error

Json parses the JSON response body and stores the result in the provided variable (v).

func (*Response) Ok

func (r *Response) Ok() bool

Ok checks if the HTTP response status code indicates success (2xx).

func (*Response) OriginalResponse

func (r *Response) OriginalResponse() *http.Response

OriginalResponse returns the original HTTP response.

func (*Response) Request

func (r *Response) Request() *http.Request

Request returns the original HTTP request associated with the response.

func (*Response) SaveToFile

func (r *Response) SaveToFile(filename string) error

SaveToFile saves the response body to a file with the specified filename.

func (*Response) Status

func (r *Response) Status() int

Status returns the HTTP status code of the response.

func (*Response) StatusText

func (r *Response) StatusText() string

StatusText returns the status text part of the HTTP status code and reason.

func (*Response) Text

func (r *Response) Text() string

Text returns the response body as a string.

func (*Response) XML

func (r *Response) XML(v interface{}) error

XML parses the xml response body and stores the result in the provided variable (v).

type ResponseInterceptor

type ResponseInterceptor func(resp *Response) error

ResponseInterceptor defines a function signature for response interceptors.

type ResponseInterceptorChain

type ResponseInterceptorChain []ResponseInterceptor

ResponseInterceptorChain alias for ResponseInterceptors

type Surf

type Surf struct {
	Config *Config
	Debug  bool
}

Surf represents the main Surf client configuration.

func New

func New(config *Config) *Surf

New creates a new Surf instance with the given configuration.

func (*Surf) CloneDefaultConfig

func (s *Surf) CloneDefaultConfig() *Config

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) Get

func (s *Surf) Get(url string, args ...WithRequestConfig) (*Response, error)

func (*Surf) Head

func (s *Surf) Head(url string, args ...WithRequestConfig) (*Response, error)

func (*Surf) Options

func (s *Surf) Options(url string, args ...WithRequestConfig) (*Response, error)

func (*Surf) Patch

func (s *Surf) Patch(url string, args ...WithRequestConfig) (*Response, error)

func (*Surf) Post

func (s *Surf) Post(url string, args ...WithRequestConfig) (*Response, error)

func (*Surf) Put

func (s *Surf) Put(url string, args ...WithRequestConfig) (*Response, error)

func (*Surf) Request

func (s *Surf) Request(config *RequestConfig) (*Response, error)

Request performs an HTTP request using the provided configuration.

func (*Surf) Trace

func (s *Surf) Trace(url string, args ...WithRequestConfig) (*Response, error)

func (*Surf) Upload

func (s *Surf) Upload(url string, file *multipartFile, args ...WithRequestConfig) (resp *Response, err error)

Upload performs a file upload using the provided URL, file, and optional request configuration.

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

Directories

Path Synopsis
examples module

Jump to

Keyboard shortcuts

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