gclient

package module
v0.0.0-...-3572a17 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2024 License: Apache-2.0 Imports: 32 Imported by: 3

README

gclient

A simple, yet elegant, HTTP library.

Documentation

Index

Constants

View Source
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 "
)
View Source
const (
	LevelError = "ERROR"
	LevelDebug = "DEBUG"
	LevelWarn  = "WARN"
	LevelInfo  = "INFO"
)
View Source
const Name = "GClient"
View Source
const Version = "alpha"

Variables

View Source
var (
	ErrAutoRedirectDisabled = errors.New("auto redirect is disabled")
)
View Source
var ErrFileCacheExpired = fmt.Errorf("the key is expired")
View Source
var LoggerPrefixName = Name

Functions

func AnyString

func AnyString(any any) string

AnyString converts `any` to string. It's most commonly used converting function.

func Base64Decode

func Base64Decode(str string) (string, error)

func Base64Encode

func Base64Encode(str string) string

func Base64File

func Base64File(path string) (string, error)

func Base64Reader

func Base64Reader(reader io.Reader) (string, error)

func Base64StdEncoding

func Base64StdEncoding(base64Str string) io.Reader

func DefaultHttpClient

func DefaultHttpClient() *http.Client

func DefaultHttpClientWithTimeOut

func DefaultHttpClientWithTimeOut(localAddr net.Addr) *http.Client

func HttpBuildQuery

func HttpBuildQuery(data map[string]any) string

HttpBuildQuery Generate get request parameters

func IsJSONType

func IsJSONType(s string) bool

IsJSONType method is to check JSON content type or not

func IsMatchString

func IsMatchString(expr string, s string) bool

func IsStreamType

func IsStreamType(s string) bool

func IsXMLType

func IsXMLType(s string) bool

IsXMLType method is to check XML content type or not

func Md5File

func Md5File(path string) (string, error)

func Md5Reader

func Md5Reader(reader io.Reader) (string, error)

Md5Reader f, _ := os.Open("./_example/1.jpeg") f.Seek(0, 0) requests.Md5Reader(f)

func Md5String

func Md5String(str string) string

func NewLogger

func NewLogger() *logger

func NewReadCloser

func NewReadCloser(content []byte, repeatable bool) io.ReadCloser

func OnAfterRequestDebug

func OnAfterRequestDebug(client *Client, request *http.Request) error

func OnResponseDebug

func OnResponseDebug(client *Client, request *http.Request, response *Response) error

func OnResponseWriterRequestLog

func OnResponseWriterRequestLog(client *Client, request *http.Request, response *Response) error

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 Sha1String(str string) string

func Sha256String

func Sha256String(str string) string

func URIQuery

func URIQuery(uri string, query ...url.Values) *url.URL

func URLDecode

func URLDecode(str string) (string, error)

func URLEncode

func URLEncode(str string) string

func UrlValues

func UrlValues(uvs ...url.Values) url.Values

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 CacheItem

type CacheItem struct {
	V string    `json:"v"`
	E time.Time `json:"e"`
}

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 New

func New() *Client

func NewSetHttpClient

func NewSetHttpClient(client *http.Client) *Client

func (*Client) AsForm

func (c *Client) AsForm() *Client

func (*Client) AsJson

func (c *Client) AsJson() *Client

func (*Client) AsXml

func (c *Client) AsXml() *Client

func (*Client) Clone

func (c *Client) Clone() *Client

func (*Client) Connect

func (c *Client) Connect(ctx context.Context, uri string, data any) (*Response, error)

func (*Client) ConnectBytes

func (c *Client) ConnectBytes(ctx context.Context, uri string, data any) ([]byte, error)

func (*Client) ConnectUnmarshal

func (c *Client) ConnectUnmarshal(ctx context.Context, uri string, data, d any) error

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, uri string, data any) (*Response, error)

func (*Client) DeleteBytes

func (c *Client) DeleteBytes(ctx context.Context, uri string, data any) ([]byte, error)

func (*Client) DeleteUnmarshal

func (c *Client) DeleteUnmarshal(ctx context.Context, uri string, data, d any) error

func (*Client) DoRequest

func (c *Client) DoRequest(ctx context.Context, method, uri string, body any) (response *Response, err error)

func (*Client) DoRequestBytes

func (c *Client) DoRequestBytes(ctx context.Context, method string, uri string, data any) ([]byte, error)

func (*Client) DoRequestUnmarshal

func (c *Client) DoRequestUnmarshal(ctx context.Context, method string, uri string, data, d any) error

func (*Client) EnableDebug

func (c *Client) EnableDebug() *Client

func (*Client) Get

func (c *Client) Get(ctx context.Context, uri string, data any) (*Response, error)

func (*Client) GetBytes

func (c *Client) GetBytes(ctx context.Context, uri string, data any) ([]byte, error)

func (*Client) GetUnmarshal

func (c *Client) GetUnmarshal(ctx context.Context, uri string, data, d any) error

func (*Client) Head

func (c *Client) Head(ctx context.Context, uri string, data any) (*Response, error)

func (*Client) HeadBytes

func (c *Client) HeadBytes(ctx context.Context, uri string, data any) ([]byte, error)

func (*Client) HeadUnmarshal

func (c *Client) HeadUnmarshal(ctx context.Context, uri string, data, d any) error

func (*Client) Next

func (c *Client) Next(req *http.Request) (*Response, error)

Next calls the next middleware. This should only be call in HandlerFunc.

func (*Client) OnAfterRequest

func (c *Client) OnAfterRequest(callback RequestCallback) *Client

func (*Client) OnBeforeRequest

func (c *Client) OnBeforeRequest(callback ClientCallback) *Client

func (*Client) OnError

func (c *Client) OnError(h ErrorHook) *Client

func (*Client) OnPanic

func (c *Client) OnPanic(h ErrorHook) *Client

func (*Client) OnResponse

func (c *Client) OnResponse(callback ResponseCallback) *Client

func (*Client) OnSuccess

func (c *Client) OnSuccess(h SuccessHook) *Client

func (*Client) Options

func (c *Client) Options(ctx context.Context, uri string, data any) (*Response, error)

func (*Client) OptionsBytes

func (c *Client) OptionsBytes(ctx context.Context, uri string, data any) ([]byte, error)

func (*Client) OptionsUnmarshal

func (c *Client) OptionsUnmarshal(ctx context.Context, uri string, data, d any) error

func (*Client) Patch

func (c *Client) Patch(ctx context.Context, uri string, data any) (*Response, error)

func (*Client) PatchBytes

func (c *Client) PatchBytes(ctx context.Context, uri string, data any) ([]byte, error)

func (*Client) PatchUnmarshal

func (c *Client) PatchUnmarshal(ctx context.Context, uri string, data, d any) error

func (*Client) Post

func (c *Client) Post(ctx context.Context, uri string, data any) (*Response, error)

func (*Client) PostBytes

func (c *Client) PostBytes(ctx context.Context, uri string, data any) ([]byte, error)

func (*Client) PostForm

func (c *Client) PostForm(ctx context.Context, uri string, data url.Values) (*Response, error)

func (*Client) PostFormBytes

func (c *Client) PostFormBytes(ctx context.Context, uri string, data url.Values) ([]byte, error)

func (*Client) PostFormUnmarshal

func (c *Client) PostFormUnmarshal(ctx context.Context, uri string, data url.Values, d any) error

func (*Client) PostFormWithFiles

func (c *Client) PostFormWithFiles(ctx context.Context, uri string, data url.Values) (*Response, error)

func (*Client) PostJson

func (c *Client) PostJson(ctx context.Context, uri string, data any) (*Response, error)

func (*Client) PostJsonBytes

func (c *Client) PostJsonBytes(ctx context.Context, uri string, data any) ([]byte, error)

func (*Client) PostJsonUnmarshal

func (c *Client) PostJsonUnmarshal(ctx context.Context, uri string, data, d any) error

func (*Client) PostUnmarshal

func (c *Client) PostUnmarshal(ctx context.Context, uri string, data, d any) error

func (*Client) Put

func (c *Client) Put(ctx context.Context, uri string, data any) (*Response, error)

func (*Client) PutBytes

func (c *Client) PutBytes(ctx context.Context, uri string, data any) ([]byte, error)

func (*Client) PutUnmarshal

func (c *Client) PutUnmarshal(ctx context.Context, uri string, data, d any) error

func (*Client) SetBaseURL

func (c *Client) SetBaseURL(baseUrl string) *Client

func (*Client) SetCheckRedirect

func (c *Client) SetCheckRedirect(fn func(req *http.Request, via []*http.Request) error)

func (*Client) SetCookie

func (c *Client) SetCookie(cookie Cookie) *Client

func (*Client) SetDebug

func (c *Client) SetDebug(debug bool) *Client

func (*Client) SetHeader

func (c *Client) SetHeader(header http.Header) *Client

func (*Client) SetHttpClient

func (c *Client) SetHttpClient(client *http.Client) *Client

func (*Client) SetJSONMarshaler

func (c *Client) SetJSONMarshaler(marshaler func(v interface{}) ([]byte, error)) *Client

func (*Client) SetJSONUnmarshaler

func (c *Client) SetJSONUnmarshaler(unmarshaler func(data []byte, v interface{}) error) *Client

func (*Client) SetLogger

func (c *Client) SetLogger(logger LoggerInterface) *Client

func (*Client) SetQuery

func (c *Client) SetQuery(query url.Values) *Client

func (*Client) SetRetry

func (c *Client) SetRetry(retryCount int, retryWaitTime time.Duration) *Client

func (*Client) SetTLSConfig

func (c *Client) SetTLSConfig(tlsConfig *tls.Config) *Client

func (*Client) SetTimeout

func (c *Client) SetTimeout(t time.Duration) *Client

func (*Client) SetWriter

func (c *Client) SetWriter(writer io.Writer) *Client

SetWriter os.Stderr

func (*Client) SetXMLMarshaler

func (c *Client) SetXMLMarshaler(marshaler func(v any) ([]byte, error)) *Client

func (*Client) SetXMLUnmarshaler

func (c *Client) SetXMLUnmarshaler(unmarshaler func(data []byte, v any) error) *Client

func (*Client) Trace

func (c *Client) Trace(ctx context.Context, uri string, data any) (*Response, error)

func (*Client) TraceBytes

func (c *Client) TraceBytes(ctx context.Context, uri string, data any) ([]byte, error)

func (*Client) TraceUnmarshal

func (c *Client) TraceUnmarshal(ctx context.Context, uri string, data, d any) error

func (*Client) Unmarshal

func (c *Client) Unmarshal(contentType string, b []byte, d any) (err error)

func (*Client) Use

func (c *Client) Use(middlewares ...MiddlewareFunc) *Client

Use adds one or more middleware handlers to client.

func (*Client) WithBasicAuth

func (c *Client) WithBasicAuth(username, password string) *Client

func (*Client) WithClientJar

func (c *Client) WithClientJar(jar http.CookieJar) *Client

func (*Client) WithContentType

func (c *Client) WithContentType(contentType string) *Client

func (*Client) WithCookie

func (c *Client) WithCookie(k, v string) *Client

func (*Client) WithCookieMap

func (c *Client) WithCookieMap(cookies map[string]string) *Client

func (*Client) WithCookieNextRequest

func (c *Client) WithCookieNextRequest(cache CacheInterface, ttl time.Duration) *Client

func (*Client) WithCookieString

func (c *Client) WithCookieString(cookieString string) *Client

func (*Client) WithHeader

func (c *Client) WithHeader(k, v string) *Client

func (*Client) WithHeaderMap

func (c *Client) WithHeaderMap(headers map[string]string) *Client

func (*Client) WithProxyUrl

func (c *Client) WithProxyUrl(proxyURL string) *Client

WithProxyUrl The correct pattern is like `http://USER:PASSWORD@IP:PORT` or `socks5://USER:PASSWORD@IP:PORT`.

func (*Client) WithRandomMobileUserAgent

func (c *Client) WithRandomMobileUserAgent() *Client

func (*Client) WithRandomUserAgent

func (c *Client) WithRandomUserAgent() *Client

func (*Client) WithRedirectLimit

func (c *Client) WithRedirectLimit(redirectLimit int) *Client

WithRedirectLimit limits the number of jumps.

func (*Client) WithRedirectPolicy

func (c *Client) WithRedirectPolicy(policies ...any) *Client

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 (c *Client) WithTLSKeyCrt(crtFile, keyFile string) *Client

func (*Client) WithToken

func (c *Client) WithToken(token string, authorizationType ...string) *Client

func (*Client) WithUserAgent

