Commit 59b7af59 authored by Aksel Baardsen's avatar Aksel Baardsen
Browse files

A start on species

parent d03640bd
......@@ -2,6 +2,7 @@ package main
import (
"assignment-1/models"
"encoding/json"
"fmt"
"log"
"net/http"
......@@ -48,6 +49,14 @@ func cHandler(w http.ResponseWriter, r *http.Request) {
}
func sHandler(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprintln(w, "aiaiai")
//_, _ = fmt.Fprintln(w, "aiaiai")
vars := mux.Vars(r)
s := models.GetSpecies(vars["speciesKey"])
fmt.Fprintln(w, s) //plain keys
fmt.Fprintln(w, json.NewEncoder(w).Encode(s))
}
\ No newline at end of file
......@@ -5,7 +5,7 @@ import (
"net/http"
)
const codeAPI = "https://restcountries.eu/rest/v2/alpha/"
const countryApi = "https://restcountries.eu/rest/v2/alpha/"
type Country struct {
......@@ -19,7 +19,7 @@ type Country struct {
func GetCountryByCode(code string) Country {
resp, err:= http.Get(codeAPI + code + "/")
resp, err:= http.Get(countryApi + code + "/")
if err != nil {
// handle pls
}
......
package models
import (
"encoding/json"
"net/http"
)
const speciesApi = "http://api.gbif.org/v1/species/"
type Specie struct {
Key int `json:"key"`
Kingdom string `json:"kingdom"`
Phylum string `json:"phylum"`
Order string `json:"order"`
Family string `json:"family"`
Genus string `json:"genus"`
Species string `json:"species"`
ScientificName string `json:"scientificName"`
CanonicalName string `json:"canonicalName"`
extinct bool `json:"extinct"`
Key int `json:"key"`
Kingdom string `json:"kingdom"`
Phylum string `json:"phylum"`
Order string `json:"order"`
Family string `json:"family"`
Genus string `json:"genus"`
Species string `json:"species"`
ScientificName string `json:"scientificName"`
CanonicalName string `json:"canonicalName"`
extinct bool `json:"extinct"`
}
func GetSpecies(key string) Specie {
resp, err:= http.Get(speciesApi + key + "/")
if err != nil {
// handle pls
}
specie := &Specie{}
err = json.NewDecoder(resp.Body).Decode(specie)
if err != nil {
// handle pls
}
defer resp.Body.Close()
return *specie
}
\ 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