diff --git a/docs/netutil.md b/docs/netutil.md index b49d4d9..13f59c9 100644 --- a/docs/netutil.md +++ b/docs/netutil.md @@ -31,11 +31,17 @@ import ( - [GetRequestPublicIp](#GetRequestPublicIp) - [IsPublicIP](#IsPublicIP) - [IsInternalIP](#IsInternalIP) -- [HttpGet](#HttpGet) -- [HttpDelete](#HttpDelete) -- [HttpPost](#HttpPost) -- [HttpPut](#HttpPut) -- [HttpPatch](#HttpPatch) +- [HttpRequest](#HttpRequest) +- [HttpClient](#HttpClient) +- [SendRequest](#SendRequest) +- [DecodeResponse](#DecodeResponse) +- [StructToUrlValues](#StructToUrlValues) + +- [HttpGetDeprecated](#HttpGet) +- [HttpDeleteDeprecated](#HttpDelete) +- [HttpPostDeprecated](#HttpPost) +- [HttpPutDeprecated](#HttpPut) +- [HttpPatchDeprecated](#HttpPatch) - [ParseHttpResponse](#ParseHttpResponse)
@@ -335,8 +341,232 @@ func main() { ``` +### HttpRequest +

HttpRequest is a struct used to abstract HTTP request entity.

-### HttpGet +Signature: + +```go +type HttpRequest struct { + RawURL string + Method string + Headers http.Header + QueryParams url.Values + FormData url.Values + Body []byte +} +``` +Example: + +```go +package main + +import ( + "fmt" + "net" + "github.com/duke-git/lancet/v2/netutil" +) + +func main() { + header := http.Header{} + header.Add("Content-Type", "multipart/form-data") + + postData := url.Values{} + postData.Add("userId", "1") + postData.Add("title", "testItem") + + request := &netutil.HttpRequest{ + RawURL: "https://jsonplaceholder.typicode.com/todos", + Method: "POST", + Headers: header, + FormData: postData, + } +} +``` + + +### HttpClient +

HttpClient is a struct used to send HTTP request. It can be instanced with some configurations or none config.

+ +Signature: + +```go +type HttpClient struct { + *http.Client + TLS *tls.Config + Request *http.Request + Config HttpClientConfig +} + +type HttpClientConfig struct { + SSLEnabled bool + TLSConfig *tls.Config + Compressed bool + HandshakeTimeout time.Duration + ResponseTimeout time.Duration + Verbose bool +} + +func NewHttpClient() *HttpClient + +func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient + +``` +Example: + +```go +package main + +import ( + "fmt" + "net" + "time" + "github.com/duke-git/lancet/v2/netutil" +) + +func main() { + httpClientCfg := netutil.HttpClientConfig{ + SSLEnabled: true, + HandshakeTimeout:10 * time.Second + } + httpClient := netutil.NewHttpClientWithConfig(&httpClientCfg) +} +``` + + + +### SendRequest +

Use HttpClient to send HTTP request.

+ +Signature: + +```go +func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, error) +``` +Example: + +```go +package main + +import ( + "fmt" + "net" + "time" + "github.com/duke-git/lancet/v2/netutil" +) + +func main() { + request := &netutil.HttpRequest{ + RawURL: "https://jsonplaceholder.typicode.com/todos/1", + Method: "GET", + } + + httpClient := netutil.NewHttpClient() + resp, err := httpClient.SendRequest(request) + if err != nil || resp.StatusCode != 200 { + log.Fatal(err) + } + + type Todo struct { + UserId int `json:"userId"` + Id int `json:"id"` + Title string `json:"title"` + Completed bool `json:"completed"` + } + + var todo Todo + httpClient.DecodeResponse(resp, &todo) + + fmt.Println(todo.Id) //1 +} +``` + + + +### DecodeResponse +

Decode http response into target object.

+ +Signature: + +```go +func (client *HttpClient) DecodeResponse(resp *http.Response, target any) error +``` +Example: + +```go +package main + +import ( + "fmt" + "net" + "time" + "github.com/duke-git/lancet/v2/netutil" +) + +func main() { + request := &netutil.HttpRequest{ + RawURL: "https://jsonplaceholder.typicode.com/todos/1", + Method: "GET", + } + + httpClient := netutil.NewHttpClient() + resp, err := httpClient.SendRequest(request) + if err != nil || resp.StatusCode != 200 { + log.Fatal(err) + } + + type Todo struct { + UserId int `json:"userId"` + Id int `json:"id"` + Title string `json:"title"` + Completed bool `json:"completed"` + } + + var todo Todo + httpClient.DecodeResponse(resp, &todo) + + fmt.Println(todo.Id) //1 +} +``` + + +### StructToUrlValues +

Convert struct to url values, only convert the field which is exported and has `json` tag.

+ +Signature: + +```go +func StructToUrlValues(targetStruct any) url.Values +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/netutil" +) + +func main() { + type TodoQuery struct { + Id int `json:"id"` + UserId int `json:"userId"` + } + todoQuery := TodoQuery{ + Id: 1, + UserId: 2, + } + todoValues := netutil.StructToUrlValues(todoQuery) + + fmt.Println(todoValues.Get("id")) //1 + fmt.Println(todoValues.Get("userId")) //2 +} +``` + + + +### HttpGet (Deprecated: use SendRequest for replacement)

Send http get request.

Signature: @@ -378,7 +608,7 @@ func main() { -### HttpPost +### HttpPost (Deprecated: use SendRequest for replacement)

Send http post request.

Signature: @@ -427,7 +657,7 @@ func main() { -### HttpPut +### HttpPut (Deprecated: use SendRequest for replacement)

Send http put request.

Signature: @@ -477,7 +707,7 @@ func main() { -### HttpDelete +### HttpDelete (Deprecated: use SendRequest for replacement)

Send http delete request.

Signature: @@ -516,7 +746,7 @@ func main() { -### HttpPatch +### HttpPatch (Deprecated: use SendRequest for replacement)

Send http patch request.

Signature: diff --git a/docs/netutil_zh-CN.md b/docs/netutil_zh-CN.md index 900abda..cde79b2 100644 --- a/docs/netutil_zh-CN.md +++ b/docs/netutil_zh-CN.md @@ -30,11 +30,18 @@ import ( - [IsPublicIP](#IsPublicIP) - [IsInternalIP](#IsInternalIP) -- [HttpGet](#HttpGet) -- [HttpDelete](#HttpDelete) -- [HttpPost](#HttpPost) -- [HttpPut](#HttpPut) -- [HttpPatch](#HttpPatch) +- [HttpRequest](#HttpRequest) +- [HttpClient](#HttpClient) +- [SendRequest](#SendRequest) +- [DecodeResponse](#DecodeResponse) +- [StructToUrlValues](#StructToUrlValues) + +- [HttpGetDeprecated](#HttpGet) +- [HttpDeleteDeprecated](#HttpDelete) +- [HttpPostDeprecated](#HttpPost) +- [HttpPutDeprecated](#HttpPut) +- [HttpPatchDeprecated](#HttpPatch) + - [ParseHttpResponse](#ParseHttpResponse)
@@ -334,8 +341,232 @@ func main() { ``` +### HttpRequest +

HttpRequest用于抽象HTTP请求实体的结构

-### HttpGet +函数签名: + +```go +type HttpRequest struct { + RawURL string + Method string + Headers http.Header + QueryParams url.Values + FormData url.Values + Body []byte +} +``` +例子: + +```go +package main + +import ( + "fmt" + "net" + "github.com/duke-git/lancet/v2/netutil" +) + +func main() { + header := http.Header{} + header.Add("Content-Type", "multipart/form-data") + + postData := url.Values{} + postData.Add("userId", "1") + postData.Add("title", "testItem") + + request := &netutil.HttpRequest{ + RawURL: "https://jsonplaceholder.typicode.com/todos", + Method: "POST", + Headers: header, + FormData: postData, + } +} +``` + + +### HttpClient +

HttpClient是用于发送HTTP请求的结构体。它可以用一些配置参数或无配置实例化.

+ +函数签名: + +```go +type HttpClient struct { + *http.Client + TLS *tls.Config + Request *http.Request + Config HttpClientConfig +} + +type HttpClientConfig struct { + SSLEnabled bool + TLSConfig *tls.Config + Compressed bool + HandshakeTimeout time.Duration + ResponseTimeout time.Duration + Verbose bool +} + +func NewHttpClient() *HttpClient + +func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient + +``` +例子: + +```go +package main + +import ( + "fmt" + "net" + "time" + "github.com/duke-git/lancet/v2/netutil" +) + +func main() { + httpClientCfg := netutil.HttpClientConfig{ + SSLEnabled: true, + HandshakeTimeout:10 * time.Second + } + httpClient := netutil.NewHttpClientWithConfig(&httpClientCfg) +} +``` + + + +### SendRequest +

HttpClient发送http请求

+ +函数签名: + +```go +func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, error) +``` +例子: + +```go +package main + +import ( + "fmt" + "net" + "time" + "github.com/duke-git/lancet/v2/netutil" +) + +func main() { + request := &netutil.HttpRequest{ + RawURL: "https://jsonplaceholder.typicode.com/todos/1", + Method: "GET", + } + + httpClient := netutil.NewHttpClient() + resp, err := httpClient.SendRequest(request) + if err != nil || resp.StatusCode != 200 { + log.Fatal(err) + } + + type Todo struct { + UserId int `json:"userId"` + Id int `json:"id"` + Title string `json:"title"` + Completed bool `json:"completed"` + } + + var todo Todo + httpClient.DecodeResponse(resp, &todo) + + fmt.Println(todo.Id) //1 +} +``` + + + +### DecodeResponse +

解析http响应体到目标结构体

+ +函数签名: + +```go +func (client *HttpClient) DecodeResponse(resp *http.Response, target any) error +``` +例子: + +```go +package main + +import ( + "fmt" + "net" + "time" + "github.com/duke-git/lancet/v2/netutil" +) + +func main() { + request := &netutil.HttpRequest{ + RawURL: "https://jsonplaceholder.typicode.com/todos/1", + Method: "GET", + } + + httpClient := netutil.NewHttpClient() + resp, err := httpClient.SendRequest(request) + if err != nil || resp.StatusCode != 200 { + log.Fatal(err) + } + + type Todo struct { + UserId int `json:"userId"` + Id int `json:"id"` + Title string `json:"title"` + Completed bool `json:"completed"` + } + + var todo Todo + httpClient.DecodeResponse(resp, &todo) + + fmt.Println(todo.Id) //1 +} +``` + + +### StructToUrlValues +

将结构体转为url values, 仅转化结构体导出字段并且包含`json` tag.

+ +函数签名: + +```go +func StructToUrlValues(targetStruct any) url.Values +``` +例子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/netutil" +) + +func main() { + type TodoQuery struct { + Id int `json:"id"` + UserId int `json:"userId"` + } + todoQuery := TodoQuery{ + Id: 1, + UserId: 2, + } + todoValues := netutil.StructToUrlValues(todoQuery) + + fmt.Println(todoValues.Get("id")) //1 + fmt.Println(todoValues.Get("userId")) //2 +} +``` + + + +### HttpGet (Deprecated: use SendRequest for replacement)

发送http get请求

函数签名: @@ -377,7 +608,7 @@ func main() { -### HttpPost +### HttpPost (Deprecated: use SendRequest for replacement)

发送http post请求

函数签名: @@ -426,7 +657,7 @@ func main() { -### HttpPut +### HttpPut (Deprecated: use SendRequest for replacement)

发送http put请求

函数签名: @@ -438,7 +669,7 @@ func main() { // params[3] http client,类型必须是http.Client func HttpPut(url string, params ...any) (*http.Response, error) ``` -Example: +例子: ```go package main @@ -476,7 +707,7 @@ func main() { -### HttpDelete +### HttpDelete (Deprecated: use SendRequest for replacement)

发送http delete请求

函数签名: @@ -515,7 +746,7 @@ func main() { -### HttpPatch +### HttpPatch (Deprecated: use SendRequest for replacement)

发送http patch请求

函数签名: