Documentation ¶
Index ¶
- Constants
- Variables
- func AnyString(any any) string
- func Base64Decode(str string) (string, error)
- func Base64Encode(str string) string
- func Base64File(path string) (string, error)
- func Base64Reader(reader io.Reader) (string, error)
- func Base64StdEncoding(base64Str string) io.Reader
- func DefaultHttpClient() *http.Client
- func DefaultHttpClientWithTimeOut(localAddr net.Addr) *http.Client
- func HttpBuildQuery(data map[string]any) string
- func IsJSONType(s string) bool
- func IsMatchString(expr string, s string) bool
- func IsStreamType(s string) bool
- func IsXMLType(s string) bool
- func Md5File(path string) (string, error)
- func Md5Reader(reader io.Reader) (string, error)
- func Md5String(str string) string
- func NewLogger() *logger
- func NewReadCloser(content []byte, repeatable bool) io.ReadCloser
- func OnAfterRequestDebug(client *Client, request *http.Request) error
- func OnResponseDebug(client *Client, request *http.Request, response *Response) error
- func OnResponseWriterRequestLog(client *Client, request *http.Request, response *Response) error
- func RandomMobileUserAgent() string
- func RandomUserAgent() string
- func Sha1String(str string) string
- func Sha256String(str string) string
- func URIQuery(uri string, query ...url.Values) *url.URL
- func URLDecode(str string) (string, error)
- func URLEncode(str string) string
- func UrlValues(uvs ...url.Values) url.Values
- type CacheInterface
- type CacheItem
- type Client
- func (c *Client) AsForm() *Client
- func (c *Client) AsJson() *Client
- func (c *Client) AsXml() *Client
- func (c *Client) Clone() *Client
- func (c *Client) Connect(ctx context.Context, uri string, data any) (*Response, error)
- func (c *Client) ConnectBytes(ctx context.Context, uri string, data any) ([]byte, error)
- func (c *Client) ConnectUnmarshal(ctx context.Context, uri string, data, d any) error
- func (c *Client) Delete(ctx context.Context, uri string, data any) (*Response, error)
- func (c *Client) DeleteBytes(ctx context.Context, uri string, data any) ([]byte, error)
- func (c *Client) DeleteUnmarshal(ctx context.Context, uri string, data, d any) error
- func (c *Client) DoRequest(ctx context.Context, method, uri string, body any) (response *Response, err error)
- func (c *Client) DoRequestBytes(ctx context.Context, method string, uri string, data any) ([]byte, error)
- func (c *Client) DoRequestUnmarshal(ctx context.Context, method string, uri string, data, d any) error
- func (c *Client) EnableDebug() *Client
- func (c *Client) Get(ctx context.Context, uri string, data any) (*Response, error)
- func (c *Client) GetBytes(ctx context.Context, uri string, data any) ([]byte, error)
- func (c *Client) GetUnmarshal(ctx context.Context, uri string, data, d any) error
- func (c *Client) Head(ctx context.Context, uri string, data any) (*Response, error)
- func (c *Client) HeadBytes(ctx context.Context, uri string, data any) ([]byte, error)
- func (c *Client) HeadUnmarshal(ctx context.Context, uri string, data, d any) error
- func (c *Client) Next(req *http.Request) (*Response, error)
- func (c *Client) OnAfterRequest(callback RequestCallback) *Client
- func (c *Client) OnBeforeRequest(callback ClientCallback) *Client
- func (c *Client) OnError(h ErrorHook) *Client
- func (c *Client) OnPanic(h ErrorHook) *Client
- func (c *Client) OnResponse(callback ResponseCallback) *Client
- func (c *Client) OnSuccess(h SuccessHook) *Client
- func (c *Client) Options(ctx context.Context, uri string, data any) (*Response, error)
- func (c *Client) OptionsBytes(ctx context.Context, uri string, data any) ([]byte, error)
- func (c *Client) OptionsUnmarshal(ctx context.Context, uri string, data, d any) error
- func (c *Client) Patch(ctx context.Context, uri string, data any) (*Response, error)
- func (c *Client) PatchBytes(ctx context.Context, uri string, data any) ([]byte, error)
- func (c *Client) PatchUnmarshal(ctx context.Context, uri string, data, d any) error
- func (c *Client) Post(ctx context.Context, uri string, data any) (*Response, error)
- func (c *Client) PostBytes(ctx context.Context, uri string, data any) ([]byte, error)
- func (c *Client) PostForm(ctx context.Context, uri string, data url.Values) (*Response, error)
- func (c *Client) PostFormBytes(ctx context.Context, uri string, data url.Values) ([]byte, error)
- func (c *Client) PostFormUnmarshal(ctx context.Context, uri string, data url.Values, d any) error
- func (c *Client) PostFormWithFiles(ctx context.Context, uri string, data url.Values) (*Response, error)
- func (c *Client) PostJson(ctx context.Context, uri string, data any) (*Response, error)
- func (c *Client) PostJsonBytes(ctx context.Context, uri string, data any) ([]byte, error)
- func (c *Client) PostJsonUnmarshal(ctx context.Context, uri string, data, d any) error
- func (c *Client) PostUnmarshal(ctx context.Context, uri string, data, d any) error
- func (c *Client) Put(ctx context.Context, uri string, data any) (*Response, error)
- func (c *Client) PutBytes(ctx context.Context, uri string, data any) ([]byte, error)
- func (c *Client) PutUnmarshal(ctx context.Context, uri string, data, d any) error
- func (c *Client) SetBaseURL(baseUrl string) *Client
- func (c *Client) SetCheckRedirect(fn func(req *http.Request, via []*http.Request) error)
- func (c *Client) SetCookie(cookie Cookie) *Client
- func (c *Client) SetDebug(debug bool) *Client
- func (c *Client) SetHeader(header http.Header) *Client
- func (c *Client) SetHttpClient(client *http.Client) *Client
- func (c *Client) SetJSONMarshaler(marshaler func(v interface{}) ([]byte, error)) *Client
- func (c *Client) SetJSONUnmarshaler(unmarshaler func(data []byte, v interface{}) error) *Client
- func (c *Client) SetLogger(logger LoggerInterface) *Client
- func (c *Client) SetQuery(query url.Values) *Client
- func (c *Client) SetRetry(retryCount int, retryWaitTime time.Duration) *Client
- func (c *Client) SetTLSConfig(tlsConfig *tls.Config) *Client
- func (c *Client) SetTimeout(t time.Duration) *Client
- func (c *Client) SetWriter(writer io.Writer) *Client
- func (c *Client) SetXMLMarshaler(marshaler func(v any) ([]byte, error)) *Client
- func (c *Client) SetXMLUnmarshaler(unmarshaler func(data []byte, v any) error) *Client
- func (c *Client) Trace(ctx context.Context, uri string, data any) (*Response, error)
- func (c *Client) TraceBytes(ctx context.Context, uri string, data any) ([]byte, error)
- func (c *Client) TraceUnmarshal(ctx context.Context, uri string, data, d any) error
- func (c *Client) Unmarshal(contentType string, b []byte, d any) (err error)
- func (c *Client) Use(middlewares ...MiddlewareFunc) *Client
- func (c *Client) WithBasicAuth(username, password string) *Client
- func (c *Client) WithClientJar(jar http.CookieJar) *Client
- func (c *Client) WithContentType(contentType string) *Client
- func (c *Client) WithCookie(k, v string) *Client
- func (c *Client) WithCookieMap(cookies map[string]string) *Client
- func (c *Client) WithCookieNextRequest(cache CacheInterface, ttl time.Duration) *Client
- func (c *Client) WithCookieString(cookieString string) *Client
- func (c *Client) WithHeader(k, v string) *Client
- func (c *Client) WithHeaderMap(headers map[string]string) *Client
- func (c *Client) WithProxyUrl(proxyURL string) *Client
- func (c *Client) WithRandomMobileUserAgent() *Client
- func (c *Client) WithRandomUserAgent() *Client
- func (c *Client) WithRedirectLimit(redirectLimit int) *Client
- func (c *Client) WithRedirectPolicy(policies ...any) *Client
- func (c *Client) WithTLSKeyCrt(crtFile, keyFile string) *Client
- func (c *Client) WithToken(token string, authorizationType ...string) *Client
- func (c *Client) WithUserAgent(userAgent string) *Client
- type ClientCallback
- type Cookie
- type CtxKeyString
- type ErrorHook
- type FileCache
- type LoggerInterface
- type MiddlewareFunc
- type ReadCloser
- type RedirectPolicy
- type RedirectPolicyFunc
- type RequestCallback
- type RequestError
- type Response
- func (r *Response) Close() error
- func (r *Response) ContentType() string
- func (r *Response) GetCookie() Cookie
- func (r *Response) IsError() bool
- func (r *Response) IsSuccess() bool
- func (r *Response) ReadAll() []byte
- func (r *Response) ReadAllString() string
- func (r *Response) ReadStream(fn func(line []byte, number int64)) int64
- func (r *Response) Unmarshal(d any) error
- type ResponseCallback
- type ResponseError
- type ResponseInterface
- type SuccessHook
Constants ¶
const ( HttpSchemeName = `http` HttpParamFileHolder = `@file:` HttpRegexParamJson = `^[\w\[\]]+=.+` HttpHeaderHost = `Host` HttpHeaderCookie = `Cookie` HttpHeaderUserAgent = `User-Agent` HttpHeaderAuthorization = "Authorization" HttpHeaderAccept = "Accept" HttpMIMEEventStream = "text/event-stream" HttpHeaderCacheControl = "Cache-Control" HttpHeaderConnection = "Connection" HttpHeaderContentType = `Content-Type` CharsetUTF8 = "charset=UTF-8" HttpHeaderContentTypeJson = `application/json` HttpHeaderContentTypeJsonCharsetUTF8 = HttpHeaderContentTypeJson + "; " + CharsetUTF8 HttpHeaderContentTypeXml = `application/xml` HttpHeaderContentTypeXmlCharsetUTF8 = HttpHeaderContentTypeXml + "; " + CharsetUTF8 HttpHeaderContentTypeForm = `application/x-www-form-urlencoded` AuthorizationTypeBearer = "Bearer " AuthorizationTypeBasic = "Basic " )
const ( LevelError = "ERROR" LevelDebug = "DEBUG" LevelWarn = "WARN" LevelInfo = "INFO" )
const Name = "GClient"
const Version = "alpha"
Variables ¶
var (
ErrAutoRedirectDisabled = errors.New("auto redirect is disabled")
)
var ErrFileCacheExpired = fmt.Errorf("the key is expired")
var LoggerPrefixName = Name
Functions ¶
func Base64Decode ¶
func Base64Encode ¶
func Base64File ¶
func Base64StdEncoding ¶
func DefaultHttpClient ¶
func HttpBuildQuery ¶
HttpBuildQuery Generate get request parameters
func IsJSONType ¶
IsJSONType method is to check JSON content type or not
func IsMatchString ¶
func IsStreamType ¶
func NewReadCloser ¶
func NewReadCloser(content []byte, repeatable bool) io.ReadCloser
func OnResponseDebug ¶
func RandomMobileUserAgent ¶
func RandomMobileUserAgent() string
RandomMobileUserAgent generates a random MOBILE browser user-agent on every requests
func RandomUserAgent ¶
func RandomUserAgent() string
RandomUserAgent generates a random DESKTOP browser user-agent on every requests
func Sha1String ¶
func Sha256String ¶
Types ¶
type CacheInterface ¶
type CacheInterface interface { Set(key, value string, ttl time.Duration) error Get(key string) (string, error) Has(key string) bool Delete(key string) error CleanExpired() error }
func NewFileCache ¶
func NewFileCache(paths ...string) CacheInterface
type Client ¶
type Client struct { *http.Client Debug bool BaseURL string Query url.Values Header http.Header Cookie Cookie Logger LoggerInterface JSONMarshal func(v any) ([]byte, error) JSONUnmarshal func(data []byte, v any) error XMLMarshal func(v any) ([]byte, error) XMLUnmarshal func(data []byte, v any) error // contains filtered or unexported fields }
func NewSetHttpClient ¶
func (*Client) ConnectBytes ¶
func (*Client) ConnectUnmarshal ¶
func (*Client) DeleteBytes ¶
func (*Client) DeleteUnmarshal ¶
func (*Client) DoRequestBytes ¶
func (*Client) DoRequestUnmarshal ¶
func (*Client) EnableDebug ¶
func (*Client) GetUnmarshal ¶
func (*Client) HeadUnmarshal ¶
func (*Client) OnAfterRequest ¶
func (c *Client) OnAfterRequest(callback RequestCallback) *Client
func (*Client) OnBeforeRequest ¶
func (c *Client) OnBeforeRequest(callback ClientCallback) *Client
func (*Client) OnResponse ¶
func (c *Client) OnResponse(callback ResponseCallback) *Client
func (*Client) OnSuccess ¶
func (c *Client) OnSuccess(h SuccessHook) *Client
func (*Client) OptionsBytes ¶
func (*Client) OptionsUnmarshal ¶
func (*Client) PatchBytes ¶
func (*Client) PatchUnmarshal ¶
func (*Client) PostFormBytes ¶
func (*Client) PostFormUnmarshal ¶
func (*Client) PostFormWithFiles ¶
func (*Client) PostJsonBytes ¶
func (*Client) PostJsonUnmarshal ¶
func (*Client) PostUnmarshal ¶
func (*Client) PutUnmarshal ¶
func (*Client) SetBaseURL ¶
func (*Client) SetCheckRedirect ¶
func (*Client) SetJSONMarshaler ¶
func (*Client) SetJSONUnmarshaler ¶
func (*Client) SetLogger ¶
func (c *Client) SetLogger(logger LoggerInterface) *Client
func (*Client) SetXMLMarshaler ¶
func (*Client) SetXMLUnmarshaler ¶
func (*Client) TraceBytes ¶
func (*Client) TraceUnmarshal ¶
func (*Client) Use ¶
func (c *Client) Use(middlewares ...MiddlewareFunc) *Client
Use adds one or more middleware handlers to client.
func (*Client) WithBasicAuth ¶
func (*Client) WithContentType ¶
func (*Client) WithCookie ¶
func (*Client) WithCookieNextRequest ¶
func (c *Client) WithCookieNextRequest(cache CacheInterface, ttl time.Duration) *Client
func (*Client) WithCookieString ¶
func (*Client) WithHeader ¶
func (*Client) WithProxyUrl ¶
WithProxyUrl The correct pattern is like `http://USER:PASSWORD@IP:PORT` or `socks5://USER:PASSWORD@IP:PORT`.
func (*Client) WithRandomMobileUserAgent ¶
func (*Client) WithRandomUserAgent ¶
func (*Client) WithRedirectLimit ¶
WithRedirectLimit limits the number of jumps.
func (*Client) WithRedirectPolicy ¶
WithRedirectPolicy method sets the client redirect poilicy. provides ready to use redirect policies. Wanna create one for yourself refer to `redirect.go`.
WithRedirectLimit(20) WithRedirectPolicy(FlexibleRedirectPolicy(20)) WithRedirectPolicy(FlexibleRedirectPolicy(20), DomainCheckRedirectPolicy("host1.com", "host2.net"))
func (*Client) WithTLSKeyCrt ¶
func (*Client) WithUserAgent ¶
type ClientCallback ¶
type Cookie ¶
func NewCookieString ¶
type CtxKeyString ¶
type CtxKeyString string
type FileCache ¶
type FileCache struct {
// contains filtered or unexported fields
}
func (*FileCache) CleanExpired ¶
type LoggerInterface ¶
type ReadCloser ¶
type ReadCloser struct {
// contains filtered or unexported fields
}
func (*ReadCloser) Close ¶
func (b *ReadCloser) Close() error
type RedirectPolicy ¶
RedirectPolicy to regulate the redirects in the client. Objects implementing the RedirectPolicy interface can be registered as
Apply function should return nil to continue the redirect jounery, otherwise return error to stop the redirect.
func DomainCheckRedirectPolicy ¶
func DomainCheckRedirectPolicy(hostnames ...string) RedirectPolicy
DomainCheckRedirectPolicy is convenient method to define domain name redirect rule in client. Redirect is allowed for only mentioned host in the policy.
requests.WithRedirectPolicy(DomainCheckRedirectPolicy("host1.com", "host2.org", "host3.net"))
func FlexibleRedirectPolicy ¶
func FlexibleRedirectPolicy(noOfRedirect int) RedirectPolicy
FlexibleRedirectPolicy is convenient method to create No of redirect policy for HTTP client.
func NoRedirectPolicy ¶
func NoRedirectPolicy() RedirectPolicy
NoRedirectPolicy is used to disable redirects in the HTTP client
requests.WithRedirectPolicy(NoRedirectPolicy())
type RedirectPolicyFunc ¶
The RedirectPolicyFunc type is an adapter to allow the use of ordinary functions as RedirectPolicy. If f is a function with the appropriate signature, RedirectPolicyFunc(f) is a RedirectPolicy object that calls f.
type RequestError ¶
func (*RequestError) Error ¶
func (r *RequestError) Error() string
type Response ¶
func (*Response) ContentType ¶
ContentType response header Content-Type
func (*Response) IsError ¶
IsError method returns true if HTTP status `code >= 400` otherwise false.
func (*Response) IsSuccess ¶
IsSuccess method returns true if HTTP status `code >= 200 and <= 299` otherwise false.
func (*Response) ReadAllString ¶
ReadAllString retrieves and returns the response content as string.
func (*Response) ReadStream ¶
ReadStream Microsoft ChatGPT Data Structure end
type ResponseCallback ¶
type ResponseError ¶
func (*ResponseError) Error ¶
func (e *ResponseError) Error() string
type ResponseInterface ¶
type SuccessHook ¶
Source Files ¶
- client.go
- client_callback.go
- client_cookie.go
- client_header.go
- client_middleware.go
- client_redirect.go
- client_request.go
- client_request_bytes.go
- client_request_do.go
- client_request_unmarshal.go
- cookie.go
- crypto.go
- define_callbacks.go
- errors.go
- file_cache.go
- helper.go
- logger.go
- random_user_agent.go
- read_closer.go
- response.go