Skip to content
Snippets Groups Projects
Commit 42b0dbbf authored by jotangen's avatar jotangen
Browse files

/Diag fixed with json

parent d31a584a
No related branches found
No related tags found
No related merge requests found
No preview for this file type
package main
import (
"encoding/json"
"fmt"
"html"
"log"
......@@ -14,14 +15,23 @@ import (
// URL given to r, page data loaded.
// formatted HTML is then sent to the page via W
func urlSplitForCountry(a string)(string, string){
const lengthUrl = 5
// 0/1=conserv/2=v1/3=country/4=name/5=no&limit=10
// 0/1=conserv/2=v1/3=country/4=no&limit=10
partsOne := strings.Split(a, "/")
// 0=no & 1=limit=no
partsTwo := strings.Split(partsOne[5], "&")
// 0=no & 1=limit=10
partsTwo := strings.Split(partsOne[4], "&")
// 0=&limit 1=10
partsThree := strings.Split(partsOne[5],"=")
partsThree := strings.Split(partsOne[4], "=")
fmt.Printf(" PartsOne: %d\n PartsOne[4]: %d\n", len(partsOne), len(partsOne[4]))
if len(partsOne) == lengthUrl {
CountryCode := partsTwo[0]
fmt.Printf("CountryCode is: %s\n", CountryCode)
......@@ -30,6 +40,11 @@ func urlSplitForCountry(a string)(string, string){
fmt.Printf("Limit is: %s\n", Limit)
return CountryCode, Limit
} else {fmt.Printf("Remember to use /conservation/v1/country/<value>&limit=<value> ")
return "",""
}
}
func defaultHandler(w http.ResponseWriter, r *http.Request){
......@@ -45,13 +60,15 @@ func viewHandler(w http.ResponseWriter, r *http.Request) {
CountryCode, Limit := urlSplitForCountry(r.URL.Path)
// Get country
Country, Ccode, Flag := getCountryName(CountryCode)
CountryInfo:= getCountryName(CountryCode)
fmt.Fprintf(w,"Country name is %s\n Country code is: %s\n Flag is: %s\n ", Country, Ccode, Flag)
//fmt.Fprintf(w,"Country name is %s\n Country code is: %s\n Flag is: %s\n ", Country, Ccode, Flag)
// get species from a given country
Species := getSpeciesByCountry(CountryCode,Limit)
fmt.Fprintf(w,"Species %v\n",Species)
//fmt.Fprintf(w,"Species %v\n",Species.Collection)
json.NewEncoder(w).Encode(CountryInfo)
json.NewEncoder(w).Encode(Species)
}
func speciesHandler(w http.ResponseWriter, r *http.Request){
......@@ -66,16 +83,16 @@ Species := getSpecies(speciesKey)
Year:= getYearForSpecie(speciesKey)
// write to web
fmt.Fprintf(w," Species: %v\n Year: %s\n ", Species, Year)
//fmt.Fprintf(w," Species: %v\n Year: %s\n ", Species, Year)
json.NewEncoder(w).Encode(Species)
json.NewEncoder(w).Encode(Year)
}
func diagnosticHandler(w http.ResponseWriter, r *http.Request){
// /conservation/v1/diag/
StatGBIF, StatRestC, Version, Time := getDiagnostic()
fmt.Fprintf(w," GBIF API Status: %d\n Rest Countries API Status: %d\n", StatGBIF, StatRestC)
Diagnostic := getDiagnostic()
fmt.Fprintf(w," Version: %s\n UpTime: %s\n", Version, Time)
json.NewEncoder(w).Encode(Diagnostic)
}
// Global var type time
......@@ -85,6 +102,9 @@ var Start time.Time
func main() {
port := os.Getenv("PORT")
// Remove this when deploy
port ="8080"
if port == "" {
//log.fatal formatting output and exits
log.Fatal("$PORT must be set")
......@@ -98,7 +118,7 @@ func main() {
// Add code to this function
http.HandleFunc("/", defaultHandler)
http.HandleFunc("/conservation/v1/country/name/", viewHandler)
http.HandleFunc("/conservation/v1/country/", viewHandler)
http.HandleFunc("/conservation/v1/species/", speciesHandler)
......
......@@ -57,56 +57,58 @@ type SpeciesKeyBody struct{
type SpeciesYear struct{
//Hint: For the year, check the /species/{key}/name path for a given key.
Year string `json:"bracketYear"`
Year int `json:"bracketYear"`
}
func getDiagnostic() (int,int,string, time.Duration) {
type Diagnostics struct{
Gbif int
Restcountries int
Version string
Uptime time.Duration
}
/*
"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>
func getDiagnostic() Diagnostics {
*/
//res := Response{}
urlGBIF := apiRootGBIF
respDiag, err := http.Get(urlGBIF)
fmt.Printf("Dette er vår respDiag%s\n",respDiag)
if err !=nil {
fmt.Println("Error:we have a problem", err)
return 0,0, "",0
return Diagnostics{}
}
defer respDiag.Body.Close()
//json.NewDecoder(respDiag.Body).Decode(ci)
//json.NewDecoder(respDiag.Body).Decode(diag.Gbif)
urlRestCountr := apiRootRestCountr
respCountr, err := http.Get(urlRestCountr)
if err !=nil {
fmt.Println("Error:we have a problem", err)
return 0, 0,"",0
return Diagnostics{}
}
defer respCountr.Body.Close()
//json.NewDecoder(respCountr.Body).Decode(diag.Restcountries)
t := time.Now()
elapsed := t.Sub(Start)
diag := Diagnostics{respDiag.StatusCode, respCountr.StatusCode, version, elapsed}
return respDiag.StatusCode, respCountr.StatusCode, version, elapsed
return diag
}
//Should return the name Norway
func getCountryName(countryName string) (string, string, string) {
func getCountryName(countryCode string) CntrInfo {
// Instance of struct
ci := &CntrInfo{}
ci := CntrInfo{}
//Change hardcoded to func parameters "We pass them"
//countryCode := "NO"
//Implement passed parameters
url := apiRoot + countryName
url := apiRoot + countryCode
fmt.Println("Dette er vår url",url)
//resp is json, so we need to parse it
......@@ -117,7 +119,7 @@ func getCountryName(countryName string) (string, string, string) {
if err !=nil {
fmt.Println("Error:we have a problem", err)
return "", "",""
return ci
}
//Closes when surrounded functions are finished
......@@ -126,10 +128,10 @@ func getCountryName(countryName string) (string, string, string) {
//resp.Body JSON
//NewDecoder returns a new decoder that reads from r
json.NewDecoder(resp.Body).Decode(ci)
json.NewDecoder(resp.Body).Decode(&ci)
fmt.Println("after json decoder",ci)
return ci.Name, ci.CountryCode, ci.Flag
return ci
}
// Should return a struct with species and key that has occurrence in a pre defined country
......@@ -181,8 +183,9 @@ func getSpecies (SpeciesKey string) SpeciesKeyBody {
}
func getYearForSpecie(SpeciesKey string) SpeciesYear {
const Name = "name"
const Name = "/name"
sy:= SpeciesYear{}
// http://api.gbif.org/v1/species/5231190/name
url:= apiRootSpecies + SpeciesKey + Name
respYear, err:=http.Get(url)
if err !=nil {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment