Skip to content
Snippets Groups Projects
Commit 0350ff2a authored by Herman Andersen Dyrkorn's avatar Herman Andersen Dyrkorn
Browse files

commenting an chaging some errors

parent eb03b69d
No related branches found
No related tags found
No related merge requests found
......@@ -9,8 +9,9 @@ import (
)
func main() {
//start clock for diagnostics
//start timer
assignment1.InitTime()
//trying to get portnumber
port := os.Getenv("PORT")
if port == "" {
port = "8080"
......
......@@ -2,7 +2,6 @@ package assignment1
import (
"encoding/json"
"fmt"
"net/http"
"strings"
)
......@@ -12,25 +11,34 @@ func CountryHandler(w http.ResponseWriter, r *http.Request) {
http.Header.Add(w.Header(), "content-type", "application/json")
APIURL := "https://restcountries.eu/rest/v2/alpha/"
//gets the query from the url the user writes
urlQuery := r.URL.RawQuery
//splitting the url path that the user writes
parts := strings.Split(r.URL.Path, "/")
//checking if the path is right
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?limit='integer'", status)
return
}
APIURL += parts[4]
//setting up a client and a empty country strct
client := http.DefaultClient
APIURL += parts[4]
country := &Country{}
//do a request on the APIURL and decode the response to the countrystruct
resp := GetTheRequest(APIURL, client)
country := &Country{}
DecodeCountry(country, resp, w)
//creating a new URL to get species and species keys
NEWURL := "http://api.gbif.org/v1/occurrence/search?country=" + country.Alpha2Code + "&" + urlQuery
//do a request on the new url and decodes the response into the countrystruct
resp2 := GetTheRequest(NEWURL, client)
DecodeKeyAndSpecies(country, resp2)
//encode countrystruct back to the user
json.NewEncoder(w).Encode(country)
}
......@@ -38,8 +46,9 @@ func CountryHandler(w http.ResponseWriter, r *http.Request) {
//DecodeCountry function
func DecodeCountry(country *Country, resp *http.Response, w http.ResponseWriter) {
json.NewDecoder(resp.Body).Decode(&country)
if country.Alpha2Code == "" {
//checking if there is any errors from the decoding
err := json.NewDecoder(resp.Body).Decode(&country)
if err != nil {
status := http.StatusBadRequest
http.Error(w, "Expected a country, got nothing", status)
return
......@@ -49,19 +58,21 @@ func DecodeCountry(country *Country, resp *http.Response, w http.ResponseWriter)
//DecodeKeyAndSpecies function
func DecodeKeyAndSpecies(country *Country, resp *http.Response) {
var speciesKeyAndSpecies = &Results{}
//creating a empty resultstruct and decodes it
speciesKeyAndSpecies := &Results{}
json.NewDecoder(resp.Body).Decode(speciesKeyAndSpecies)
var species []string
var spesiesKey []int
for k, v := range speciesKeyAndSpecies.Result {
fmt.Println(k)
//loop that puts all the decoded values into two arrays that contains keys and speciesname
for _, v := range speciesKeyAndSpecies.Result {
species = append(species, v.Specie)
spesiesKey = append(spesiesKey, v.Key)
}
//copy keys and speciesname into country
country.SpeciesKey = spesiesKey
country.Species = species
......
......@@ -10,27 +10,34 @@ import (
func DiagHandler(w http.ResponseWriter, r *http.Request) {
http.Header.Add(w.Header(), "content-type", "application/json")
//splitting the path into smaller parts
parts := strings.Split(r.URL.Path, "/")
//checks that the path is right
if len(parts) != 5 || parts[1] != "conservation" || parts[2] != "v1" || parts[3] != "diag" {
status := http.StatusBadRequest
http.Error(w, "Expecting format /conservation/v1/diag/", status)
return
}
//creating a diagnostics struct
diag := &Diag{}
//filling the diagstruct with info
diag.Uptime = Uptime()
diag.Version = "V1"
diag.RestCountry = GetStatusCode("https://restcountries.eu")
diag.GBIF = GetStatusCode("http://api.gbif.org/v1/")
//encode the struct back to the user
json.NewEncoder(w).Encode(diag)
}
//GetStatusCode function
func GetStatusCode(URL string) int {
//getrequest on a given url
resp, err := http.Get(URL)
if err != nil {
panic(err)
}
//returning the statuscode of the response
return resp.StatusCode
}
......@@ -4,21 +4,23 @@ import (
"net/http"
)
//GetTheRequest function
//GetTheRequest function returns a respons from the api
func GetTheRequest(URL string, client *http.Client) *http.Response {
//do a request on the url
req, err := http.NewRequest(http.MethodGet, URL, nil)
if err != nil {
panic(err)
}
//getting response from the client
resp, err := client.Do(req)
if err != nil {
panic(err)
}
//returning the response
return resp
}
//HandlerNil function
//HandlerNil function that deals with the / endpoint
func HandlerNil(w http.ResponseWriter, r *http.Request) {
//fmt.Println("Default Handler: Invalid request received.")
http.Error(w, "Invalid request", http.StatusBadRequest)
http.Error(w, "Invalid request, expected formating conservation/v1/diag or country or species", http.StatusBadRequest)
}
......@@ -11,26 +11,35 @@ func SpeciesHandler(w http.ResponseWriter, r *http.Request) {
http.Header.Add(w.Header(), "content-type", "application/json")
APIURL := "http://api.gbif.org/v1/species/"
//splitting the path into smaller parts
parts := strings.Split(r.URL.Path, "/")
//checks that the path is right
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/'Spiciesnumber'", status)
return
}
APIURL += parts[4]
client := http.DefaultClient
//setting up a client and does a response
client := http.DefaultClient
resp := GetTheRequest(APIURL, client)
//appending "name" to the APRURL and does a new response
APIURL += "/name"
resp2 := GetTheRequest(APIURL, client)
//creating a species struct and a speciesyear struct
species := &Species{}
year := &SpeciesYear{}
//decode species and speciesyear
DecodeSpecies(species, resp, w)
DecodeYear(resp2, year)
//copy over year into speciesstruct and encode it back to user
species.Year = year.Year
json.NewEncoder(w).Encode(species)
......@@ -38,9 +47,9 @@ func SpeciesHandler(w http.ResponseWriter, r *http.Request) {
//DecodeSpecies function
func DecodeSpecies(species *Species, resp *http.Response, w http.ResponseWriter) {
json.NewDecoder(resp.Body).Decode(species)
if species.Key == 0 {
//checking if there is any errors from the decoding
err := json.NewDecoder(resp.Body).Decode(species)
if err != nil {
status := http.StatusBadRequest
http.Error(w, "Expected a species, got nothing", status)
return
......@@ -50,6 +59,5 @@ func DecodeSpecies(species *Species, resp *http.Response, w http.ResponseWriter)
//DecodeYear function
func DecodeYear(resp *http.Response, year *SpeciesYear) {
json.NewDecoder(resp.Body).Decode(year)
}
......@@ -2,14 +2,17 @@ package assignment1
import "time"
//variable that keeps track of time
var startTime time.Time
//Uptime function
func Uptime() float64 {
//return the time since start as seconds
return time.Since(startTime).Seconds()
}
//InitTime function
func InitTime() {
//starts the timer
startTime = time.Now()
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment