Skip to content
Snippets Groups Projects
Commit 5abe3c27 authored by andmag's avatar andmag
Browse files

species and speciesKey is in country struct

parent f2a57b75
No related branches found
No related tags found
No related merge requests found
......@@ -19,7 +19,6 @@ func main() {
http.HandleFunc("/conservation/v1/country/", assignment1.HandlerCountry)
http.HandleFunc("/conservation/v1/species/", assignment1.HandlerSpecies)
http.HandleFunc("/conservation/v1/diag/", assignment1.HandlerDiag)
//http.HandleFunc("/conservation/v1/species2/", assignment1.HandlerOnlySpecies)
fmt.Println("Listening on port " + port)
log.Fatal(http.ListenAndServe(":"+port, nil))
......
......@@ -7,7 +7,7 @@ import (
"strings"
)
func replyCountry(w http.ResponseWriter, r *http.Request, url string) {
func replyCountry(w http.ResponseWriter, r *http.Request, url string, url2 string) {
http.Header.Add(w.Header(), "content-type", "application/json")
......@@ -26,6 +26,9 @@ func replyCountry(w http.ResponseWriter, r *http.Request, url string) {
country := &Country{}
json.NewDecoder(resp.Body).Decode(country)
replySpeciesCountry(w, r, url2, country)
json.NewEncoder(w).Encode(country)
}
......@@ -45,29 +48,16 @@ func HandlerCountry(w http.ResponseWriter, r *http.Request) {
}
APIURL += parts[4]
APIURL2 := "http://api.gbif.org/v1/occurrence/search?country="
APIURL2 += parts[4]
replyCountry(w, r, APIURL)
replyCountry(w, r, APIURL, APIURL2)
}
// HandlerOnlySpecies skjdfhkshfkl
func HandlerOnlySpecies(w http.ResponseWriter, r *http.Request) {
APIURL := "http://api.gbif.org/v1/occurrence/search?country="
parts := strings.Split(r.URL.Path, "/")
if len(parts) != 5 || parts[1] != "conservation" || parts[2] != "v1" || parts[3] != "spec" || parts[4] == "" {
status := http.StatusBadRequest
http.Error(w, "Expecting format /conservation/v1/spec/'alphacode'", status)
return
}
APIURL += parts[4]
func replySpeciesCountry(w http.ResponseWriter, r *http.Request, url string, country *Country) {
http.Header.Add(w.Header(), "content-type", "application/json")
req, err := http.NewRequest(http.MethodGet, APIURL, nil)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
fmt.Println("Error", err)
}
......@@ -82,13 +72,9 @@ func HandlerOnlySpecies(w http.ResponseWriter, r *http.Request) {
type Result Results
var name = &Result{}
//v, _ := ioutil.ReadAll(resp.Body)
//fmt.Println("Body: ", string(v))
var nameAndKey = &Result{}
err2 := json.NewDecoder(resp.Body).Decode(name)
err2 := json.NewDecoder(resp.Body).Decode(nameAndKey)
if err2 != nil {
fmt.Println("Error", err2.Error())
}
......@@ -96,30 +82,14 @@ func HandlerOnlySpecies(w http.ResponseWriter, r *http.Request) {
var spec []string
var specKey []int
for k, v := range name.Res1 {
for k, v := range nameAndKey.Res1 {
fmt.Println(k)
fmt.Println(v.Species)
spec = append(spec, v.Species)
specKey = append(specKey, v.SpeciesKey)
fmt.Println(v.SpeciesKey)
/*for k1, v1 := range v {
fmt.Println(k1, v1)
}*/
fmt.Println(v)
}
fmt.Println(spec)
result := Results2{}
result.Res2 = spec
result.Res3 = specKey
err3 := json.NewEncoder(w).Encode(result)
if err3 != nil {
fmt.Println("error2", err3.Error())
}
country.Species = spec
country.SpeciesKey = specKey
}
......@@ -5,30 +5,18 @@ type Country struct {
Code string `json:"alpha2Code"`
Countryname string `json:"name"`
Countryflag string `json:"flag"`
// Species []Spec
// SpeciesKey []SpecKey
Species []string `json:"species"`
SpeciesKey []int `json:"speciesKey"`
}
// Name jdhgkjdng
type Name struct {
type Both struct {
Species string `json:"species"`
SpeciesKey int `json:"speciesKey"`
}
/*
type Key struct {
//SpeciesKey int `json:"speciesKey`
}
*/
type Results2 struct {
Res2 []string `json:"species"`
Res3 []int `json:"speciesKey"`
}
type Results struct {
Res1 []Name `json:"results"`
//Res2 []Key
Res1 []Both `json:"results"`
}
// Species comment endpoint
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment