builder

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2023 License: MIT Imports: 20 Imported by: 3

README

Here is a generated GitHub README.md based on the provided Go code:

Go HTTP Client Builder

Go HTTP Client Builder is a Go package for building HTTP clients with fluent interface. It provides an easy way to configure HTTP requests and handle responses.

Installation

go get github.com/catnovel/builder

Usage

Create a new client
client := builder.NewClient()
Configure the client
client.SetBaseUrl("https://api.example.com")
       .SetTimeout(30)
       .SetUserAgent("my-app/1.0")
Make a request
req := client.R()
resp, err := req.Get("/users")
Handle response
if err != nil {
  // handle error
}

fmt.Println(resp.GetStatusCode())
fmt.Println(resp.String())
Post form data
req.SetFormData(map[string]string{
  "name": "John",
  "email": "john@email.com", 
})

resp, err := req.Post("/users")

Request builder

The R() method creates a new Request object that can be used to configure an HTTP request.

Set request body
req.SetBody(map[string]interface{}{
  "name": "John",
  "age": 30,
})

Supports body types: string, []byte, map[string]interface{}, etc.

Set request headers
req.SetHeader("Content-Type", "application/json")
   .SetHeader("Authorization", "Bearer token")
Set query parameters
req.SetQueryParam("sort", "desc")
   .SetQueryParam("limit", 100)
Set cookies
req.SetCookies([]*http.Cookie{
  {Name: "session", Value: "1234"},
})

Response builder

The Response object contains the HTTP response and useful methods to handle it.

Get response as string
resp.String()
Get response as JSON
var data struct{}
resp.Json(&data) 
Get response as HTML document
doc := resp.Html()
Get response headers
headers := resp.GetHeader()

Full Example

client := builder.NewClient()
req := client.R()

req.SetQueryParam("page", 1)
   .SetHeader("Authorization", "token")

resp, err := req.Get("/users")

if err != nil {
  // handle error 
}

var result []User
resp.Json(&result)

Contributing

Pull requests are welcome. Feel free to open an issue for any bugs or feature requests.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Constants

View Source
const (
	// MethodGet HTTP method
	MethodGet = "GET"

	// MethodPost HTTP method
	MethodPost = "POST"

	// MethodPut HTTP method
	MethodPut = "PUT"

	// MethodDelete HTTP method
	MethodDelete = "DELETE"

	// MethodPatch HTTP method
	MethodPatch = "PATCH"

	// MethodHead HTTP method
	MethodHead = "HEAD"

	// MethodOptions HTTP method
	MethodOptions = "OPTIONS"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	sync.RWMutex                // 用于保证线程安全
	MaxConcurrent chan struct{} // 用于限制并发数
	// contains filtered or unexported fields
}

Client 类型用于存储 HTTP 请求的相关信息。

func NewClient

func NewClient() *Client

NewClient 方法用于创建一个新的 Client 对象, 并返回该对象的指针。

func (*Client) GetClientBaseURL added in v0.0.8

func (client *Client) GetClientBaseURL() string

GetClientBaseURL 方法用于获取 HTTP 请求的 BaseUrl 部分。它返回一个 string 类型的参数。

func (*Client) GetClientBody

func (client *Client) GetClientBody() interface{}

GetClientBody 方法用于获取 HTTP 请求的 Body 部分。它返回一个 interface{} 类型的参数。

func (*Client) GetClientCookie

func (client *Client) GetClientCookie() string

GetClientCookie 方法用于获取 HTTP 请求的 Cookie 部分。它返回一个 string 类型的参数。

func (*Client) GetClientDebug

func (client *Client) GetClientDebug() bool

GetClientDebug 方法用于获取 HTTP 请求的 Debug 部分。它返回一个 bool 类型的参数。

func (*Client) GetClientHeaders

func (client *Client) GetClientHeaders() http.Header

GetClientHeaders 方法用于获取 HTTP 请求的 Header 部分。它返回一个 http.Header 类型的参数。

func (*Client) GetClientQueryParams

func (client *Client) GetClientQueryParams() url.Values

GetClientQueryParams 方法用于获取 HTTP 请求的 Query 部分。它返回一个 url.Values 类型的参数。

func (*Client) GetClientQueryParamsEncode

func (client *Client) GetClientQueryParamsEncode() string

GetClientQueryParamsEncode 方法用于获取 HTTP 请求的 Query 部分。它返回一个 Encode 后的 string 类型的参数。

func (*Client) GetClientRetryNumber

func (client *Client) GetClientRetryNumber() int

GetClientRetryNumber 方法用于获取 HTTP 请求的 RetryNumber 部分。它返回一个 int 类型的参数。

func (*Client) GetClientTimeout

func (client *Client) GetClientTimeout() int

GetClientTimeout 方法用于获取 HTTP 请求的 Timeout 部分。它返回一个 int 类型的参数。

func (*Client) R

func (client *Client) R() *Request

R 方法用于创建一个新的 Request 对象。它接收一个 string 类型的参数,该参数表示 HTTP 请求的 Path 部分。

func (*Client) SetBaseURL

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

SetBaseURL 方法用于设置HTTP请求的 BaseUrl 部分。它接收一个 string 类型的参数,该参数表示 BaseUrl 的值。

func (*Client) SetBasicAuth

func (client *Client) SetBasicAuth(username, password string) *Client

SetBasicAuth 方法用于设置 HTTP 请求的 BasicAuth 部分。它接收两个 string 类型的参数,分别表示用户名和密码。

func (*Client) SetContentType added in v0.1.0

func (client *Client) SetContentType(contentType string) *Client

SetContentType 方法用于设置 HTTP 请求的 ContentType 部分。它接收一个 string 类型的参数,该参数表示 ContentType 的值。

func (*Client) SetCookie

func (client *Client) SetCookie(cookie string) *Client

SetCookie 方法用于设置 HTTP 请求的 Cookie 部分。它接收一个 string 类型的参数,该参数表示 Cookie 的值。

func (*Client) SetCookieJar

func (client *Client) SetCookieJar(cookieJar http.CookieJar) *Client

SetCookieJar 方法用于设置 HTTP 请求的 CookieJar 部分。它接收一个 http.CookieJar 类型的参数,该参数表示 CookieJar 的值。

func (*Client) SetDebug

func (client *Client) SetDebug() *Client

SetDebug 方法用于设置是否输出调试信息,如果调用该方法,那么将输出调试信息。

func (*Client) SetDebugFile

func (client *Client) SetDebugFile(name string) *Client

SetDebugFile 方法用于设置输出调试信息的文件。它接收一个 string 类型的参数,该参数表示文件名。

func (*Client) SetFormDataQueryParams

func (client *Client) SetFormDataQueryParams(params url.Values) *Client

SetFormDataQueryParams 方法用于设置 HTTP 请求的 Query 部分。它接收一个 url.Values 类型的参数,

func (*Client) SetHeader

func (client *Client) SetHeader(key string, value interface{}) *Client

SetHeader 方法用于设置 HTTP 请求的 Header 部分。它接收两个 string 类型的参数,

func (*Client) SetHeaders

func (client *Client) SetHeaders(headers map[string]interface{}) *Client

SetHeaders 方法用于设置 HTTP 请求的 Header 部分。它接收一个 map[string]interface{} 类型的参数,

func (*Client) SetProxy

func (client *Client) SetProxy(proxy string) *Client

SetProxy 方法用于设置 HTTP 请求的 Proxy 部分。它接收一个 string 类型的参数,该参数表示 Proxy 的值。

func (*Client) SetQueryParam

func (client *Client) SetQueryParam(key string, value any) *Client

SetQueryParam 方法用于设置 HTTP 请求的 Query 部分。它接收两个 string 类型的参数,

func (*Client) SetQueryParamString

func (client *Client) SetQueryParamString(query string) *Client

SetQueryParamString 方法用于设置 HTTP 请求的 Query 部分。它接收一个 string 类型的参数,

func (*Client) SetQueryParams

func (client *Client) SetQueryParams(params map[string]any) *Client

SetQueryParams 方法用于设置 HTTP 请求的 Query 部分。它接收一个 map[string]interface{} 类型的参数,

func (*Client) SetResultFunc added in v0.1.0

func (client *Client) SetResultFunc(f func(v string) (string, error)) *Client

