From 156ea6409c5a14d680696b95483a75136a33e7c5 Mon Sep 17 00:00:00 2001 From: huangqz Date: Mon, 30 Jun 2025 14:59:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0ip=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sdk/requests/json_request.go | 71 +++++++++++++++++++++++++ sdk/requests/request.go | 27 +++++++--- services/ip/client.go | 44 +++++++++++++++ services/ip/client_test.go | 48 +++++++++++++++++ services/ip/ip.go | 100 +++++++++++++++++++++++++++++++++++ 5 files changed, 282 insertions(+), 8 deletions(-) create mode 100644 sdk/requests/json_request.go create mode 100644 services/ip/client.go create mode 100644 services/ip/client_test.go create mode 100644 services/ip/ip.go diff --git a/sdk/requests/json_request.go b/sdk/requests/json_request.go new file mode 100644 index 0000000..0ec75dd --- /dev/null +++ b/sdk/requests/json_request.go @@ -0,0 +1,71 @@ +package requests + +import ( + "bytes" + "encoding/json" + "fmt" + "golib.gaore.com/GaoreGo/haiwai-common-sdk-go/sdk/utils" + "io" + "strings" +) + +type JsonRequest struct { + *baseRequest +} + +func (request *JsonRequest) init() { + request.baseRequest = defaultBaseRequest() + request.baseRequest.AddHeaderParam("Content-Type", Json) + request.Method = POST +} + +func (request *JsonRequest) BuildUrl() string { + + var hostname string + if request.Domain.Func == nil { + hostname = request.Domain.Default + } else if hostname = request.Domain.Func(request.GetEnv(), request.GetArea()); hostname == "" { + hostname = request.Domain.Default + } + + url := fmt.Sprintf("%s://%s", strings.ToLower(request.Scheme), hostname) + if len(request.Port) > 0 { + url = fmt.Sprintf("%s:%s", url, request.Port) + } + return url + request.BuildQueries() +} + +func (request *JsonRequest) GetStyle() string { + return STREAM +} + +func (request *JsonRequest) BuildQueries() string { + path := strings.TrimLeft(strings.TrimSpace(request.GetActionName()), "/") + mod := "&" + if !strings.Contains(path, "?") { + mod = "?" + } + request.queries = "/" + path + mod + utils.GetUrlFormedMap(request.QueryParams) + return request.queries +} + +func (request *JsonRequest) GetActionName() string { + return request.actionName +} + +func (request *JsonRequest) InitWithApiInfo(domain Host, version, urlPath string) { + request.init() + request.SetDomain(domain) + request.version = version + request.actionName = urlPath +} + +func (request *JsonRequest) GetBodyReader() io.Reader { + if request.JsonParams != nil && len(request.JsonParams) > 0 { + body, err := json.Marshal(request.JsonParams) + if err == nil { + return bytes.NewReader(body) + } + } + return strings.NewReader("") +} diff --git a/sdk/requests/request.go b/sdk/requests/request.go index 79cf7ad..e6fc33f 100644 --- a/sdk/requests/request.go +++ b/sdk/requests/request.go @@ -12,8 +12,9 @@ import ( ) const ( - RPC = "RPC" - ROA = "ROA" + RPC = "RPC" + ROA = "ROA" + STREAM = "STREAM" HTTP = "HTTP" HTTPS = "HTTPS" @@ -36,10 +37,11 @@ const ( Raw = "application/octet-stream" Form = "application/x-www-form-urlencoded" - Header = "Header" - Query = "Query" - Body = "Body" - Path = "Path" + Header = "Header" + Query = "Query" + Body = "Body" + BodyJson = "Json" + Path = "Path" TEST = "TEST" PRE = "PRE" @@ -96,6 +98,7 @@ type AcsRequest interface { AddHeaderParam(key, value string) addQueryParam(key, value string) addFormParam(key, value string) + addJsonParam(string, interface{}) } type baseRequest struct { @@ -119,6 +122,7 @@ type baseRequest struct { QueryParams map[string]string Headers map[string]string FormParams map[string]string + JsonParams map[string]interface{} Content []byte queries string @@ -240,6 +244,10 @@ func (request *baseRequest) addFormParam(key, val string) { request.FormParams[key] = val } +func (request *baseRequest) addJsonParam(key string, val interface{}) { + request.JsonParams[key] = val +} + func defaultBaseRequest() (request *baseRequest) { request = &baseRequest{ Scheme: HTTP, @@ -252,6 +260,7 @@ func defaultBaseRequest() (request *baseRequest) { "Accept-Encoding": Json, }, FormParams: make(map[string]string), + JsonParams: make(map[string]interface{}), } return } @@ -300,14 +309,14 @@ func flatRepeatedList(reflectValue reflect.Value, request AcsRequest, position s value = fieldDefault } - err = addParam(request, fieldPosition, name, value) + err = addParam(request, fieldPosition, name, value, reflectValue.Field(i).Interface()) } } return } -func addParam(request AcsRequest, position, key, value string) (err error) { +func addParam(request AcsRequest, position, key, value string, vAny interface{}) (err error) { if len(value) > 0 { switch position { case Header: @@ -316,6 +325,8 @@ func addParam(request AcsRequest, position, key, value string) (err error) { request.addQueryParam(key, value) case Body: request.addFormParam(key, value) + case BodyJson: + request.addJsonParam(key, vAny) default: errmsg := fmt.Sprintf("unsupport positions add param `%s`", position) err = errors.New(errmsg) diff --git a/services/ip/client.go b/services/ip/client.go new file mode 100644 index 0000000..ca90cec --- /dev/null +++ b/services/ip/client.go @@ -0,0 +1,44 @@ +package ip + +import ( + "fmt" + "golib.gaore.com/GaoreGo/haiwai-common-sdk-go/sdk" + "golib.gaore.com/GaoreGo/haiwai-common-sdk-go/sdk/requests" +) + +const ( + VERSION = "2025-06-24" +) + +var HOST = requests.Host{ + Default: "ip.gaore.com", + Func: func(s string, area string) string { + if area != "" { + fmt.Println("ip.gaore.com." + area) + return "ip.gaore.com." + area + } + return "ip.gaore.com.hk" + }, +} + +type Client struct { + sdk.Client +} + +func NewClient(area string, env ...string) (client *Client, err error) { + client = new(Client) + err = client.InitWithArea(area, env...) + 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..2c04b19 --- /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("hk") + 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("hk") + 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..0e140f6 --- /dev/null +++ b/services/ip/ip.go @@ -0,0 +1,100 @@ +package ip + +import ( + "golib.gaore.com/GaoreGo/haiwai-common-sdk-go/sdk/requests" + "golib.gaore.com/GaoreGo/haiwai-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 +}