1package ipinfo 2 3import ( 4 "strings" 5) 6 7// ASNDetails represents details for an ASN. 8type ASNDetails struct { 9 ASN string `json:"asn"` 10 Name string `json:"name"` 11 Country string `json:"country"` 12 CountryName string `json:"-"` 13 Allocated string `json:"allocated"` 14 Registry string `json:"registry"` 15 Domain string `json:"domain"` 16 NumIPs uint64 `json:"num_ips"` 17 Type string `json:"type"` 18 Prefixes []ASNDetailsPrefix `json:"prefixes"` 19 Prefixes6 []ASNDetailsPrefix `json:"prefixes6"` 20 Peers []string `json:"peers"` 21 Upstreams []string `json:"upstreams"` 22 Downstreams []string `json:"downstreams"` 23} 24 25// ASNDetailsPrefix represents data for prefixes managed by an ASN. 26type ASNDetailsPrefix struct { 27 Netblock string `json:"netblock"` 28 ID string `json:"id"` 29 Name string `json:"name"` 30 Country string `json:"country"` 31 Size string `json:"size"` 32 Status string `json:"status"` 33 Domain string `json:"domain"` 34} 35 36// InvalidASNError is reported when the invalid ASN was specified. 37type InvalidASNError struct { 38 ASN string 39} 40 41func (err *InvalidASNError) Error() string { 42 return "invalid ASN: " + err.ASN 43} 44 45func (v *ASNDetails) setCountryName() { 46 if v.Country != "" { 47 v.CountryName = countriesMap[v.Country] 48 } 49} 50 51// GetASNDetails returns the details for the specified ASN. 52func GetASNDetails(asn string) (*ASNDetails, error) { 53 return DefaultClient.GetASNDetails(asn) 54} 55 56// GetASNDetails returns the details for the specified ASN. 57func (c *Client) GetASNDetails(asn string) (*ASNDetails, error) { 58 if !strings.HasPrefix(asn, "AS") { 59 return nil, &InvalidASNError{ASN: asn} 60 } 61 62 // perform cache lookup. 63 if c.Cache != nil { 64 if res, err := c.Cache.Get(cacheKey(asn)); err == nil { 65 return res.(*ASNDetails), nil 66 } 67 } 68 69 // prepare req 70 req, err := c.newRequest(nil, "GET", asn, nil) 71 if err != nil { 72 return nil, err 73 } 74 75 // do req 76 v := new(ASNDetails) 77 if _, err := c.do(req, v); err != nil { 78 return nil, err 79 } 80 81 // format 82 v.setCountryName() 83 84 // cache req result 85 if c.Cache != nil { 86 if err := c.Cache.Set(cacheKey(asn), v); err != nil { 87 return v, err 88 } 89 } 90 91 return v, nil 92} 93