Skip to content
Snippets Groups Projects
Commit 77b8239f authored by andmag's avatar andmag
Browse files

Finalizing comments

parent ee17aec5
Branches
No related tags found
No related merge requests found
......@@ -2,7 +2,6 @@ package assignment1
import (
"encoding/json"
"fmt"
"net/http"
"strings"
)
......@@ -16,7 +15,7 @@ func countryRequest(url string, c *http.Client, country *Country) {
// decodes the response into the struct
err := json.NewDecoder(resp.Body).Decode(&country)
if err != nil {
fmt.Println("Error", err.Error())
panic(err)
}
// closes the body
......@@ -35,7 +34,7 @@ func speciesInCountryRequest(url string, c *http.Client, country *Country) {
// decodes the response into the array of results
err := json.NewDecoder(resp.Body).Decode(nameAndKey)
if err != nil {
fmt.Println("Error", err.Error())
panic(err)
}
// closes the body
......@@ -67,6 +66,7 @@ func HandlerCountry(w http.ResponseWriter, r *http.Request) {
urlQuery := r.URL.RawQuery
parts := strings.Split(r.URL.Path, "/")
// checks
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)
......@@ -77,7 +77,7 @@ func HandlerCountry(w http.ResponseWriter, r *http.Request) {
APIURL += parts[4]
APIURL2 := "http://api.gbif.org/v1/occurrence/search?country=" + parts[4] + "&" + urlQuery
//empty country struct
// empty country struct
country := &Country{}
// makes a client
......@@ -90,6 +90,6 @@ func HandlerCountry(w http.ResponseWriter, r *http.Request) {
// encodes everything to the browser
err := json.NewEncoder(w).Encode(country)
if err != nil {
fmt.Println("Error", err.Error())
panic(err)
}
}
......@@ -2,7 +2,6 @@ package assignment1
import (
"encoding/json"
"fmt"
"net/http"
)
......@@ -30,6 +29,6 @@ func HandlerDiag(w http.ResponseWriter, r *http.Request) {
// encodes everything to the browser
err := json.NewEncoder(w).Encode(diagnostics)
if err != nil {
fmt.Println("Error", err.Error())
panic(err)
}
}
......@@ -11,17 +11,17 @@ func HandlerNil(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Invalid request", http.StatusBadRequest)
}
// handles all the requests and returns the response
// handles the requests from the apis and returns the response
func doRequest(url string, c *http.Client) *http.Response {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
fmt.Println("Error", err)
panic(err)
}
resp, err := c.Do(req)
if err != nil {
fmt.Println("Error", err)
panic(err)
}
return resp
......
......@@ -2,7 +2,6 @@ package assignment1
import (
"encoding/json"
"fmt"
"net/http"
"strings"
)
......@@ -16,7 +15,7 @@ func speciesRequest(url string, c *http.Client, species *Species) {
// decodes the response into the struct
err := json.NewDecoder(resp.Body).Decode(&species)
if err != nil {
fmt.Println("Error", err.Error())
panic(err)
}
// closes the body
......@@ -35,7 +34,7 @@ func yearRequest(url string, c *http.Client, species *Species) {
// decodes the response into the struct
err := json.NewDecoder(resp.Body).Decode(&year)
if err != nil {
fmt.Println("Error", err.Error())
panic(err)
}
// puts the year from the year struct into the species struct
......@@ -79,6 +78,6 @@ func HandlerSpecies(w http.ResponseWriter, r *http.Request) {
// encodes species which now contains year as well
err := json.NewEncoder(w).Encode(species)
if err != nil {
fmt.Println("Error", err.Error())
panic(err)
}
}
......@@ -38,7 +38,7 @@ type Year struct {
Year string `json:"bracketYear"`
}
// Diag struct:
// Diag struct: struct for diagnostics
type Diag struct {
Gbif int
Restcountries int
......
......@@ -4,7 +4,8 @@ import (
"time"
)
var startTime time.Time // to measure the time since the last service restart
// to measure the time since the last service restart
var startTime time.Time
// Uptime function: returns time since start time in seconds
func Uptime() float64 {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment