Commit 7bab0a68 authored by Fredrik Lønnemo's avatar Fredrik Lønnemo
Browse files

i realise the country handler is not correct

parent 40929584
......@@ -8,12 +8,6 @@ import (
"os"
)
/* the three genders
/conservation/v1/country/
/conservation/v1/species/
/conservation/v1/diag/
*/
func main() {
port := os.Getenv("PORT")
......
package internal
import (
"fmt"
"net/http"
"encoding/json"
"strings"
"time"
)
// Version states latest version
// Version states current version
var Version = "v1"
// Country yes haha
// Country struct holds data for a country and its species, not done
type Country struct{
Code string `json:"alpha2code"`
Countryname string `json:"name"`
CountryFlag string `json:"flag"`
Species []string `json:"results"`
SpeciesKey []string `json:"facets"`
SpeciesKey []string `json:"results"`
}
// Species TODO
// Species holds the data of a species
type Species struct {
Key int `json:"key"`
Kingdom string `json:"kingdom"`
......@@ -33,7 +32,7 @@ type Species struct {
Year string `json:"year"`
}
// Diagnosis is like diogenes but not
// Diagnosis holds status codes of the services, version, and uptime
type Diagnosis struct {
Gbif int
Restcountries int
......@@ -41,30 +40,24 @@ type Diagnosis struct {
Uptime int
}
// DoRequest does request
// DoRequest performs an http request and returns the response
func DoRequest(c* http.Client, requestURL string) *http.Response{
r, e := http.NewRequest(http.MethodGet, requestURL, nil)
if e != nil {
fmt.Print("||||||||||||||||||| ERROR FORMING REQUEST |||||||||||||||||||||||||||")
panic(e)
}
resp, err := c.Do(r)
if err != nil {
fmt.Print("||||||||||||||||||| ERROR DOING HTML STUFF |||||||||||||||||||||||||||")
panic(err)
}
defer resp.Body.Close()
return resp
}
// http://localhost:8080/conservation/v1/country/no?limit=2
// HandlerCountry yeehwaw
// HandlerCountry handler for country and its species
func HandlerCountry(w http.ResponseWriter, r* http.Request){
occurrenceAPIURL := "http://api.gbif.org/v1/occurrence/search?"
......@@ -85,18 +78,21 @@ func HandlerCountry(w http.ResponseWriter, r* http.Request){
err := json.NewDecoder(resp.Body).Decode(&output)
if err != nil {
fmt.Print("||||||||||||||||||| ERROR PARSING JSON AS OCCURRENCE |||||||||||||||||||||||||||")
panic(err)
}
defer resp.Body.Close()
resp = DoRequest(client, countryInput)
err = json.NewDecoder(resp.Body).Decode(&output)
if err != nil {
fmt.Print("||||||||||||||||||| ERROR PARSING JSON AS COUNTRY |||||||||||||||||||||||||||")
panic(err)
}
defer resp.Body.Close()
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(output)
......@@ -120,65 +116,50 @@ func HandlerSpecies(w http.ResponseWriter, r* http.Request){
resp := DoRequest(client, speciesURL)
err := json.NewDecoder(resp.Body).Decode(&output)
if err != nil {
fmt.Print("||||||||||||||||||| ERROR PARSING JSON AS SPECIES |||||||||||||||||||||||||||")
panic(err)
}
defer resp.Body.Close()
speciesURL += "/name"
resp = DoRequest(client, speciesURL)
err = json.NewDecoder(resp.Body).Decode(&output)
if err != nil {
fmt.Print("||||||||||||||||||| ERROR PARSING JSON AS SPECIES |||||||||||||||||||||||||||")
panic(err)
}
defer resp.Body.Close()
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(output)
}
var startTime time.Time //Global variabel. Deklareres i fila du skal regne ut uptime i
var startTime time.Time
// HandlerDiagnostics handler diager
func HandlerDiagnostics(w http.ResponseWriter, r* http.Request){
speciesURL := "http://api.gbif.org/v1/species/5817566"
//occurrenceURL := "http://api.gbif.org/v1/occurrence/search?country=no&limit=1"
countryURL := "https://restcountries.eu/rest/v2/alpha/no"
client := http.DefaultClient
gbifStatus := DoDiagnosticRequest(client, speciesURL)
countriesStatus := DoDiagnosticRequest(client, countryURL)
uptime := int(time.Since(startTime).Seconds()) //Regner ut uptime og caster om til int
uptime := int(time.Since(startTime).Seconds())
output := Diagnosis{gbifStatus, countriesStatus, Version, uptime}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(output)
/*
gbif int
restcountries int
version string
uptime int
"gbif": "<http status code for GBIF API>",
"restcountries": "<http status code for restcountries API>",
"version": "v1",
"uptime": <time in seconds from the last service restart>
*/
}
// DoDiagnosticRequest does request, but returns http status if things go wrong
// DoDiagnosticRequest does request, but returns http status, even if things go wrong
func DoDiagnosticRequest(c* http.Client, requestURL string) int{
r, e := http.NewRequest(http.MethodGet, requestURL, nil)
if e != nil {
fmt.Print("||||||||||||||||||| ERROR FORMING REQUEST |||||||||||||||||||||||||||")
panic(e)
}
......
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