Skip to content

Commit f57915c

Browse files
authored
Merge pull request #20 from ipinfo/uman/country-name
Add country full name using a abbrev->name mapping.
2 parents e54b195 + 1e46139 commit f57915c

File tree

7 files changed

+320
-18
lines changed

7 files changed

+320
-18
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 2.1.0
2+
3+
- A new field `CountryName` was added to both `ASNDetails` and `Core`, which
4+
is the full name of the country abbreviated in the existing `Country` field.
5+
For example, if `Country == "PK"`, now `CountryName == "Pakistan'` exists.
6+
17
# 2.0.0
28

39
- The API for creating a client and making certain requests has changed and has

example/get-country-name/main.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/ipinfo/go/ipinfo"
8+
)
9+
10+
func main() {
11+
countryName, err := ipinfo.GetIPCountryName(nil)
12+
if err != nil {
13+
log.Fatal(err)
14+
}
15+
fmt.Printf("%v\n", countryName)
16+
}

ipinfo/asn.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type ASNDetails struct {
99
ASN string `json:"asn"`
1010
Name string `json:"name"`
1111
Country string `json:"country"`
12+
CountryName string `json:"-"`
1213
Allocated string `json:"allocated"`
1314
Registry string `json:"registry"`
1415
Domain string `json:"domain"`
@@ -73,6 +74,11 @@ func (c *Client) GetASNDetails(asn string) (*ASNDetails, error) {
7374
return nil, err
7475
}
7576

77+
// map country to full country name
78+
if v.Country != "" {
79+
v.CountryName = countriesMap[v.Country]
80+
}
81+
7682
// cache req result
7783
if c.Cache != nil {
7884
if err := c.Cache.Set(cacheKey, v); err != nil {

ipinfo/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
const (
1414
defaultBaseURL = "https://ipinfo.io/"
15-
defaultUserAgent = "IPinfoClient/Go/2.0.0"
15+
defaultUserAgent = "IPinfoClient/Go/2.1.0"
1616
)
1717

1818
// A Client is the main handler to communicate with the IPinfo API.

ipinfo/core.go

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@ import (
66

77
// Core represents data from the Core API.
88
type Core struct {
9-
IP net.IP `json:"ip"`
10-
Hostname string `json:"hostname"`
11-
City string `json:"city"`
12-
Region string `json:"region"`
13-
Country string `json:"country"`
14-
Location string `json:"loc"`
15-
Org string `json:"org"`
16-
Postal string `json:"postal"`
17-
Timezone string `json:"timezone"`
18-
ASN *CoreASN `json:"asn"`
19-
Company *CoreCompany `json:"company"`
20-
Carrier *CoreCarrier `json:"carrier"`
21-
Privacy *CorePrivacy `json:"privacy"`
22-
Abuse *CoreAbuse `json:"abuse"`
23-
Domains *CoreDomains `json:"domains"`
9+
IP net.IP `json:"ip"`
10+
Hostname string `json:"hostname"`
11+
City string `json:"city"`
12+
Region string `json:"region"`
13+
Country string `json:"country"`
14+
CountryName string `json:'-"`
15+
Location string `json:"loc"`
16+
Org string `json:"org"`
17+
Postal string `json:"postal"`
18+
Timezone string `json:"timezone"`
19+
ASN *CoreASN `json:"asn"`
20+
Company *CoreCompany `json:"company"`
21+
Carrier *CoreCarrier `json:"carrier"`
22+
Privacy *CorePrivacy `json:"privacy"`
23+
Abuse *CoreAbuse `json:"abuse"`
24+
Domains *CoreDomains `json:"domains"`
2425
}
2526

2627
// CoreASN represents ASN data for the Core API.
@@ -114,6 +115,11 @@ func (c *Client) GetIPInfo(ip net.IP) (*Core, error) {
114115
return nil, err
115116
}
116117

118+
// map country to full country name
119+
if v.Country != "" {
120+
v.CountryName = countriesMap[v.Country]
121+
}
122+
117123
// cache req result
118124
if c.Cache != nil {
119125
if err := c.Cache.Set(cacheKey, v); err != nil {
@@ -205,6 +211,22 @@ func (c *Client) GetIPCountry(ip net.IP) (string, error) {
205211
return core.Country, nil
206212
}
207213

214+
/* COUNTRY NAME */
215+
216+
// GetIPCountryName returns the full country name for the specified IP.
217+
func GetIPCountryName(ip net.IP) (string, error) {
218+
return DefaultClient.GetIPCountryName(ip)
219+
}
220+
221+
// GetIPCountryName returns the full country name for the specified IP.
222+
func (c *Client) GetIPCountryName(ip net.IP) (string, error) {
223+
core, err := c.GetIPInfo(ip)
224+
if err != nil {
225+
return "", err
226+
}
227+
return core.CountryName, nil
228+
}
229+
208230
/* LOCATION */
209231

210232
// GetIPLocation returns the location for the specified IP.

ipinfo/countries.go

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
package ipinfo
2+
3+
var countriesMap = map[string]string{
4+
"BD": "Bangladesh",
5+
"BE": "Belgium",
6+
"BF": "Burkina Faso",
7+
"BG": "Bulgaria",
8+
"BA": "Bosnia and Herzegovina",
9+
"BB": "Barbados",
10+
"WF": "Wallis and Futuna",
11+
"BL": "Saint Barthelemy",
12+
"BM": "Bermuda",
13+
"BN": "Brunei",
14+
"BO": "Bolivia",
15+
"BH": "Bahrain",
16+
"BI": "Burundi",
17+
"BJ": "Benin",
18+
"BT": "Bhutan",
19+
"JM": "Jamaica",
20+
"BV": "Bouvet Island",
21+
"BW": "Botswana",
22+
"WS": "Samoa",
23+
"BQ": "Bonaire, Saint Eustatius and Saba ",
24+
"BR": "Brazil",
25+
"BS": "Bahamas",
26+
"JE": "Jersey",
27+
"BY": "Belarus",
28+
"BZ": "Belize",
29+
"RU": "Russia",
30+
"RW": "Rwanda",
31+
"RS": "Serbia",
32+
"TL": "East Timor",
33+
"RE": "Reunion",
34+
"TM": "Turkmenistan",
35+
"TJ": "Tajikistan",
36+
"RO": "Romania",
37+
"TK": "Tokelau",
38+
"GW": "Guinea-Bissau",
39+
"GU": "Guam",
40+
"GT": "Guatemala",
41+
"GS": "South Georgia and the South Sandwich Islands",
42+
"GR": "Greece",
43+
"GQ": "Equatorial Guinea",
44+
"GP": "Guadeloupe",
45+
"JP": "Japan",
46+
"GY": "Guyana",
47+
"GG": "Guernsey",
48+
"GF": "French Guiana",
49+
"GE": "Georgia",
50+
"GD": "Grenada",
51+
"GB": "United Kingdom",
52+
"GA": "Gabon",
53+
"SV": "El Salvador",
54+
"GN": "Guinea",
55+
"GM": "Gambia",
56+
"GL": "Greenland",
57+
"GI": "Gibraltar",
58+
"GH": "Ghana",
59+
"OM": "Oman",
60+
"TN": "Tunisia",
61+
"JO": "Jordan",
62+
"HR": "Croatia",
63+
"HT": "Haiti",
64+
"HU": "Hungary",
65+
"HK": "Hong Kong",
66+
"HN": "Honduras",
67+
"HM": "Heard Island and McDonald Islands",
68+
"VE": "Venezuela",
69+
"PR": "Puerto Rico",
70+
"PS": "Palestinian Territory",
71+
"PW": "Palau",
72+
"PT": "Portugal",
73+
"SJ": "Svalbard and Jan Mayen",
74+
"PY": "Paraguay",
75+
"IQ": "Iraq",
76+
"PA": "Panama",
77+
"PF": "French Polynesia",
78+
"PG": "Papua New Guinea",
79+
"PE": "Peru",
80+
"PK": "Pakistan",
81+
"PH": "Philippines",
82+
"PN": "Pitcairn",
83+
"PL": "Poland",
84+
"PM": "Saint Pierre and Miquelon",
85+
"ZM": "Zambia",
86+
"EH": "Western Sahara",
87+
"EE": "Estonia",
88+
"EG": "Egypt",
89+
"ZA": "South Africa",
90+
"EC": "Ecuador",
91+
"IT": "Italy",
92+
"VN": "Vietnam",
93+
"SB": "Solomon Islands",
94+
"ET": "Ethiopia",
95+
"SO": "Somalia",
96+
"ZW": "Zimbabwe",
97+
"SA": "Saudi Arabia",
98+
"ES": "Spain",
99+
"ER": "Eritrea",
100+
"ME": "Montenegro",
101+
"MD": "Moldova",
102+
"MG": "Madagascar",
103+
"MF": "Saint Martin",
104+
"MA": "Morocco",
105+
"MC": "Monaco",
106+
"UZ": "Uzbekistan",
107+
"MM": "Myanmar",
108+
"ML": "Mali",
109+
"MO": "Macao",
110+
"MN": "Mongolia",
111+
"MH": "Marshall Islands",
112+
"MK": "Macedonia",
113+
"MU": "Mauritius",
114+
"MT": "Malta",
115+
"MW": "Malawi",
116+
"MV": "Maldives",
117+
"MQ": "Martinique",
118+
"MP": "Northern Mariana Islands",
119+
"MS": "Montserrat",
120+
"MR": "Mauritania",
121+
"IM": "Isle of Man",
122+
"UG": "Uganda",
123+
"TZ": "Tanzania",
124+
"MY": "Malaysia",
125+
"MX": "Mexico",
126+
"IL": "Israel",
127+
"FR": "France",
128+
"IO": "British Indian Ocean Territory",
129+
"SH": "Saint Helena",
130+
"FI": "Finland",
131+
"FJ": "Fiji",
132+
"FK": "Falkland Islands",
133+
"FM": "Micronesia",
134+
"FO": "Faroe Islands",
135+
"NI": "Nicaragua",
136+
"NL": "Netherlands",
137+
"NO": "Norway",
138+
"NA": "Namibia",
139+
"VU": "Vanuatu",
140+
"NC": "New Caledonia",
141+
"NE": "Niger",
142+
"NF": "Norfolk Island",
143+
"NG": "Nigeria",
144+
"NZ": "New Zealand",
145+
"NP": "Nepal",
146+
"NR": "Nauru",
147+
"NU": "Niue",
148+
"CK": "Cook Islands",
149+
"XK": "Kosovo",
150+
"CI": "Ivory Coast",
151+
"CH": "Switzerland",
152+
"CO": "Colombia",
153+
"CN": "China",
154+
"CM": "Cameroon",
155+
"CL": "Chile",
156+
"CC": "Cocos Islands",
157+
"CA": "Canada",
158+
"CG": "Republic of the Congo",
159+
"CF": "Central African Republic",
160+
"CD": "Democratic Republic of the Congo",
161+
"CZ": "Czech Republic",
162+
"CY": "Cyprus",
163+
"CX": "Christmas Island",
164+
"CR": "Costa Rica",
165+
"CW": "Curacao",
166+
"CV": "Cape Verde",
167+
"CU": "Cuba",
168+
"SZ": "Swaziland",
169+
"SY": "Syria",
170+
"SX": "Sint Maarten",
171+
"KG": "Kyrgyzstan",
172+
"KE": "Kenya",
173+
"SS": "South Sudan",
174+
"SR": "Suriname",
175+
"KI": "Kiribati",
176+
"KH": "Cambodia",
177+
"KN": "Saint Kitts and Nevis",
178+
"KM": "Comoros",
179+
"ST": "Sao Tome and Principe",
180+
"SK": "Slovakia",
181+
"KR": "South Korea",
182+
"SI": "Slovenia",
183+
"KP": "North Korea",
184+
"KW": "Kuwait",
185+
"SN": "Senegal",
186+
"SM": "San Marino",
187+
"SL": "Sierra Leone",
188+
"SC": "Seychelles",
189+
"KZ": "Kazakhstan",
190+
"KY": "Cayman Islands",
191+
"SG": "Singapore",
192+
"SE": "Sweden",
193+
"SD": "Sudan",
194+
"DO": "Dominican Republic",
195+
"DM": "Dominica",
196+
"DJ": "Djibouti",
197+
"DK": "Denmark",
198+
"VG": "British Virgin Islands",
199+
"DE": "Germany",
200+
"YE": "Yemen",
201+
"DZ": "Algeria",
202+
"US": "United States",
203+
"UY": "Uruguay",
204+
"YT": "Mayotte",
205+
"UM": "United States Minor Outlying Islands",
206+
"LB": "Lebanon",
207+
"LC": "Saint Lucia",
208+
"LA": "Laos",
209+
"TV": "Tuvalu",
210+
"TW": "Taiwan",
211+
"TT": "Trinidad and Tobago",
212+
"TR": "Turkey",
213+
"LK": "Sri Lanka",
214+
"LI": "Liechtenstein",
215+
"LV": "Latvia",
216+
"TO": "Tonga",
217+
"LT": "Lithuania",
218+
"LU": "Luxembourg",
219+
"LR": "Liberia",
220+
"LS": "Lesotho",
221+
"TH": "Thailand",
222+
"TF": "French Southern Territories",
223+
"TG": "Togo",
224+
"TD": "Chad",
225+
"TC": "Turks and Caicos Islands",
226+
"LY": "Libya",
227+
"VA": "Vatican",
228+
"VC": "Saint Vincent and the Grenadines",
229+
"AE": "United Arab Emirates",
230+
"AD": "Andorra",
231+
"AG": "Antigua and Barbuda",
232+
"AF": "Afghanistan",
233+
"AI": "Anguilla",
234+
"VI": "U.S. Virgin Islands",
235+
"IS": "Iceland",
236+
"IR": "Iran",
237+
"AM": "Armenia",
238+
"AL": "Albania",
239+
"AO": "Angola",
240+
"AQ": "Antarctica",
241+
"AS": "American Samoa",
242+
"AR": "Argentina",
243+
"AU": "Australia",
244+
"AT": "Austria",
245+
"AW": "Aruba",
246+
"IN": "India",
247+
"AX": "Aland Islands",
248+
"AZ": "Azerbaijan",
249+
"IE": "Ireland",
250+
"ID": "Indonesia",
251+
"UA": "Ukraine",
252+
"QA": "Qatar",
253+
"MZ": "Mozambique",
254+
}

ipinfo/ipinfo.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//go:generate go run gen-fields.go
2-
31
package ipinfo
42

53
// DefaultClient is the package-level client available to the user.

0 commit comments

Comments
 (0)