diff --git a/services/ip/client.go b/services/ip/client.go new file mode 100644 index 0000000..f0cb56d --- /dev/null +++ b/services/ip/client.go @@ -0,0 +1,36 @@ +package ip + +import ( + "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk" + "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/requests" +) + +const ( + VERSION = "2025-06-24" +) + +var HOST = requests.Host{ + Default: "ip", +} + +type Client struct { + sdk.Client +} + +func NewClient() (client *Client, err error) { + client = new(Client) + err = client.Init() + return +} + +func (client *Client) CreateGetIpRequest(req *IpRequest) (resp *IpResponse, err error) { + resp = CreateIpResponse() + err = client.DoAction(req, resp) + return +} + +func (client *Client) CreateGetIpsRequest(req *IpsRequest) (resp *IpsResponse, err error) { + resp = CreateIpsResponse() + err = client.DoAction(req, resp) + return +} diff --git a/services/ip/client_test.go b/services/ip/client_test.go new file mode 100644 index 0000000..21deb3a --- /dev/null +++ b/services/ip/client_test.go @@ -0,0 +1,48 @@ +package ip + +import ( + "fmt" + "testing" +) + +// 测试获取单个ip地区信息 +func TestGetIp(t *testing.T) { + client, newErr := NewClient() + if newErr != nil { + panic(newErr) + } + req := CreateIpRequest(IpParam{ + Ip: "114.234.202.136", + }) + res, doErr := client.CreateGetIpRequest(req) + if doErr != nil { + panic(doErr) + } + if res.Code != 0 { + t.Error("查询多个ip失败") + } + fmt.Printf(fmt.Sprintf("%v", res)) +} + +// 测试获取多个ip地区信息 +func TestGetIps(t *testing.T) { + client, newErr := NewClient() + if newErr != nil { + panic(newErr) + } + req := CreateIpsRequest(IpsParam{ + Ips: []string{ + "2001:ee0:5208:e600:4c51:3189:28a4:b668", + "114.234.202.136", + }, + }) + res, err := client.CreateGetIpsRequest(req) + if err != nil { + t.Error(err) + return + } + if res.Code != 0 { + t.Error("查询多个ip失败") + } + fmt.Printf(fmt.Sprintf("%v", res)) +} diff --git a/services/ip/ip.go b/services/ip/ip.go new file mode 100644 index 0000000..c8cac42 --- /dev/null +++ b/services/ip/ip.go @@ -0,0 +1,100 @@ +package ip + +import ( + "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/requests" + "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/responses" +) + +type IpInfo struct { + Ip string `json:"ip"` + City string `json:"city"` + Province string `json:"province"` + Country string `json:"country"` + Isp string `json:"isp"` + Owner string `json:"owner"` + Continent string `json:"continent"` + Accuracy string `json:"accuracy"` + Adcode string `json:"adcode"` + Areacode string `json:"areacode"` + Asnumber string `json:"asnumber"` + Radius string `json:"radius"` + Latwgs string `json:"latwgs"` + Lngwgs string `json:"lngwgs"` + Source string `json:"source"` + Timezone string `json:"timezone"` + Zipcode string `json:"zipcode"` + District string `json:"district"` +} + +// IpsParam +// 单个ip请求参数 +type IpParam struct { + Ip string `json:"ip"` +} +type IpRequest struct { + *requests.JsonRequest + Ip string `position:"Json" field:"ip"` +} + +// IpResponse +// 单个ip返回参数 +type IpResponse struct { + *responses.BaseResponse + Code int `json:"code"` + Msg string `json:"msg"` + Data IpInfo `json:"data"` +} + +// CreateIpRequest +// 同时支持ipv4、ipv6格式查询 +func CreateIpRequest(param IpParam) (req *IpRequest) { + req = &IpRequest{ + JsonRequest: &requests.JsonRequest{}, + Ip: param.Ip, + } + req.InitWithApiInfo(HOST, VERSION, "/v1/getIp") + req.Method = requests.POST + return +} +func CreateIpResponse() (resp *IpResponse) { + resp = &IpResponse{ + BaseResponse: &responses.BaseResponse{}, + } + return +} + +// 多个ip请求参数 +type IpsParam struct { + Ips []string `json:"ips"` +} +type IpsRequest struct { + *requests.JsonRequest + Ips []string `position:"Json" field:"ips"` +} + +// IpsResponse +// 多个ip返回参数 +type IpsResponse struct { + *responses.BaseResponse + Code int `json:"code"` + Msg string `json:"msg"` + Data map[string]IpInfo `json:"data"` +} + +// CreateIpsRequest +// 同时支持ipv4、ipv6格式查询 +func CreateIpsRequest(param IpsParam) (req *IpsRequest) { + req = &IpsRequest{ + JsonRequest: &requests.JsonRequest{}, + Ips: param.Ips, + } + req.InitWithApiInfo(HOST, VERSION, "/v1/getIps") + req.Method = requests.POST + return +} +func CreateIpsResponse() (resp *IpsResponse) { + resp = &IpsResponse{ + BaseResponse: &responses.BaseResponse{}, + } + return +}