Skip to content

Commit

Permalink
feat: ip location
Browse files Browse the repository at this point in the history
  • Loading branch information
zenghur committed Aug 14, 2020
1 parent ba0e89b commit 9f9f04f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 7 deletions.
1 change: 1 addition & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const (
districtList API = "/ws/district/v1/list"
districtGetChildren API = "/ws/district/v1/getchildren"
districtSearch API = "/ws/district/v1/search"
ipLocation API = "/ws/location/v1/ip"
)

// Full returns the full path.
Expand Down
8 changes: 1 addition & 7 deletions district.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,11 @@ import (

// DistrictResponse 行政区划回复
type DistrictResponse struct {
Status int `json:"status"`
Message string `json:"message"`
Meta
DataVersion string `json:"data_version"`
Result [][]*DistrictInfo `json:"result"`
}

// Success means a valid response.
func (r *DistrictResponse) Success() bool {
return r != nil && r.Status == 0
}

// DistrictInfo 行政区划信息
type DistrictInfo struct {
ID string `json:"id"`
Expand Down
46 changes: 46 additions & 0 deletions ip_location.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package txlbs

import (
"context"
"net/url"
)

// IPLocationResponse IP定位回复
type IPLocationResponse struct {
Meta
Result struct {
IP string `json:"ip"`
Location struct {
Lng float64 `json:"lng"`
Lat float64 `json:"lat"`
} `json:"location"`
AdInfo struct {
Nation string `json:"nation"`
Province string `json:"province"`
City string `json:"city"`
Adcode int `json:"adcode"`
} `json:"ad_info"`
} `json:"result"`
}

// IPLocation 定位查询
func (c *Client) IPLocation(ctx context.Context, ip string) (*IPLocationResponse, error) {
v := url.Values{}
if ip != "" {
v.Set("ip", ip)
}
v, err := c.signatureQueryParameter(v, string(ipLocation))
if err != nil {
return nil, err
}
r := c.ca.NewRequest().WithPath(ipLocation.Full(v.Encode()))
resp, err := c.ca.Do(ctx, r)
if err != nil {
return nil, err
}
var dr IPLocationResponse
if err := resp.DecodeFromJSON(&dr); err != nil {
return nil, err
}
return &dr, nil
}
12 changes: 12 additions & 0 deletions meta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package txlbs

// Meta includes common response info.
type Meta struct {
Status int `json:"status"`
Message string `json:"message"`
}

// Success means a valid response.
func (r *Meta) Success() bool {
return r != nil && r.Status == 0
}

0 comments on commit 9f9f04f

Please sign in to comment.