From 5be4f0af10489219e16310b7f25f70ac3e16b341 Mon Sep 17 00:00:00 2001 From: andmag <andmag@stud.ntnu.no> Date: Wed, 9 Oct 2019 09:19:23 +0200 Subject: [PATCH] diag added, optimalization --- cmd/main.go | 1 + country.go | 35 +++++++++++- diagnostics.go | 33 +++++++++++ func.go | 152 ------------------------------------------------- species.go | 61 ++++++++------------ struct.go | 15 +++-- 6 files changed, 104 insertions(+), 193 deletions(-) create mode 100644 diagnostics.go delete mode 100644 func.go diff --git a/cmd/main.go b/cmd/main.go index 093cc4a..7bf6046 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -19,6 +19,7 @@ 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)) diff --git a/country.go b/country.go index 9638dd7..a33ed32 100644 --- a/country.go +++ b/country.go @@ -38,7 +38,6 @@ func HandlerCountry(w http.ResponseWriter, r *http.Request) { //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) @@ -50,3 +49,37 @@ func HandlerCountry(w http.ResponseWriter, r *http.Request) { replyCountry(w, r, APIURL) } + +/* +// 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, "/") + + APIURL += parts[4] + + http.Header.Add(w.Header(), "content-type", "application/json") + + req, err := http.NewRequest(http.MethodGet, APIURL, 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() + + var species []SpeciesByCountry + //species := []SpeciesByCountry{} + //mySpeciesList := make([]SBCList, 0) + json.NewDecoder(resp.Body).Decode(&species) + json.NewEncoder(w).Encode(species) + +} +*/ diff --git a/diagnostics.go b/diagnostics.go new file mode 100644 index 0000000..89b5bed --- /dev/null +++ b/diagnostics.go @@ -0,0 +1,33 @@ +package assignment1 + +import ( + "encoding/json" + "fmt" + "net/http" +) + +// HandlerNil kjhfkerjhgk +func HandlerNil(w http.ResponseWriter, r *http.Request) { + fmt.Println("Default Handler: Invalid request received.") + http.Error(w, "Invalid request", http.StatusBadRequest) +} + +// HandlerDiag dsjfbgdfjgb +func HandlerDiag(w http.ResponseWriter, r *http.Request) { + http.Header.Add(w.Header(), "content-type", "application/json") + + diagnostics := &Diag{} + diagnostics.Version = "v1" + diagnostics.Restcountries = getStatusCode("https://restcountries.eu/") + diagnostics.Gbif = getStatusCode("http://api.gbif.org/v1/") + + json.NewEncoder(w).Encode(diagnostics) +} + +func getStatusCode(URL string) int { + resp, err := http.Get(URL) + if err != nil { + panic(err) + } + return resp.StatusCode +} diff --git a/func.go b/func.go deleted file mode 100644 index 6eb5f44..0000000 --- a/func.go +++ /dev/null @@ -1,152 +0,0 @@ -package assignment1 - -import ( - "fmt" - "net/http" -) - -// HandlerNil kjhfkerjhgk -func HandlerNil(w http.ResponseWriter, r *http.Request) { - fmt.Println("Default Handler: Invalid request received.") - http.Error(w, "Invalid request", http.StatusBadRequest) -} - -/* -func replyRequest(w http.ResponseWriter, r *http.Request, url string, url2 string, c 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() - - switch c { - case "country": - country := &Country{} - 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) - //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) { - - 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] - - c := "country" - - 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 - -} - -// 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) - -} -*/ - -// HandlerDiag dsjfbgdfjgb -func HandlerDiag(w http.ResponseWriter, r *http.Request) { - -} diff --git a/species.go b/species.go index c1189c0..18102e3 100644 --- a/species.go +++ b/species.go @@ -7,33 +7,35 @@ import ( "strings" ) -func replySpecies(w http.ResponseWriter, r *http.Request, url string, url2 string) { - - species := &Species{} +// HandlerSpecies kdsjfhkdjghlfdkhn +func HandlerSpecies(w http.ResponseWriter, r *http.Request) { http.Header.Add(w.Header(), "content-type", "application/json") - req, err := http.NewRequest(http.MethodGet, url, nil) - if err != nil { - fmt.Println("Error", err) - } + species := &Species{} - client := http.DefaultClient - resp, err := client.Do(req) - if err != nil { - fmt.Println("Error", err) + APIURL := "http://api.gbif.org/v1/species/" + + parts := strings.Split(r.URL.Path, "/") + if len(parts) != 5 || parts[1] != "conservation" || parts[2] != "v1" || parts[3] != "species" || parts[4] == "" { + status := http.StatusBadRequest + http.Error(w, "Expecting format /conservation/v1/species/'speciesNumber'", status) + return } - defer resp.Body.Close() + APIURL += parts[4] + APIURL2 := APIURL + "/name" - json.NewDecoder(resp.Body).Decode(species) + s := "species" + y := "year" - replyWithYear(species, url2) + replySpecies(w, r, APIURL, s, species) + replySpecies(w, r, APIURL2, y, species) json.NewEncoder(w).Encode(species) } -func replyWithYear(species *Species, url string) { +func replySpecies(w http.ResponseWriter, r *http.Request, url string, str string, species *Species) { req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { @@ -48,25 +50,12 @@ func replyWithYear(species *Species, url string) { 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) - + switch str { + case "species": + json.NewDecoder(resp.Body).Decode(&species) + case "year": + year := &Year{} + json.NewDecoder(resp.Body).Decode(&year) + species.Year = year.Year + } } diff --git a/struct.go b/struct.go index 2000a1f..d1653bc 100644 --- a/struct.go +++ b/struct.go @@ -9,6 +9,13 @@ type Country struct { //Speciescategorycount string `json:"` } +/* +type SpeciesByCountry struct { + Species string `json:"species"` + SpeciesKey int `json:"speciesKey"` +} +*/ + // Species comment endpoint type Species struct { Key int `json:"key"` @@ -29,8 +36,8 @@ type Year struct { // Diag comment endpoint type Diag struct { - Gbif string `json:""` - Restcountries string `json:""` - Version string `json:""` - uptime string `json:""` + Gbif int + Restcountries int + Version string + uptime int } -- GitLab