Commit 0efb7cad authored by Aksel Baardsen's avatar Aksel Baardsen
Browse files

fixed hardcoded status codes && comments && logic

parent 2247f095
......@@ -11,7 +11,7 @@ import (
func Chandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
limit := r.FormValue("limit")
limit := r.FormValue("limit") // gets the limit, if specified
if len(limit) == 0 {
limit = "5"
}
......@@ -19,7 +19,7 @@ func Chandler(w http.ResponseWriter, r *http.Request) {
country, code := pkg.GetCountryByCode(vars["country_identifier"], limit)
c, _ := json.Marshal(country)
if code == 200 {
if code == http.StatusOK {
w.Header().Set("Content-Type", "application/json")
_, err := fmt.Fprintln(w, string(c))
if err != nil {
......
......@@ -2,6 +2,7 @@ package pkg
import (
"log"
"net/http"
)
const countryApi = "https://restcountries.eu/rest/v2/alpha/"
......@@ -37,28 +38,32 @@ func GetCountryByCode(code, limit string) (Country, int) {
if err != nil {
log.Fatal(err)
}
if resp.StatusCode != http.StatusOK {
return Country{}, http.StatusBadGateway
}
Unfold(&country, resp)
// gets species in that country
resp, err = getBody(urlSpecies)
Unfold(&species, resp)
if err != nil {
log.Fatal(err)
}
if resp.StatusCode != http.StatusOK {
return Country{}, http.StatusBadGateway
}
Unfold(&species, resp)
// adds species-keys & specie-names to the country struct
// adds species-keys & specie-names to the country struct, ensuring no dupes
check := make(map[Results]bool)
for i, _ := range species.Results {
if check[species.Results[i]] {
continue //[continues the loop] replace check with ! and add content from below
if !check[species.Results[i]] {
check[species.Results[i]] = true
country.SpeciesKey = append(country.SpeciesKey, species.Results[i].SpeciesKey)
country.Species = append(country.Species, species.Results[i].Species)
}
check[species.Results[i]] = true
country.SpeciesKey = append(country.SpeciesKey, species.Results[i].SpeciesKey)
country.Species = append(country.Species, species.Results[i].Species)
}
return country, 200
return country, http.StatusOK
}
......
......@@ -47,24 +47,4 @@ func getBody(url string) (*http.Response, error) {
}
return resp, nil
/*
var bodyString string
if resp.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
bodyString = string(bodyBytes)
}
return bodyString, resp.StatusCode
*/
}
/*
funcs for testing purposes
func GetString(url string) (string, int) {
return getString(url)
}*/
\ No newline at end of file
}
\ No newline at end of file
package pkg
import "log"
import (
"log"
"net/http"
)
const speciesApi = "http://api.gbif.org/v1/species/"
......@@ -20,32 +23,25 @@ type Specie struct {
func GetSpecies(key string) (Specie, int) {
var specie Specie
url := speciesApi + key
resp, err := getBody(url)
if err != nil {
log.Fatal(err)
}
Unfold(&specie, resp)
/*
err := json.Unmarshal([]byte(sBody), &specie)
if err != nil {
log.Fatal(err)
if resp.StatusCode != http.StatusOK {
return Specie{}, http.StatusBadGateway
}
*/
Unfold(&specie, resp)
url = url + "/name"
resp, err = getBody(url)
if err != nil {
log.Fatal(err)
}
Unfold(&specie, resp)
/*
err = json.Unmarshal([]byte(sBody), &specie)
if err != nil {
log.Fatal(err)
if resp.StatusCode != http.StatusOK {
return Specie{}, http.StatusBadGateway
}
*/
Unfold(&specie, resp)
return specie, 200
return specie, http.StatusOK
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment