添加ip服务
This commit is contained in:
		
							parent
							
								
									10283347b6
								
							
						
					
					
						commit
						156ea6409c
					
				
							
								
								
									
										71
									
								
								sdk/requests/json_request.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										71
									
								
								sdk/requests/json_request.go
									
									
									
									
									
										Normal file
									
								
							@ -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("")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -14,6 +14,7 @@ import (
 | 
				
			|||||||
const (
 | 
					const (
 | 
				
			||||||
	RPC    = "RPC"
 | 
						RPC    = "RPC"
 | 
				
			||||||
	ROA    = "ROA"
 | 
						ROA    = "ROA"
 | 
				
			||||||
 | 
						STREAM = "STREAM"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	HTTP  = "HTTP"
 | 
						HTTP  = "HTTP"
 | 
				
			||||||
	HTTPS = "HTTPS"
 | 
						HTTPS = "HTTPS"
 | 
				
			||||||
@ -39,6 +40,7 @@ const (
 | 
				
			|||||||
	Header   = "Header"
 | 
						Header   = "Header"
 | 
				
			||||||
	Query    = "Query"
 | 
						Query    = "Query"
 | 
				
			||||||
	Body     = "Body"
 | 
						Body     = "Body"
 | 
				
			||||||
 | 
						BodyJson = "Json"
 | 
				
			||||||
	Path     = "Path"
 | 
						Path     = "Path"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	TEST    = "TEST"
 | 
						TEST    = "TEST"
 | 
				
			||||||
@ -96,6 +98,7 @@ type AcsRequest interface {
 | 
				
			|||||||
	AddHeaderParam(key, value string)
 | 
						AddHeaderParam(key, value string)
 | 
				
			||||||
	addQueryParam(key, value string)
 | 
						addQueryParam(key, value string)
 | 
				
			||||||
	addFormParam(key, value string)
 | 
						addFormParam(key, value string)
 | 
				
			||||||
 | 
						addJsonParam(string, interface{})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type baseRequest struct {
 | 
					type baseRequest struct {
 | 
				
			||||||
@ -119,6 +122,7 @@ type baseRequest struct {
 | 
				
			|||||||
	QueryParams map[string]string
 | 
						QueryParams map[string]string
 | 
				
			||||||
	Headers     map[string]string
 | 
						Headers     map[string]string
 | 
				
			||||||
	FormParams  map[string]string
 | 
						FormParams  map[string]string
 | 
				
			||||||
 | 
						JsonParams  map[string]interface{}
 | 
				
			||||||
	Content     []byte
 | 
						Content     []byte
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	queries      string
 | 
						queries      string
 | 
				
			||||||
@ -240,6 +244,10 @@ func (request *baseRequest) addFormParam(key, val string) {
 | 
				
			|||||||
	request.FormParams[key] = val
 | 
						request.FormParams[key] = val
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (request *baseRequest) addJsonParam(key string, val interface{}) {
 | 
				
			||||||
 | 
						request.JsonParams[key] = val
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func defaultBaseRequest() (request *baseRequest) {
 | 
					func defaultBaseRequest() (request *baseRequest) {
 | 
				
			||||||
	request = &baseRequest{
 | 
						request = &baseRequest{
 | 
				
			||||||
		Scheme:       HTTP,
 | 
							Scheme:       HTTP,
 | 
				
			||||||
@ -252,6 +260,7 @@ func defaultBaseRequest() (request *baseRequest) {
 | 
				
			|||||||
			"Accept-Encoding":    Json,
 | 
								"Accept-Encoding":    Json,
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		FormParams: make(map[string]string),
 | 
							FormParams: make(map[string]string),
 | 
				
			||||||
 | 
							JsonParams: make(map[string]interface{}),
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return
 | 
						return
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -300,14 +309,14 @@ func flatRepeatedList(reflectValue reflect.Value, request AcsRequest, position s
 | 
				
			|||||||
				value = fieldDefault
 | 
									value = fieldDefault
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			err = addParam(request, fieldPosition, name, value)
 | 
								err = addParam(request, fieldPosition, name, value, reflectValue.Field(i).Interface())
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return
 | 
						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 {
 | 
						if len(value) > 0 {
 | 
				
			||||||
		switch position {
 | 
							switch position {
 | 
				
			||||||
		case Header:
 | 
							case Header:
 | 
				
			||||||
@ -316,6 +325,8 @@ func addParam(request AcsRequest, position, key, value string) (err error) {
 | 
				
			|||||||
			request.addQueryParam(key, value)
 | 
								request.addQueryParam(key, value)
 | 
				
			||||||
		case Body:
 | 
							case Body:
 | 
				
			||||||
			request.addFormParam(key, value)
 | 
								request.addFormParam(key, value)
 | 
				
			||||||
 | 
							case BodyJson:
 | 
				
			||||||
 | 
								request.addJsonParam(key, vAny)
 | 
				
			||||||
		default:
 | 
							default:
 | 
				
			||||||
			errmsg := fmt.Sprintf("unsupport positions add param `%s`", position)
 | 
								errmsg := fmt.Sprintf("unsupport positions add param `%s`", position)
 | 
				
			||||||
			err = errors.New(errmsg)
 | 
								err = errors.New(errmsg)
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										44
									
								
								services/ip/client.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								services/ip/client.go
									
									
									
									
									
										Normal file
									
								
							@ -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
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										48
									
								
								services/ip/client_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								services/ip/client_test.go
									
									
									
									
									
										Normal file
									
								
							@ -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))
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										100
									
								
								services/ip/ip.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										100
									
								
								services/ip/ip.go
									
									
									
									
									
										Normal file
									
								
							@ -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
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user