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

getting species by countries are now unique, not sure if is a part of the assignment

parent bac7e75b
Branches
No related tags found
No related merge requests found
......@@ -33,87 +33,51 @@ type Results struct {
}
func GetCountryByCode(code, limit string) Country {
var x Country
url := countryApi + code
cBody := getString(url)
err := json.Unmarshal([]byte(cBody), &x)
// gets country info
var country Country
urlCountry := countryApi + code
cBody := getString(urlCountry)
err := json.Unmarshal([]byte(cBody), &country)
if err != nil {
log.Fatal(err)
}
/*
resp, err:= http.Get(url)
// gets species in that country
var species Response
urlSpecies := occurrenceApi + "country=" + code + "&limit=" + limit
rBody := getString(urlSpecies)
err = json.Unmarshal([]byte(rBody), &species)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
bodyString := string(bodyBytes)
err = json.Unmarshal([]byte(bodyString), &x)
if err != nil {
log.Fatal(err)
}
//log.Info(bodyString)
// adds specieskeys&specienames to the country struct
// and checks if the countrycode is correct
for i, _ := range species.Results {//i := 0; i < len(species.Results); i++ {
if country.Code == species.Results[i].CountryCode {
if species.Results[i].SpeciesKey != 0 || contains(country.SpeciesKey, species.Results[i].SpeciesKey) {
country.Species = append(country.Species, species.Results[i].Species)
country.SpeciesKey = append(country.SpeciesKey, species.Results[i].SpeciesKey)
}
*/
// #########################################################################################
var y Response
url2 := occurrenceApi + "country=" + 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)
}
return country
bodyString := string(bodyBytes)
err = json.Unmarshal([]byte(bodyString), &y)
if err != nil {
log.Fatal(err)
}
//log.Info(bodyString)
}
*/
rBody := getString(url2)
err = json.Unmarshal([]byte(rBody), &y)
if err != nil {
log.Fatal(err)
func contains(s []int, e int) bool {
for _, a := range s {
if a == e {
return true
}
//adds specieskeys&specienames to the country struct
// NEEDS CHECK IF SPECIES COUNTRY IS CORRECT !!
for i := 0; i < len(y.Results); i++ {
if x.Code == y.Results[i].CountryCode {
x.Species = append(x.Species, y.Results[i].Species)
x.SpeciesKey = append(x.SpeciesKey, y.Results[i].SpeciesKey)
}
return false
}
return x
}
//returns json ready for parsing as string
func getString(url string) string {
resp, err:= http.Get(url)
......@@ -134,6 +98,5 @@ func getString(url string) string {
return "err0r"
}
return bodyString
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment