Commit 8e241e2c authored by Aksel Baardsen's avatar Aksel Baardsen
Browse files

Added comments for pkg/*

parent 1e5bd626
......@@ -3,6 +3,7 @@ package pkg
const countryApi = "https://restcountries.eu/rest/v2/alpha/"
const occurrenceApi = "http://api.gbif.org/v1/occurrence/search?"
// country struct that will be returned to client
type Country struct {
Code string `json:"code,omitempty"`
CountryName string `json:"countryname,omitempty"`
......@@ -11,22 +12,25 @@ type Country struct {
SpeciesKey []int `json:"speciesKey,omitempty"`
}
// country struct used for parsing http.response
type tempCountry struct {
Code string `json:"alpha2code"`
Flag string `json:"flag"`
Name string `json:"name"`
}
// struct used to get array of species
type response struct {
Results []results `json:"results,omitempty"`
}
// array for the creation of species
type results struct {
Species string `json:"species,omitempty"`
SpeciesKey int `json:"speciesKey,omitempty"`
CountryCode string `json:"countryCode,omitempty"`
}
// returns country information & species based on country-code and limit
func GetCountryByCode(code, limit string) (Country, error) {
var country Country
......@@ -43,10 +47,11 @@ func GetCountryByCode(code, limit string) (Country, error) {
// add tmp info to country struct
country.CountryFlag = tmpC.Flag
country.CountryName = tmpC.Name
country.Code = tmpC.Code // was passed with the func call
country.Code = tmpC.Code // used to get 2-letter ISO standard code (uppercase)
urlSpecies := occurrenceApi + "country=" + country.Code + "&limit=" + limit
// gets species in specified country
// gets species in specified country with limit applied
err = getBody(urlSpecies, &species)
if err != nil {
return Country{}, err
......
......@@ -5,6 +5,7 @@ import (
"net/http"
"time"
)
const gbifApi = "http://api.gbif.org/v1/"
const restApi = "https://restcountries.eu/rest/v2"
......@@ -14,6 +15,7 @@ func init() {
startTime = time.Now()
}
// struct for returning requested data
type Diag struct {
Gbif int `json:"gbif,omitempty"`
Restcountries int `json:"restcountries,omitempty"`
......@@ -21,11 +23,15 @@ type Diag struct {
Uptime int `json:"uptime,omitempty"`
}
// sets up the struct above
func GetDiag(d* Diag) error {
// gets status of GBIF api, returns appropriate error
if err := getGbifStatus(d); err != nil{
return fmt.Errorf("error occured while contacting GBIF API: %s", err)
}
// gets status of restcountries api, returns appropriate error
if err := getRestStatus(d); err != nil {
return fmt.Errorf("error occured while contacting Restcountries API: %s", err)
}
......@@ -33,6 +39,7 @@ func GetDiag(d* Diag) error {
d.Version = "v1"
getUptime(d)
// all went well
return nil
}
......
......@@ -36,7 +36,7 @@ func (s *Specie) unmarshal(resp *http.Response) error {
return err
}
// parses httpresponse as a string used later for parsing
// parses http.response as a string used later for parsing
func getBody(url string, m mashup) error {
// creates a client to allow for timeout
......@@ -65,7 +65,7 @@ func getBody(url string, m mashup) error {
return nil
}
// func for returning correct http.Head response code
// func for returning correct errors & response code
func HttpError(w http.ResponseWriter, err error) {
// if timeout happened
......@@ -75,10 +75,12 @@ func HttpError(w http.ResponseWriter, err error) {
}
switch err.Error() {
case "400", "404":
http.Error(w, "Not found", http.StatusBadGateway)
case "400":
http.Error(w, "Bad Request: " + err.Error(), http.StatusBadRequest)
case "404":
http.Error(w, "Not found: " + err.Error(), http.StatusNotFound)
default:
http.Error(w, "Error occurred", http.StatusInternalServerError)
http.Error(w, "Error occurred: " + err.Error(), http.StatusInternalServerError)
}
}
......@@ -2,6 +2,7 @@ package pkg
const speciesApi = "http://api.gbif.org/v1/species/"
// struct for returning correct json keys
type Specie struct {
Key int `json:"key,omitempty"`
Kingdom string `json:"kingdom,omitempty"`
......@@ -14,10 +15,12 @@ type Specie struct {
Year string `json:"year,omitempty"`
}
// gets all relevant species information
func GetSpecies(key string) (Specie, error) {
var specie Specie
url := speciesApi + key
// fills in the body of the specie, returns error & empty specie when appropriate
err := getBody(url, &specie)
if err != nil {
return Specie{}, err
......@@ -25,12 +28,18 @@ func GetSpecies(key string) (Specie, error) {
// supplies the json keys not provided in the first request (year)
url = url + "/name"
// using a temp-Specie to only get 'year', not replacing any other variable (just in case of varying info)
// I interpreted the task as only asking for 'year', not 'bracketYear' as well
var tempSpecie Specie
err = getBody(url, &tempSpecie)
if err != nil {
return Specie{}, err
}
// sets year (if it occurred; has ",omitifempty" in json format)
specie.Year = tempSpecie.Year
// all went well
return specie, nil
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment