diff --git a/docs/netutil.md b/docs/netutil.md new file mode 100644 index 0000000..ddab77f --- /dev/null +++ b/docs/netutil.md @@ -0,0 +1,455 @@ +# Netutil +Package netutil contains functions to get net information and send http request. + +
+ +## Source: + +[https://github.com/duke-git/lancet/blob/main/netutil/net.go](https://github.com/duke-git/lancet/blob/main/netutil/net.go) + +[https://github.com/duke-git/lancet/blob/main/netutil/request.go](https://github.com/duke-git/lancet/blob/main/netutil/request.go) + +
+ +## Usage: +```go +import ( + "github.com/duke-git/lancet/netutil" +) +``` + +
+ +## Index +- [ConvertMapToQueryString](#ConvertMapToQueryString) +- [GetInternalIp](#GetInternalIp) +- [GetPublicIpInfo](#GetPublicIpInfo) +- [IsPublicIP](#IsPublicIP) +- [HttpGet](#HttpGet) +- [HttpDelete](#HttpDelete) +- [HttpPost](#HttpPost) +- [HttpPut](#HttpPut) + +- [HttpPatch](#HttpPatch) +- [ParseHttpResponse](#ParseHttpResponse) + +
+ +## Documentation + + +### ConvertMapToQueryString +

Convert map to url query string.

+ +Signature: + +```go +func ConvertMapToQueryString(param map[string]interface{}) string +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/netutil" +) + +func main() { + var m = map[string]interface{}{ + "c": 3, + "a": 1, + "b": 2, + } + qs := netutil.ConvertMapToQueryString(m) + + fmt.Println(qs) //a=1&b=2&c=3 +} +``` + + + +### GetInternalIp +

Get internal ip information.

+ +Signature: + +```go +func GetInternalIp() string +``` +Example: + +```go +package main + +import ( + "fmt" + "net" + "github.com/duke-git/lancet/netutil" +) + +func main() { + internalIp := netutil.GetInternalIp() + ip := net.ParseIP(internalIp) + + fmt.Println(ip) //192.168.1.9 +} +``` + + + +### GetPublicIpInfo +

Get public ip information.

+ +Signature: + +```go +func GetPublicIpInfo() (*PublicIpInfo, error) +type PublicIpInfo struct { + Status string `json:"status"` + Country string `json:"country"` + CountryCode string `json:"countryCode"` + Region string `json:"region"` + RegionName string `json:"regionName"` + City string `json:"city"` + Lat float64 `json:"lat"` + Lon float64 `json:"lon"` + Isp string `json:"isp"` + Org string `json:"org"` + As string `json:"as"` + Ip string `json:"query"` +} +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/netutil" +) + +func main() { + publicIpInfo, err := netutil.GetPublicIpInfo() + if err != nil { + fmt.Println(err) + } + + fmt.Println(publicIpInfo) +} +``` + + + +### IsPublicIP +

Checks if a ip is public or not.

+ +Signature: + +```go +func IsPublicIP(IP net.IP) bool +``` +Example: + +```go +package main + +import ( + "fmt" + "net" + "github.com/duke-git/lancet/netutil" +) + +func main() { + ip1 := net.ParseIP("192.168.0.1") + ip2 := net.ParseIP("36.112.24.10") + + fmt.Println(netutil.IsPublicIP(ip1)) //false + fmt.Println(netutil.IsPublicIP(ip2)) //true +} +``` + + + + +### HttpGet +

Send http get request.

+ +Signature: + +```go +// params[0] is header which type should be http.Header or map[string]string, +// params[1] is query param which type should be url.Values or map[string]interface{}, +// params[2] is post body which type should be []byte. +// params[3] is http client which type should be http.Client. +func HttpGet(url string, params ...interface{}) (*http.Response, error) +``` +Example: + +```go +package main + +import ( + "fmt" + "io/ioutil" + "log" + "github.com/duke-git/lancet/netutil" +) + +func main() { + url := "https://jsonplaceholder.typicode.com/todos/1" + header := map[string]string{ + "Content-Type": "application/json", + } + + resp, err := netutil.HttpGet(url, header) + if err != nil { + log.Fatal(err) + } + + body, _ := ioutil.ReadAll(resp.Body) + fmt.Println(body) +} +``` + + + +### HttpPost +

Send http post request.

+ +Signature: + +```go +// params[0] is header which type should be http.Header or map[string]string, +// params[1] is query param which type should be url.Values or map[string]interface{}, +// params[2] is post body which type should be []byte. +// params[3] is http client which type should be http.Client. +func HttpPost(url string, params ...interface{}) (*http.Response, error) +``` +Example: + +```go +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "github.com/duke-git/lancet/netutil" +) + +func main() { + url := "https://jsonplaceholder.typicode.com/todos" + header := map[string]string{ + "Content-Type": "application/json", + } + type Todo struct { + UserId int `json:"userId"` + Title string `json:"title"` + } + todo := Todo{1, "TestAddToDo"} + bodyParams, _ := json.Marshal(todo) + + resp, err := netutil.HttpPost(url, header, nil, bodyParams) + if err != nil { + log.Fatal(err) + } + + body, _ := ioutil.ReadAll(resp.Body) + fmt.Println(body) +} +``` + + + +### HttpPut +

Send http put request.

+ +Signature: + +```go +// params[0] is header which type should be http.Header or map[string]string, +// params[1] is query param which type should be url.Values or map[string]interface{}, +// params[2] is post body which type should be []byte. +// params[3] is http client which type should be http.Client. +func HttpPut(url string, params ...interface{}) (*http.Response, error) +``` +Example: + +```go +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "github.com/duke-git/lancet/netutil" +) + +func main() { + url := "https://jsonplaceholder.typicode.com/todos/1" + header := map[string]string{ + "Content-Type": "application/json", + } + type Todo struct { + Id int `json:"id"` + UserId int `json:"userId"` + Title string `json:"title"` + } + todo := Todo{1, 1, "TestPutToDo"} + bodyParams, _ := json.Marshal(todo) + + resp, err := netutil.HttpPut(url, header, nil, bodyParams) + if err != nil { + log.Fatal(err) + } + + body, _ := ioutil.ReadAll(resp.Body) + fmt.Println(body) +} +``` + + + +### HttpDelete +

Send http delete request.

+ +Signature: + +```go +// params[0] is header which type should be http.Header or map[string]string, +// params[1] is query param which type should be url.Values or map[string]interface{}, +// params[2] is post body which type should be []byte. +// params[3] is http client which type should be http.Client. +func HttpDelete(url string, params ...interface{}) (*http.Response, error) +``` +Example: + +```go +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "github.com/duke-git/lancet/netutil" +) + +func main() { + url := "https://jsonplaceholder.typicode.com/todos/1" + resp, err := netutil.HttpDelete(url) + if err != nil { + log.Fatal(err) + } + + body, _ := ioutil.ReadAll(resp.Body) + fmt.Println(body) +} +``` + + + +### HttpPatch +

Send http patch request.

+ +Signature: + +```go +// params[0] is header which type should be http.Header or map[string]string, +// params[1] is query param which type should be url.Values or map[string]interface{}, +// params[2] is post body which type should be []byte. +// params[3] is http client which type should be http.Client. +func HttpPatch(url string, params ...interface{}) (*http.Response, error) +``` +Example: + +```go +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "github.com/duke-git/lancet/netutil" +) + +func main() { + url := "https://jsonplaceholder.typicode.com/todos/1" + header := map[string]string{ + "Content-Type": "application/json", + } + type Todo struct { + Id int `json:"id"` + UserId int `json:"userId"` + Title string `json:"title"` + } + todo := Todo{1, 1, "TestPatchToDo"} + bodyParams, _ := json.Marshal(todo) + + resp, err := netutil.HttpPatch(url, header, nil, bodyParams) + if err != nil { + log.Fatal(err) + } + + body, _ := ioutil.ReadAll(resp.Body) + fmt.Println(body) +} +``` + + + +### ParseHttpResponse +

Decode http response to specified interface.

+ +Signature: + +```go +func ParseHttpResponse(resp *http.Response, obj interface{}) error +``` +Example: + +```go +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "github.com/duke-git/lancet/netutil" +) + +func main() { + url := "https://jsonplaceholder.typicode.com/todos/1" + header := map[string]string{ + "Content-Type": "application/json", + } + + resp, err := netutil.HttpGet(url, header) + if err != nil { + log.Fatal(err) + } + + type Todo struct { + Id int `json:"id"` + UserId int `json:"userId"` + Title string `json:"title"` + Completed bool `json:"completed"` + } + + toDoResp := &Todo{} + err = netutil.ParseHttpResponse(resp, toDoResp) + if err != nil { + log.Fatal(err) + } + + fmt.Println(toDoResp) +} +``` + diff --git a/docs/netutil_zh-CN.md b/docs/netutil_zh-CN.md new file mode 100644 index 0000000..93c4beb --- /dev/null +++ b/docs/netutil_zh-CN.md @@ -0,0 +1,455 @@ +# Netutil +netutil网络包支持获取ip地址,发送http请求。 + +
+ +## 源码: + +[https://github.com/duke-git/lancet/blob/main/netutil/net.go](https://github.com/duke-git/lancet/blob/main/netutil/net.go) + +[https://github.com/duke-git/lancet/blob/main/netutil/request.go](https://github.com/duke-git/lancet/blob/main/netutil/request.go) + +
+ +## 用法: +```go +import ( + "github.com/duke-git/lancet/netutil" +) +``` + +
+ +## 目录 +- [ConvertMapToQueryString](#ConvertMapToQueryString) +- [GetInternalIp](#GetInternalIp) +- [GetPublicIpInfo](#GetPublicIpInfo) +- [IsPublicIP](#IsPublicIP) +- [HttpGet](#HttpGet) +- [HttpDelete](#HttpDelete) +- [HttpPost](#HttpPost) +- [HttpPut](#HttpPut) + +- [HttpPatch](#HttpPatch) +- [ParseHttpResponse](#ParseHttpResponse) + +
+ +## 文档 + + +### ConvertMapToQueryString +

将map转换成http查询字符串.

+ +函数签名: + +```go +func ConvertMapToQueryString(param map[string]interface{}) string +``` +例子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/netutil" +) + +func main() { + var m = map[string]interface{}{ + "c": 3, + "a": 1, + "b": 2, + } + qs := netutil.ConvertMapToQueryString(m) + + fmt.Println(qs) //a=1&b=2&c=3 +} +``` + + + +### GetInternalIp +

获取内部ip

+ +函数签名: + +```go +func GetInternalIp() string +``` +例子: + +```go +package main + +import ( + "fmt" + "net" + "github.com/duke-git/lancet/netutil" +) + +func main() { + internalIp := netutil.GetInternalIp() + ip := net.ParseIP(internalIp) + + fmt.Println(ip) //192.168.1.9 +} +``` + + + +### GetPublicIpInfo +

获取公网ip信息

+ +函数签名: + +```go +func GetPublicIpInfo() (*PublicIpInfo, error) +type PublicIpInfo struct { + Status string `json:"status"` + Country string `json:"country"` + CountryCode string `json:"countryCode"` + Region string `json:"region"` + RegionName string `json:"regionName"` + City string `json:"city"` + Lat float64 `json:"lat"` + Lon float64 `json:"lon"` + Isp string `json:"isp"` + Org string `json:"org"` + As string `json:"as"` + Ip string `json:"query"` +} +``` +例子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/netutil" +) + +func main() { + publicIpInfo, err := netutil.GetPublicIpInfo() + if err != nil { + fmt.Println(err) + } + + fmt.Println(publicIpInfo) +} +``` + + + +### IsPublicIP +

判断ip是否是公共ip

+ +函数签名: + +```go +func IsPublicIP(IP net.IP) bool +``` +例子: + +```go +package main + +import ( + "fmt" + "net" + "github.com/duke-git/lancet/netutil" +) + +func main() { + ip1 := net.ParseIP("192.168.0.1") + ip2 := net.ParseIP("36.112.24.10") + + fmt.Println(netutil.IsPublicIP(ip1)) //false + fmt.Println(netutil.IsPublicIP(ip2)) //true +} +``` + + + + +### HttpGet +

发送http get请求

+ +函数签名: + +```go +// params[0] http请求header,类型必须是http.Header或者map[string]string +// params[1] http查询字符串,类型必须是url.Values或者map[string]interface{} +// params[2] post请求体,类型必须是[]byte +// params[3] http client,类型必须是http.Client +func HttpGet(url string, params ...interface{}) (*http.Response, error) +``` +例子: + +```go +package main + +import ( + "fmt" + "io/ioutil" + "log" + "github.com/duke-git/lancet/netutil" +) + +func main() { + url := "https://jsonplaceholder.typicode.com/todos/1" + header := map[string]string{ + "Content-Type": "application/json", + } + + resp, err := netutil.HttpGet(url, header) + if err != nil { + log.Fatal(err) + } + + body, _ := ioutil.ReadAll(resp.Body) + fmt.Println(body) +} +``` + + + +### HttpPost +

发送http post请求

+ +函数签名: + +```go +// params[0] http请求header,类型必须是http.Header或者map[string]string +// params[1] http查询字符串,类型必须是url.Values或者map[string]interface{} +// params[2] post请求体,类型必须是[]byte +// params[3] http client,类型必须是http.Client +func HttpPost(url string, params ...interface{}) (*http.Response, error) +``` +例子: + +```go +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "github.com/duke-git/lancet/netutil" +) + +func main() { + url := "https://jsonplaceholder.typicode.com/todos" + header := map[string]string{ + "Content-Type": "application/json", + } + type Todo struct { + UserId int `json:"userId"` + Title string `json:"title"` + } + todo := Todo{1, "TestAddToDo"} + bodyParams, _ := json.Marshal(todo) + + resp, err := netutil.HttpPost(url, header, nil, bodyParams) + if err != nil { + log.Fatal(err) + } + + body, _ := ioutil.ReadAll(resp.Body) + fmt.Println(body) +} +``` + + + +### HttpPut +

发送http put请求

+ +函数签名: + +```go +// params[0] http请求header,类型必须是http.Header或者map[string]string +// params[1] http查询字符串,类型必须是url.Values或者map[string]interface{} +// params[2] post请求体,类型必须是[]byte +// params[3] http client,类型必须是http.Client +func HttpPut(url string, params ...interface{}) (*http.Response, error) +``` +Example: + +```go +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "github.com/duke-git/lancet/netutil" +) + +func main() { + url := "https://jsonplaceholder.typicode.com/todos/1" + header := map[string]string{ + "Content-Type": "application/json", + } + type Todo struct { + Id int `json:"id"` + UserId int `json:"userId"` + Title string `json:"title"` + } + todo := Todo{1, 1, "TestPutToDo"} + bodyParams, _ := json.Marshal(todo) + + resp, err := netutil.HttpPut(url, header, nil, bodyParams) + if err != nil { + log.Fatal(err) + } + + body, _ := ioutil.ReadAll(resp.Body) + fmt.Println(body) +} +``` + + + +### HttpDelete +

发送http delete请求

+ +函数签名: + +```go +// params[0] http请求header,类型必须是http.Header或者map[string]string +// params[1] http查询字符串,类型必须是url.Values或者map[string]interface{} +// params[2] post请求体,类型必须是[]byte +// params[3] http client,类型必须是http.Client +func HttpDelete(url string, params ...interface{}) (*http.Response, error) +``` +例子: + +```go +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "github.com/duke-git/lancet/netutil" +) + +func main() { + url := "https://jsonplaceholder.typicode.com/todos/1" + resp, err := netutil.HttpDelete(url) + if err != nil { + log.Fatal(err) + } + + body, _ := ioutil.ReadAll(resp.Body) + fmt.Println(body) +} +``` + + + +### HttpPatch +

发送http patch请求

+ +函数签名: + +```go +// params[0] http请求header,类型必须是http.Header或者map[string]string +// params[1] http查询字符串,类型必须是url.Values或者map[string]interface{} +// params[2] post请求体,类型必须是[]byte +// params[3] http client,类型必须是http.Client +func HttpPatch(url string, params ...interface{}) (*http.Response, error) +``` +例子: + +```go +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "github.com/duke-git/lancet/netutil" +) + +func main() { + url := "https://jsonplaceholder.typicode.com/todos/1" + header := map[string]string{ + "Content-Type": "application/json", + } + type Todo struct { + Id int `json:"id"` + UserId int `json:"userId"` + Title string `json:"title"` + } + todo := Todo{1, 1, "TestPatchToDo"} + bodyParams, _ := json.Marshal(todo) + + resp, err := netutil.HttpPatch(url, header, nil, bodyParams) + if err != nil { + log.Fatal(err) + } + + body, _ := ioutil.ReadAll(resp.Body) + fmt.Println(body) +} +``` + + + +### ParseHttpResponse +

将http请求响应解码成特定struct值

+ +函数签名: + +```go +func ParseHttpResponse(resp *http.Response, obj interface{}) error +``` +例子: + +```go +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "github.com/duke-git/lancet/netutil" +) + +func main() { + url := "https://jsonplaceholder.typicode.com/todos/1" + header := map[string]string{ + "Content-Type": "application/json", + } + + resp, err := netutil.HttpGet(url, header) + if err != nil { + log.Fatal(err) + } + + type Todo struct { + Id int `json:"id"` + UserId int `json:"userId"` + Title string `json:"title"` + Completed bool `json:"completed"` + } + + toDoResp := &Todo{} + err = netutil.ParseHttpResponse(resp, toDoResp) + if err != nil { + log.Fatal(err) + } + + fmt.Println(toDoResp) +} +``` +