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

working status code prototype

parent e323c379
......@@ -15,7 +15,12 @@ func Chandler(w http.ResponseWriter, r *http.Request) {
limit = "5"
}
country := pkg.GetCountryByCode(vars["country_identifier"], limit)
country, code := pkg.GetCountryByCode(vars["country_identifier"], limit)
w.WriteHeader(code)
c, _ := json.Marshal(country)
_, _ = fmt.Fprintln(w, string(c))
if code == 200 {
_, _ = fmt.Fprintln(w, string(c))
} else {
_, _ = fmt.Fprintln(w, code)
}
}
\ No newline at end of file
......@@ -11,10 +11,15 @@ import (
func Shandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
specie := pkg.GetSpecies(vars["speciesKey"])
specie, code := pkg.GetSpecies(vars["speciesKey"])
w.WriteHeader(code)
s, _ := json.Marshal(specie)
_, _ = fmt.Fprintln(w, string(s))
if code == 200 {
_, _ = fmt.Fprintln(w, string(s))
} else {
_, _ = fmt.Fprintln(w, code)
}
}
......@@ -2,7 +2,7 @@ package pkg
import (
"encoding/json"
"log"
"fmt"
)
const countryApi = "https://restcountries.eu/rest/v2/alpha/"
......@@ -26,7 +26,7 @@ type Results struct {
CountryCode string `json:"countryCode"`
}
func GetCountryByCode(code, limit string) Country {
func GetCountryByCode(code, limit string) (Country, int) {
var country Country
var species Response
......@@ -34,17 +34,23 @@ func GetCountryByCode(code, limit string) Country {
urlSpecies := occurrenceApi + "country=" + code + "&limit=" + limit
// gets country info
cBody := getString(urlCountry)
cBody, status := getString(urlCountry)
if status != 200 {
return country, status
}
err := json.Unmarshal([]byte(cBody), &country)
if err != nil {
log.Fatal(err)
fmt.Println(err)
}
// gets species in that country
rBody := getString(urlSpecies)
rBody, status := getString(urlSpecies)
if status != 200 {
return country, status
}
err = json.Unmarshal([]byte(rBody), &species)
if err != nil {
log.Fatal(err)
fmt.Println(err)
}
// adds species-keys & specie-names to the country struct
......@@ -59,7 +65,7 @@ func GetCountryByCode(code, limit string) Country {
}
}
return country
return country, status
}
......
package pkg
import (
"fmt"
"io/ioutil"
"log"
"net/http"
)
func getString(url string) string {
func getString(url string) (string, int) {
resp, err:= http.Get(url)
if err != nil {
log.Fatal(err)
fmt.Println(err)
}
defer resp.Body.Close()
......@@ -17,14 +17,12 @@ func getString(url string) string {
if resp.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
fmt.Println(err)
}
bodyString = string(bodyBytes)
} else {
log.Fatal(resp.StatusCode)
}
return bodyString
return bodyString, resp.StatusCode
}
......
......@@ -2,7 +2,7 @@ package pkg
import (
"encoding/json"
"log"
"fmt"
)
const speciesApi = "http://api.gbif.org/v1/species/"
......@@ -20,21 +20,27 @@ type Specie struct {
Year string `json:"year"`
}
func GetSpecies(key string) Specie {
func GetSpecies(key string) (Specie, int) {
var specie Specie
url := speciesApi + key
sBody := getString(url)
sBody, code := getString(url)
if code != 200 {
return specie, code
}
err := json.Unmarshal([]byte(sBody), &specie)
if err != nil {
log.Fatal(err)
fmt.Println(err)
}
url = url + "/name"
sBody = getString(url)
sBody, code = getString(url)
if code != 200 {
return specie, code
}
err = json.Unmarshal([]byte(sBody), &specie)
if err != nil {
log.Fatal(err)
fmt.Println(err)
}
return specie
return specie, code
}
\ 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