forked from alediaferia/gocountries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
country.go
125 lines (110 loc) · 2.68 KB
/
country.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package gocountries
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
const baseURL = "https://restcountries.eu/rest/v2/%s"
// Country model
type Country struct {
Name string
Capital string
AltSpellings []string
Relevance string
Region string
Subregion string
Translations map[string]string
Population int32
LatLng []float32
Demonym string
Area float32
Gini float32
Timezones []string
Borders []string
NativeName string
CallingCodes []string
TopLevelDomain []string
Alpha2Code string
Alpha3Code string
Currencies []map[string]string
Languages []map[string]string
NumericCode string
Flag string
RegionalBlocs []RegionalBloc
Cioc string
}
// RegionalBloc model
type RegionalBloc struct {
Acronym string
Name string
OtherAcronyms []string
OtherNames []string
}
func doRestcountriesCall(apiSuffix string) ([]byte, error) {
url := fmt.Sprintf(baseURL, apiSuffix)
res, err := http.Get(url)
if err != nil {
return []byte{}, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
e := fmt.Errorf("Unexpected API status code %s", res.Status)
return []byte{}, e
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return []byte{}, err
}
return body, nil
}
func doCountriesByName(name string, isFullName bool) ([]Country, error) {
apiSuffix := "name/%s"
if isFullName {
apiSuffix += "?fullText=true"
}
resData, err := doRestcountriesCall(fmt.Sprintf(apiSuffix, name))
if err != nil {
return nil, err
}
var c []Country
err = json.Unmarshal(resData, &c)
if err != nil {
return nil, err
}
return c, nil
}
// CountriesByName searches for countries by their name. It can be the native name or partial name
func CountriesByName(name string) ([]Country, error) {
return doCountriesByName(name, false)
}
// CountriesByFullName searches for countries by their full name
func CountriesByFullName(name string) ([]Country, error) {
return doCountriesByName(name, true)
}
// CountriesByCapital searches for countries with capital city matching 'name'
func CountriesByCapital(name string) ([]Country, error) {
resData, err := doRestcountriesCall(fmt.Sprintf("capital/%s", name))
if err != nil {
return nil, err
}
var c []Country
err = json.Unmarshal(resData, &c)
if err != nil {
return nil, err
}
return c, nil
}
// AllCountries retrieves every country
func AllCountries() ([]Country, error) {
resData, err := doRestcountriesCall(fmt.Sprintf("all"))
if err != nil {
return nil, err
}
var c []Country
err = json.Unmarshal(resData, &c)
if err != nil {
return nil, err
}
return c, nil
}