func (c *Client) WithUserAgent(userAgent string) *Client

type ClientCallback

type ClientCallback func(c *Client) error
type Cookie map[string][]string

func NewCookieString

func NewCookieString(cookieStr string) Cookie

func (Cookie) Add

func (v Cookie) Add(key, value string)

func (Cookie) Del

func (v Cookie) Del(key string)

func (Cookie) Encode

func (v Cookie) Encode() string

func (Cookie) Get

func (v Cookie) Get(key string) string

func (Cookie) Has

func (v Cookie) Has(key string) bool

func (Cookie) Set

func (v Cookie) Set(key, value string)

type CtxKeyString

type CtxKeyString string

type ErrorHook

type ErrorHook func(c *Client, request *http.Request, err error)

type FileCache

type FileCache struct {
	// contains filtered or unexported fields
}

func (*FileCache) CleanExpired

func (f *FileCache) CleanExpired() error

func (*FileCache) Delete

func (f *FileCache) Delete(key string) error

func (*FileCache) Get

func (f *FileCache) Get(key string) (string, error)

func (*FileCache) Has

func (f *FileCache) Has(key string) bool

func (*FileCache) Set

func (f *FileCache) Set(key, value string, ttl time.Duration) error

type LoggerInterface

type LoggerInterface interface {
	Errorf(format string, v ...any)
	Warnf(format string, v ...any)
	Debugf(format string, v ...any)
	Infof(format string, v ...any)
}

type MiddlewareFunc

type MiddlewareFunc = func(c *Client, r *http.Request) (*Response, error)

type ReadCloser

type ReadCloser struct {
	// contains filtered or unexported fields
}

func (*ReadCloser) Close

func (b *ReadCloser) Close() error

func (*ReadCloser) Read

func (b *ReadCloser) Read(p []byte) (n int, err error)

type RedirectPolicy

type RedirectPolicy interface {
	Apply(req *http.Request, via []*http.Request) error
}

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

type RedirectPolicyFunc func(*http.Request, []*http.Request) error

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.

func (RedirectPolicyFunc) Apply

func (f RedirectPolicyFunc) Apply(req *http.Request, via []*http.Request) error

Apply calls f(req, via).

type RequestCallback

type RequestCallback func(c *Client, req *http.Request) error

type RequestError

type RequestError struct {
	URI        string
	Method     string
	StatusCode int
	Response   *Response
}

func (*RequestError) Error

func (r *RequestError) Error() string

type Response

type Response struct {
	*http.Response
	Request *http.Request
	GClient *Client
}

func (*Response) Close

func (r *Response) Close() error

Close closes the response when it will never be used.

func (*Response) ContentType

func (r *Response) ContentType() string

ContentType response header Content-Type

func (*Response) GetCookie

func (r *Response) GetCookie() Cookie

func (*Response) IsError

func (r *Response) IsError() bool

IsError method returns true if HTTP status `code >= 400` otherwise false.

func (*Response) IsSuccess

func (r *Response) IsSuccess() bool

IsSuccess method returns true if HTTP status `code >= 200 and <= 299` otherwise false.

func (*Response) ReadAll

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

ReadAll retrieves and returns the response content as []byte.

func (*Response) ReadAllString

func (r *Response) ReadAllString() string

ReadAllString retrieves and returns the response content as string.

func (*Response) ReadStream

func (r *Response) ReadStream(fn func(line []byte, number int64)) int64

ReadStream Microsoft ChatGPT Data Structure end

func (*Response) Unmarshal

func (r *Response) Unmarshal(d any) error

Unmarshal content into object from JSON or XML

type ResponseCallback

type ResponseCallback func(c *Client, req *http.Request, resp *Response) error

type ResponseError

type ResponseError struct {
	Response *Response
	Err      error
}

func (*ResponseError) Error

func (e *ResponseError) Error() string

type ResponseInterface

type ResponseInterface interface {
	Close() error
	GetCookie() Cookie
	ReadAll() []byte
	ReadStream(fn func(line []byte, number int64)) int64
	ReadAllString() string
	ContentType() string
	Unmarshal(d any) error
	IsSuccess() bool
	IsError() bool
}

type SuccessHook

type SuccessHook func(c *Client, resp *Response)

Jump to

Keyboard shortcuts

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