diff --git a/cmd/main.go b/cmd/main.go index ee235e3066557fc1000b6860cd97286ddf6c3da2..093cc4a57e7386468e1e5ec1598144f74ff50459 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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)) diff --git a/country.go b/country.go new file mode 100644 index 0000000000000000000000000000000000000000..9638dd7d1027213d6d833b7f4d05393151731c20 --- /dev/null +++ b/country.go @@ -0,0 +1,52 @@ +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) + +} diff --git a/func.go b/func.go index d22b547548723222be6e0d39fae4ed19d1a8f981..6eb5f44e84197e1a2b1485c37db4df88906622ff 100644 --- a/func.go +++ b/func.go @@ -1,10 +1,8 @@ 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) { } diff --git a/species.go b/species.go new file mode 100644 index 0000000000000000000000000000000000000000..c1189c0fda61b97ec0c8c3bad631d7d9c25faa5e --- /dev/null +++ b/species.go @@ -0,0 +1,72 @@ +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) + +} diff --git a/struct.go b/struct.go index dc6d68394499115bd526f6cc12468d5317b100e6..2000a1fb8b1f02edfbe6a7c61eb561393ccff4e3 100644 --- a/struct.go +++ b/struct.go @@ -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