Skip to content
Snippets Groups Projects
Commit 1c48f086 authored by andmag's avatar andmag
Browse files

got year in species, structured code differently

parent d4e18109
Branches
No related tags found
No related merge requests found
......@@ -18,7 +18,7 @@ func main() {
http.HandleFunc("/", assignment1.HandlerNil)
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/diag/", assignment1.HandlerDiag)
fmt.Println("Listening on port " + port)
log.Fatal(http.ListenAndServe(":"+port, nil))
......
package assignment1
import (
"encoding/json"
"fmt"
"net/http"
"strings"
)
func replyCountry(w http.ResponseWriter, r *http.Request, url string) {
http.Header.Add(w.Header(), "content-type", "application/json")
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
fmt.Println("Error", err)
}
client := http.DefaultClient
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error", err)
}
defer resp.Body.Close()
country := &Country{}
json.NewDecoder(resp.Body).Decode(country)
json.NewEncoder(w).Encode(country)
}
// HandlerCountry dskjfhskjfhk
func HandlerCountry(w http.ResponseWriter, r *http.Request) {
APIURL := "https://restcountries.eu/rest/v2/alpha/"
//SJEKK LENGDE 4
parts := strings.Split(r.URL.Path, "/")
if len(parts) != 5 || parts[1] != "conservation" || parts[2] != "v1" || parts[3] != "country" || parts[4] == "" {
status := http.StatusBadRequest
http.Error(w, "Expecting format /conservation/v1/country/'AlphaCode'", status)
return
}
APIURL += parts[4]
replyCountry(w, r, APIURL)
}
package assignment1
import (
"encoding/json"
"fmt"
"net/http"
"strings"
)
// HandlerNil kjhfkerjhgk
......@@ -13,7 +11,8 @@ func HandlerNil(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Invalid request", http.StatusBadRequest)
}
func replyRequest(w http.ResponseWriter, r *http.Request, url string, c string) {
/*
func replyRequest(w http.ResponseWriter, r *http.Request, url string, url2 string, c string) {
http.Header.Add(w.Header(), "content-type", "application/json")
......@@ -36,13 +35,25 @@ func replyRequest(w http.ResponseWriter, r *http.Request, url string, c string)
json.NewDecoder(resp.Body).Decode(country)
json.NewEncoder(w).Encode(country)
case "species":
species := &Species{}
year := &Year{}
species.Year = year.Year
json.NewDecoder(resp.Body).Decode(species)
json.NewEncoder(w).Encode(species)
//both := &Both{species.Key, species.Kingdom, species.Phylum, species.Order, species.Family, species.Genus, species.ScientificName, species.CanonicalName, year.Year}
both := dealWithYear(species, url2)
json.NewEncoder(w).Encode(year)
}
}
*/
/*
// HandlerCountry dskjfhskjfhk
func HandlerCountry(w http.ResponseWriter, r *http.Request) {
......@@ -53,19 +64,69 @@ func HandlerCountry(w http.ResponseWriter, r *http.Request) {
parts := strings.Split(r.URL.Path, "/")
/*
if len(parts) != 5 || parts[1] != "conservation" || parts[2] != "v1" || parts[3] != "country" || parts[4] != "" {
status := http.StatusBadRequest
http.Error(w, "Expecting format /conservation/v1/country/'AlphaCode'", status)
return
}
*/
if len(parts) != 5 || parts[1] != "conservation" || parts[2] != "v1" || parts[3] != "country" || parts[4] == "" {
status := http.StatusBadRequest
http.Error(w, "Expecting format /conservation/v1/country/'AlphaCode'", status)
return
}
APIURL += parts[4]
c := "country"
replyRequest(w, r, APIURL, c)
replyRequest(w, r, APIURL, "", c)
}
*/
/*
func replySpecies(w http.ResponseWriter, r *http.Request, url string, url2 string) {
species := &Species{}
http.Header.Add(w.Header(), "content-type", "application/json")
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
fmt.Println("Error", err)
}
client := http.DefaultClient
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error", err)
}
defer resp.Body.Close()
json.NewDecoder(resp.Body).Decode(species)
replyWithYear(species, url2)
json.NewEncoder(w).Encode(species)
}
func replyWithYear(species *Species, url string) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
fmt.Println("Error", err)
}
client := http.DefaultClient
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error", err)
}
defer resp.Body.Close()
year := &Year{}
json.NewDecoder(resp.Body).Decode(&year)
species.Year = year.Year
}
......@@ -78,8 +139,14 @@ func HandlerSpecies(w http.ResponseWriter, r *http.Request) {
parts := strings.Split(r.URL.Path, "/")
APIURL += parts[4]
c := "species"
APIURL2 := APIURL + "/name"
replySpecies(w, r, APIURL, APIURL2)
}
*/
replyRequest(w, r, APIURL, c)
// HandlerDiag dsjfbgdfjgb
func HandlerDiag(w http.ResponseWriter, r *http.Request) {
}
package assignment1
import (
"encoding/json"
"fmt"
"net/http"
"strings"
)
func replySpecies(w http.ResponseWriter, r *http.Request, url string, url2 string) {
species := &Species{}
http.Header.Add(w.Header(), "content-type", "application/json")
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
fmt.Println("Error", err)
}
client := http.DefaultClient
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error", err)
}
defer resp.Body.Close()
json.NewDecoder(resp.Body).Decode(species)
replyWithYear(species, url2)
json.NewEncoder(w).Encode(species)
}
func replyWithYear(species *Species, url string) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
fmt.Println("Error", err)
}
client := http.DefaultClient
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error", err)
}
defer resp.Body.Close()
year := &Year{}
json.NewDecoder(resp.Body).Decode(&year)
species.Year = year.Year
}
// HandlerSpecies dfkjekjgh
func HandlerSpecies(w http.ResponseWriter, r *http.Request) {
APIURL := "http://api.gbif.org/v1/species/"
//SJEKK LENGDE 4
parts := strings.Split(r.URL.Path, "/")
APIURL += parts[4]
APIURL2 := APIURL + "/name"
replySpecies(w, r, APIURL, APIURL2)
}
......@@ -11,16 +11,20 @@ type Country struct {
// Species comment endpoint
type Species 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"`
//SpeciesName string `json:"genericName"`
Key int `json:"key"`
Kingdom string `json:"kingdom"`
Phylum string `json:"phylum"`
Order string `json:"order"`
Family string `json:"family"`
Genus string `json:"genus"`
ScientificName string `json:"scientificName"`
CanonicalName string `json:"canonicalName"`
//Extinct bool `json:"extinct"`
Year string `json:"year"`
}
// Year lkerjgflkjtgj
type Year struct {
Year string `json:"bracketYear"`
}
// Diag comment endpoint
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment