init
This commit is contained in:
@@ -12,19 +12,19 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type HuaChenIpInfo struct {
|
||||
type HuaChenIpClient struct {
|
||||
AppCode string
|
||||
Host string
|
||||
}
|
||||
|
||||
func NewHuaChenIpInfo(appCode string) *HuaChenIpInfo {
|
||||
return &HuaChenIpInfo{
|
||||
func NewHuaChenIpClient(appCode string) *HuaChenIpClient {
|
||||
return &HuaChenIpClient{
|
||||
AppCode: appCode,
|
||||
Host: "https://c2ba.api.huachen.cn",
|
||||
}
|
||||
}
|
||||
|
||||
func (h *HuaChenIpInfo) GetIpInfo(ip string) (res *ApiResult, err error) {
|
||||
func (h *HuaChenIpClient) GetIpInfo(ip string) (res *ApiResult, err error) {
|
||||
if ip == "" {
|
||||
return nil, errors.New("请输入ip地址")
|
||||
}
|
||||
@@ -43,7 +43,7 @@ func (h *HuaChenIpInfo) GetIpInfo(ip string) (res *ApiResult, err error) {
|
||||
client := &http.Client{}
|
||||
// 创建请求
|
||||
var req *http.Request
|
||||
req, err = http.NewRequest("GET", fullURL, nil)
|
||||
req, err = http.NewRequest(http.MethodGet, fullURL, nil)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "获取ip:%s信息失败,创建请求失败", ip)
|
||||
}
|
44
ip_tool/hua_chen_client_test.go
Normal file
44
ip_tool/hua_chen_client_test.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package ip_tool
|
||||
|
||||
import (
|
||||
"log"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHuaChenIpClient_GetIpInfo(t *testing.T) {
|
||||
type fields struct {
|
||||
AppCode string
|
||||
Host string
|
||||
}
|
||||
type args struct {
|
||||
ip string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
wantRes *ApiResult
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "test1",
|
||||
fields: fields{
|
||||
AppCode: "",
|
||||
Host: "https://c2ba.api.huachen.cn",
|
||||
},
|
||||
args: args{
|
||||
ip: "8.138.116.112",
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
h := &HuaChenIpClient{
|
||||
AppCode: tt.fields.AppCode,
|
||||
Host: tt.fields.Host,
|
||||
}
|
||||
gotRes, err := h.GetIpInfo(tt.args.ip)
|
||||
log.Println(gotRes, err)
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user