Skip to content
Snippets Groups Projects
Commit 08495e59 authored by Aksel Baardsen's avatar Aksel Baardsen
Browse files

/country works, but yes no

parent 75349573
Branches
No related tags found
No related merge requests found
......@@ -8,23 +8,32 @@ import (
)
const countryApi = "https://restcountries.eu/rest/v2/alpha/"
const occurrenceApi = "http://api.gbif.org/v1/occurrence/search?"
type Country struct {
Code string `json:"alpha2Code"`
CountryName string `json:"name"`
CountryFlag string `json:"flag"`
Species []Specie `json:"species"`
SpeciesKey []int `json:"specieskey"`
Species []string `json:"species"`
SpeciesKey []int `json:"speciesKey"`
}
type Response struct {
Results []Results `json:"results"`
}
type Results struct {
Species string `json:"species"`
SpeciesKey int `json:"speciesKey"`
}
func GetCountryByCode(code, limit string) Country {
var x Country
url := countryApi + code
if len(limit) > 0 {
url += "?limit=" + limit
}
resp, err:= http.Get(url)
if err != nil {
......@@ -46,14 +55,41 @@ func GetCountryByCode(code, limit string) Country {
}
// #########################################################################################
var y Response
url2 := occurrenceApi + "countryCode=" + code
if len(limit) > 0 {
url2 += "&limit=" + limit
} else {
url2 += "&limit=5"
}
resp2, err := http.Get(url2)
if err != nil {
log.Fatal(err)
}
defer resp2.Body.Close()
if resp2.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(resp2.Body)
if err != nil {
log.Fatal(err)
}
/*
country := &Country{}
err = json.NewDecoder(resp.Body).Decode(country)
bodyString := string(bodyBytes)
err = json.Unmarshal([]byte(bodyString), &y)
if err != nil {
log.Fatal(err)
}*/
}
//log.Info(bodyString)
}
//adds specieskeys&specienames to the country struct
for i := 0; i < len(y.Results); i++ {
x.Species = append(x.Species, y.Results[i].Species)
x.SpeciesKey = append(x.SpeciesKey, y.Results[i].SpeciesKey)
}
return x
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment