Skip to content
Snippets Groups Projects
Commit 22fcd5df authored by Benjamin skinstad's avatar Benjamin skinstad
Browse files

added more documentation and fixed some errors

parent 1bfc4b9e
Branches
No related tags found
No related merge requests found
# Assignment 1 imt2681 cloud # Assignment 1 imt2681 cloud
Read me for Benjamin Skinstads 470918 cloud 1 assigment
this assigment follows all set requirements for this assigment
I have added tests and redudnacy for most possible errors in my project, and input validation and checks where ever needed, there is to many
of these to list here but they are documented in the program
I was unsure where the "status code 200 if everything is okay" should be printed out so i just printline the status code to the cl after every get
I used 503 as the error code for diag on the api check, this is called if the server is whatever reason unreachable
missing error checks:
if country is given more than one letter it breaks, this is due to the limitations of my hard coded formatting
if species is given a code like 696 it breaks, this is because this is another type of json request, i was unable to find a work around here.
im not enterily happy with how i call my 404 pages and the chekcs can be a bit extreme if there are many
what i would add in the future: rate limiting, fix the json to be proper, and proper input handling
...@@ -134,7 +134,13 @@ func main() { ...@@ -134,7 +134,13 @@ func main() {
func undefined(w http.ResponseWriter, r *http.Request){ func undefined(w http.ResponseWriter, r *http.Request){
io.WriteString(w, "please use /conservation/v1/country/ /conservation/v1/species/ /conservation/v1/diag/") io.WriteString(w, "Hello and welcome to Benjamin Skinstad project\n" +
"the valid url calls are /conservation/v1/country/ /conservation/v1/species/ /conservation/v1/diag/\n" +
"some example urls are:\n" +
"https://sheltered-sea-90657.herokuapp.com/conservation/v1/species/6969\n" +
"https://sheltered-sea-90657.herokuapp.com/conservation/v1/country/us&limit=100\n" +
"https://sheltered-sea-90657.herokuapp.com/conservation/v1/diag/\n\n\n" +
"these links will only work the heroku deployed site, but they wiil still work if you change the heroku part with localhost")
} }
...@@ -145,6 +151,7 @@ func country(w http.ResponseWriter, r *http.Request){ ...@@ -145,6 +151,7 @@ func country(w http.ResponseWriter, r *http.Request){
filtered := strings.Replace(input,countryFilter, "", -1) //removes the unessesary parts of the url filtered := strings.Replace(input,countryFilter, "", -1) //removes the unessesary parts of the url
if filtered == ""{ if filtered == ""{
io.WriteString(w,"404") io.WriteString(w,"404")
}else { }else {
...@@ -163,6 +170,11 @@ func country(w http.ResponseWriter, r *http.Request){ ...@@ -163,6 +170,11 @@ func country(w http.ResponseWriter, r *http.Request){
} }
resp, err := http.Get("https://restcountries.eu/rest/v2/alpha/" + countryCode) //get full cpountry json responce resp, err := http.Get("https://restcountries.eu/rest/v2/alpha/" + countryCode) //get full cpountry json responce
println(resp.StatusCode)
if resp.StatusCode == 404 {
io.WriteString(w, "404")
}else{
if err != nil { if err != nil {
print("error\n") print("error\n")
log.Fatalln(err) log.Fatalln(err)
...@@ -180,51 +192,14 @@ func country(w http.ResponseWriter, r *http.Request){ ...@@ -180,51 +192,14 @@ func country(w http.ResponseWriter, r *http.Request){
//gets the species part of the json //gets the species part of the json
respSpecis, err := http.Get("http://api.gbif.org/v1/occurrence/search?country=" + countryCode + "&" + "limit=" + filterNumber) respSpecis, err := http.Get("http://api.gbif.org/v1/occurrence/search?country=" + countryCode + "&" + "limit=" + filterNumber)
println(respSpecis.StatusCode)
if err != nil { if err != nil {
print("error\n") print("error\n")
log.Fatalln(err) log.Fatalln(err)
} }
// defer respSpecis.Body.Close()
// bodySpecis, err := ioutil.ReadAll(respSpecis.Body)
// if err != nil {
// print("error\n")
// log.Fatalln(err)
// }
// var infoSpecis map[string]interface{}
// json.Unmarshal([]byte(bodySpecis),&infoSpecis)
// numberResponce := infoSpecis["limit"]
// var intNumberResponce int = int(numberResponce.(float64))
// var countryJsonArray []speciesJSON
/* var objmap map[string]*json.RawMessage
//err = json.Unmarshal(bodySpecis, &objmap)
var s interface{}
err = json.Unmarshal(*objmap["results"], &s)
*/
//err = json.Unmarshal(s[1], &test)
// str := fmt.Sprintf("%v", s)
// var test map[string]interface{}
// io.WriteString(w,string(str))
// str := fmt.Sprintf("%v", respSpecis.Body)
// println(str)
var spectest arraySpeciesJSON var spectest arraySpeciesJSON
// var canonicalNameTest canonicalNameJson // var canonicalNameTest canonicalNameJson
...@@ -249,7 +224,6 @@ func country(w http.ResponseWriter, r *http.Request){ ...@@ -249,7 +224,6 @@ func country(w http.ResponseWriter, r *http.Request){
keyTest := fmt.Sprintf("%f", spectest.ArryJson[i].SpecKey) //gets the key keyTest := fmt.Sprintf("%f", spectest.ArryJson[i].SpecKey) //gets the key
if strings.Contains(duplicateCheck, keyTest) { //check if key already exists, go out of loop if it does if strings.Contains(duplicateCheck, keyTest) { //check if key already exists, go out of loop if it does
continue continue
} }
...@@ -276,11 +250,9 @@ func country(w http.ResponseWriter, r *http.Request){ ...@@ -276,11 +250,9 @@ func country(w http.ResponseWriter, r *http.Request){
var y int = int(spectest.ArryJson[i].SpecKey) var y int = int(spectest.ArryJson[i].SpecKey)
specKeyString := strconv.Itoa(y) specKeyString := strconv.Itoa(y)
canonicalNameResp, err := http.Get("http://api.gbif.org/v1/species/" + specKeyString + "/name") canonicalNameResp, err := http.Get("http://api.gbif.org/v1/species/" + specKeyString + "/name")
if err != nil { if err != nil {
print("error\n") print("error\n")
log.Fatalln(err) log.Fatalln(err)
...@@ -298,14 +270,10 @@ func country(w http.ResponseWriter, r *http.Request){ ...@@ -298,14 +270,10 @@ func country(w http.ResponseWriter, r *http.Request){
var canonicalNameMap map[string]interface{} var canonicalNameMap map[string]interface{}
json.Unmarshal([]byte(canonicalNamebody), &canonicalNameMap) //again this poor implementation of json, but it works json.Unmarshal([]byte(canonicalNamebody), &canonicalNameMap) //again this poor implementation of json, but it works
println(specKeyString)
canonicalName = canonicalNameMap["canonicalName"].(string) canonicalName = canonicalNameMap["canonicalName"].(string)
} }
tempJson := speciesJSON2{float64(key), kingdom, phylum, order, family, tempJson := speciesJSON2{float64(key), kingdom, phylum, order, family,
genus, scientificName, canonicalName, year, specKey} genus, scientificName, canonicalName, year, specKey}
...@@ -359,6 +327,7 @@ func country(w http.ResponseWriter, r *http.Request){ ...@@ -359,6 +327,7 @@ func country(w http.ResponseWriter, r *http.Request){
io.WriteString(w, string(e)) io.WriteString(w, string(e))
} }
} }
}
func species(w http.ResponseWriter, r *http.Request){ func species(w http.ResponseWriter, r *http.Request){
//io.WriteString(w, "\nspecies\n\n") //io.WriteString(w, "\nspecies\n\n")
...@@ -369,7 +338,7 @@ func species(w http.ResponseWriter, r *http.Request){ ...@@ -369,7 +338,7 @@ func species(w http.ResponseWriter, r *http.Request){
resp,err := http.Get("http://api.gbif.org/v1/species/"+filtred) //runs api call with that paramter resp,err := http.Get("http://api.gbif.org/v1/species/"+filtred) //runs api call with that paramter
if resp.StatusCode == 404 { if resp.StatusCode == 404 {
io.WriteString(w,"404") //if input is invalid we do an 404 check and writes that out on the page if that is the case io.WriteString(w,"404") //if input is invalid we do an 404 check and writes that out on the page if that is the case
println(resp.StatusCode)
}else { }else {
if err != nil { if err != nil {
...@@ -433,7 +402,7 @@ func species(w http.ResponseWriter, r *http.Request){ ...@@ -433,7 +402,7 @@ func species(w http.ResponseWriter, r *http.Request){
respYear, err := http.Get("http://api.gbif.org/v1/species/" + strconv.Itoa(namekeyInt) + "/name") //uses the namekey from the species call to find year in /name respYear, err := http.Get("http://api.gbif.org/v1/species/" + strconv.Itoa(namekeyInt) + "/name") //uses the namekey from the species call to find year in /name
println(respYear.StatusCode)
if err != nil { if err != nil {
print("error\n") print("error\n")
log.Fatalln(err) log.Fatalln(err)
...@@ -481,6 +450,7 @@ func diag(w http.ResponseWriter, r *http.Request){ ...@@ -481,6 +450,7 @@ func diag(w http.ResponseWriter, r *http.Request){
* if the site is unreachable the status code is sat to 503 (service unaviable) * if the site is unreachable the status code is sat to 503 (service unaviable)
*/ */
respGbif,err := http.Get("http://api.gbif.org/") respGbif,err := http.Get("http://api.gbif.org/")
if respGbif == nil { if respGbif == nil {
gbifCode = 503 gbifCode = 503
}else{ }else{
...@@ -532,13 +502,7 @@ func diag(w http.ResponseWriter, r *http.Request){ ...@@ -532,13 +502,7 @@ func diag(w http.ResponseWriter, r *http.Request){
} }
func fouroFour(w http.ResponseWriter, r *http.Request){
e := "404"
io.WriteString(w,string(e))
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment