Commit 075fea9a authored by Aksel Baardsen's avatar Aksel Baardsen
Browse files

models function.go file, getSpecies works (janky)

parent e0a96817
......@@ -9,6 +9,7 @@ import (
"net/http"
)
//returns json ready for parsing as string
func main() {
r := mux.NewRouter()
......@@ -67,10 +68,9 @@ func sHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
s := models.GetSpecies(vars["speciesKey"])
specie := models.GetSpecies(vars["speciesKey"])
fmt.Fprintln(w, s) //plain keys
fmt.Fprintln(w, json.NewEncoder(w).Encode(s))
s, _ := json.Marshal(specie)
fmt.Fprintln(w, string(s))
}
\ No newline at end of file
......@@ -2,9 +2,7 @@ package models
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
)
const countryApi = "https://restcountries.eu/rest/v2/alpha/"
......@@ -34,8 +32,8 @@ type Results struct {
func GetCountryByCode(code, limit string) Country {
// gets country info
var country Country
// gets country info
urlCountry := countryApi + code
cBody := getString(urlCountry)
err := json.Unmarshal([]byte(cBody), &country)
......@@ -68,36 +66,5 @@ func GetCountryByCode(code, limit string) Country {
}
func contains(s []int, e int) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
//returns json ready for parsing as string
func getString(url string) string {
resp, err:= http.Get(url)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
var bodyString string
if resp.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
return "err0r"
}
bodyString = string(bodyBytes)
} else {
return "err0r"
}
return bodyString
}
\ No newline at end of file
package models
import (
"io/ioutil"
"log"
"net/http"
)
func getString(url string) string {
resp, err:= http.Get(url)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
var bodyString string
if resp.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
return "err0r"
}
bodyString = string(bodyBytes)
} else {
return "err0r"
}
return bodyString
}
func contains(s []int, e int) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
\ No newline at end of file
......@@ -2,7 +2,7 @@ package models
import (
"encoding/json"
"net/http"
"log"
)
const speciesApi = "http://api.gbif.org/v1/species/"
......@@ -18,24 +18,43 @@ type Specie struct {
Species string `json:"species"`
ScientificName string `json:"scientificName"`
CanonicalName string `json:"canonicalName"`
extinct bool `json:"extinct"`
Year string `json:"year"`
}
func GetSpecies(key string) Specie {
resp, err:= http.Get(speciesApi + key + "/")
var specie Specie
url := speciesApi + key
sBody := getString(url)
err := json.Unmarshal([]byte(sBody), &specie)
if err != nil {
// handle pls
log.Fatal(err)
}
url = url + "/name"
sBody = getString(url)
err = json.Unmarshal([]byte(sBody), &specie)
if err != nil {
log.Fatal(err)
}
/*
resp, err:= http.Get(speciesApi + key)
if err != nil {
log.Fatal(err)
}
specie := &Specie{}
err = json.NewDecoder(resp.Body).Decode(specie)
if err != nil {
// handle pls
}
defer resp.Body.Close()
*/
return *specie
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