func (*Client) SetRetryCount added in v0.1.0

func (client *Client) SetRetryCount(num int) *Client

SetRetryCount 方法用于设置重试次数。它接收一个 int 类型的参数,该参数表示重试次数。

func (*Client) SetTimeout

func (client *Client) SetTimeout(timeout int) *Client

SetTimeout 方法用于设置 HTTP 请求的 Timeout 部分, timeout 单位为秒。它接收一个 int 类型的参数,该参数表示 Timeout 的值。

func (*Client) SetUserAgent

func (client *Client) SetUserAgent(userAgent string) *Client

SetUserAgent 方法用于设置 HTTP 请求的 User-Agent 部分。它接收一个 string 类型的参数,该参数表示 User-Agent 的值。

type LoggerClient added in v0.1.0

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

func NewLoggerClient added in v0.1.0

func NewLoggerClient(debugFile *os.File) *LoggerClient

NewLoggerClient 方法用于创建一个 LoggerClient 对象, 它接收一个 *os.File 类型的参数,该参数表示日志文件。

func (*LoggerClient) ReturnLog added in v0.1.0

func (builderLogger *LoggerClient) ReturnLog() []string

type Request

type Request struct {
	RequestRaw *http.Request // 指向 http.Request 的指针
	// contains filtered or unexported fields
}

func (*Request) Delete

func (request *Request) Delete(url string) (*Response, error)

Delete 方法用于创建一个 DELETE 请求。它接收一个 string 类型的参数,表示 HTTP 请求的路径。

func (*Request) Get

func (request *Request) Get(url string) (*Response, error)

Get 方法用于创建一个 GET 请求。它接收一个 string 类型的参数,表示 HTTP 请求的路径。

func (*Request) GetHost

func (request *Request) GetHost() string

GetHost 方法用于获取 HTTP 请求的 Host 部分的字符串。

func (*Request) GetMethod

func (request *Request) GetMethod() string

GetMethod 方法用于获取 HTTP 请求的 Method 部分的字符串。

func (*Request) GetPath

func (request *Request) GetPath() string

GetPath 方法用于获取 HTTP 请求的 Path 部分的字符串。

func (*Request) GetProto

func (request *Request) GetProto() string

GetProto 方法用于获取 HTTP 请求的 Proto 部分的字符串。

func (*Request) GetQueryParams

func (request *Request) GetQueryParams() url.Values

GetQueryParams 方法用于获取 HTTP 请求的 Query 部分的 url.Values。

func (*Request) GetQueryParamsEncode

func (request *Request) GetQueryParamsEncode() string

GetQueryParamsEncode 方法用于获取 HTTP 请求的 Query 部分的 URL 编码字符串。

func (*Request) GetQueryParamsNopCloser

func (request *Request) GetQueryParamsNopCloser() io.ReadCloser

GetQueryParamsNopCloser 方法用于获取 HTTP 请求的 Query 部分的 ReadCloser。

func (*Request) GetRequestHeader

func (request *Request) GetRequestHeader() http.Header

GetRequestHeader 方法用于获取 HTTP 请求的 Header 部分的 http.Header。

func (*Request) GetUrl

func (request *Request) GetUrl() string

GetUrl 方法用于获取 HTTP 请求的 URL 部分的字符串。

func (*Request) Head

func (request *Request) Head(url string) (*Response, error)

Head 方法用于创建一个 HEAD 请求。它接收一个 string 类型的参数,表示 HTTP 请求的路径。

func (*Request) Options

func (request *Request) Options(url string) (*Response, error)

Options 方法用于创建一个 OPTIONS 请求。它接收一个 string 类型的参数,表示 HTTP 请求的路径。

func (*Request) Patch

func (request *Request) Patch(url string) (*Response, error)

Patch 方法用于创建一个 PATCH 请求。它接收一个 string 类型的参数,表示 HTTP 请求的路径。

func (*Request) Post

func (request *Request) Post(url string) (*Response, error)

Post 方法用于创建一个 POST 请求。它接收一个 string 类型的参数,表示 HTTP 请求的路径。

func (*Request) Put

func (request *Request) Put(url string) (*Response, error)

Put 方法用于创建一个 PUT 请求。它接收一个 string 类型的参数,表示 HTTP 请求的路径。

func (*Request) SetBody

func (request *Request) SetBody(body interface{}) *Request

SetBody 方法用于设置 HTTP 请求的 Body 部分。它接收一个 interface{} 类型的参数, 该参数可以是以下几种类型:string, []byte, map[string]interface{}, map[string]string, 对于不支持的类型,方法会设置 ContentLength 为 -1,并将 GetBody 方法设置为返回 nil。 如果成功设置了 body,方法会返回 Request 指针本身,以便进行链式调用。

func (*Request) SetCookie

func (request *Request) SetCookie(cookie *http.Cookie) *Request

SetCookie 方法用于设置 HTTP 请求的 Cookie 部分。它接收一个 *http.Cookie 类型的参数,

func (*Request) SetCookies

func (request *Request) SetCookies(cookie []*http.Cookie) *Request

SetCookies 方法用于设置 HTTP 请求的 Cookies 部分。它接收一个 []*http.Cookie 类型的参数,

func (*Request) SetHeader

func (request *Request) SetHeader(key, value string) *Request

SetHeader 方法用于设置 HTTP 请求的 Header 部分。它接收两个 string 类型的参数,

func (*Request) SetHeaders added in v0.0.8

func (request *Request) SetHeaders(headers map[string]any) *Request

func (*Request) SetQueryParam

func (request *Request) SetQueryParam(key string, value interface{}) *Request

SetQueryParam 方法用于设置 HTTP 请求的 Query 部分。它接收两个 string 类型的参数,

func (*Request) SetQueryParams

func (request *Request) SetQueryParams(query map[string]interface{}) *Request

SetQueryParams 方法用于设置 HTTP 请求的 Query 部分。它接收一个 map[string]interface{} 类型的参数,

func (*Request) SetQueryString

func (request *Request) SetQueryString(query string) *Request

SetQueryString 方法用于设置 HTTP 请求的 Query 部分。它接收一个 string 类型的参数,

type Response

type Response struct {
	Result        string         // 响应体字符串结果
	ResponseRaw   *http.Response // 指向 http.Response 的指针
	RequestSource *Request       // 指向 Request 的指针
}

func (*Response) GetByte

func (response *Response) GetByte() []byte

GetByte 方法用于获取 HTTP 响应的字节结果。

func (*Response) GetCookies

func (response *Response) GetCookies() []*http.Cookie

GetCookies 方法用于获取 HTTP 响应的 Cookies 部分。

func (*Response) GetHeader

func (response *Response) GetHeader() http.Header

GetHeader 方法用于获取 HTTP 响应的 Header 部分。

func (*Response) GetProto added in v0.1.0

func (response *Response) GetProto() string

func (*Response) GetStatus

func (response *Response) GetStatus() string

GetStatus 方法用于获取 HTTP 响应的状态。

func (*Response) GetStatusCode

func (response *Response) GetStatusCode() int

GetStatusCode 方法用于获取 HTTP 响应的状态码。

func (*Response) Gjson

func (response *Response) Gjson() gjson.Result

Gjson 方法用于将 HTTP 响应的字符串结果解析为 gjson.Result 对象。

func (*Response) Html

func (response *Response) Html() *goquery.Document

Html 方法用于将 HTTP 响应的字符串结果解析为 HTML 文档。

func (*Response) HtmlGbk

func (response *Response) HtmlGbk() *goquery.Document

HtmlGbk 方法用于将 HTTP 响应的字符串结果解析为 GBK 编码的 HTML 文档。

func (*Response) IsStatusOk

func (response *Response) IsStatusOk() bool

func (*Response) Json

func (response *Response) Json(v any) error

Json 方法用于将 HTTP 响应的字符串结果解析为 JSON 对象。它接收一个 interface{} 类型的参数,该参数必须是指针类型。

func (*Response) String

func (response *Response) String() string

String 方法用于获取 HTTP 响应的字符串结果。

func (*Response) StringGbk

func (response *Response) StringGbk() string

StringGbk 方法用于将 HTTP 响应的字符串结果解码为 GBK 编码的字符串。

Jump to

Keyboard shortcuts

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