diff --git a/assignment1 b/assignment1 index e550227f1c740d4f844b53f6bbf9d8c5be16abb7..cec69aeebfbc520f0a9a887c96c8ab969c175bde 100755 Binary files a/assignment1 and b/assignment1 differ diff --git a/main.go b/main.go index f650fe64b90f993622c0bbc4d38e17a836616c1a..d910d629ed58c8fd73e7dccf62fe2bb15d37fbcb 100644 --- a/main.go +++ b/main.go @@ -17,7 +17,7 @@ import ( -func urlSplitForCountry(a string)(string, string){ +func urlSplitForCountry(a string)(string, string, string){ const lengthUrl = 5 // 0/1=conserv/2=v1/3=country/4=no&limit=10 @@ -28,28 +28,31 @@ const lengthUrl = 5 // 0=&limit 1=10 partsThree := strings.Split(partsOne[4], "=") + fmt.Printf(" PartsThree:[0] %d\n PartsThree[1]: %d\n", len(partsThree[0]), len(partsThree[1])) + if len(partsThree[1])>= 1 { - fmt.Printf(" PartsOne: %d\n PartsOne[4]: %d\n", len(partsOne), len(partsOne[4])) - if len(partsOne) == lengthUrl { + fmt.Printf(" PartsOne: %d\n PartsOne[4]: %d\n", len(partsOne), len(partsOne[4])) - CountryCode := partsTwo[0] - fmt.Printf("CountryCode is: %s\n", CountryCode) - Limit := partsThree[1] - fmt.Printf("Limit is: %s\n", Limit) + CountryCode := partsTwo[0] + fmt.Printf("CountryCode is: %s\n", CountryCode) - return CountryCode, Limit + Limit := partsThree[1] + fmt.Printf("Limit is: %s\n", Limit) + fmt.Printf(" PartsThree:[0] %d\n PartsThree[1]: %d\n", len(partsThree[0]), len(partsThree[1])) - } else {fmt.Printf("Remember to use /conservation/v1/country/<value>&limit=<value> ") - return "","" - } + return CountryCode, Limit, "" + }else { + errText := "Remember to use /conservation/v1/country/<value>&limit=<value>" + return "", "", errText + } } func defaultHandler(w http.ResponseWriter, r *http.Request){ //code to be added - fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path)) + fmt.Fprintf(w, "Hello, this is default page %q", html.EscapeString(r.URL.Path)) } func viewHandler(w http.ResponseWriter, r *http.Request) { @@ -57,7 +60,11 @@ func viewHandler(w http.ResponseWriter, r *http.Request) { //Code to do // should you test for client error or server error 400 / 500 - CountryCode, Limit := urlSplitForCountry(r.URL.Path) + CountryCode, Limit, errText := urlSplitForCountry(r.URL.Path) + + if len(errText) != 0 { + fmt.Fprintf(w,"%s\n",errText) + } // Get country CountryInfo:= getCountryName(CountryCode) @@ -65,6 +72,11 @@ func viewHandler(w http.ResponseWriter, r *http.Request) { //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 + if Limit == ""{ + // Add limit 20 to search query + Limit = "20" + fmt.Printf("Limit is now:%d\n ",Limit) + } Species := getSpeciesByCountry(CountryCode,Limit) //fmt.Fprintf(w,"Species %v\n",Species.Collection) json.NewEncoder(w).Encode(CountryInfo) @@ -103,7 +115,7 @@ func main() { port := os.Getenv("PORT") // Remove this when deploy - //port ="8080" + port ="8080" if port == "" { //log.fatal formatting output and exits diff --git a/webapp.go b/webapp.go index 019c3a57176d486e354ab946a284a0a36c927846..ca5263abe62d3245ed902098d718a0a915565717 100644 --- a/webapp.go +++ b/webapp.go @@ -98,7 +98,7 @@ func getDiagnostic() Diagnostics { return diag } -//Should return the name Norway +// Should return the countryinformation struct func getCountryName(countryCode string) CntrInfo { // Instance of struct @@ -139,9 +139,9 @@ func getSpeciesByCountry(CountryCode, LimitNumber string) CollectionFromReq { // One instance of struct cfr :=CollectionFromReq{} + // If limit is not included in search query url := apiRootSpeciesInCountry + CountryCode + limitSyntax + LimitNumber - fmt.Printf("This is limit number in getSpecByCountry function %s\n",LimitNumber) // Result stored as JSON in resp resp, err := http.Get(url)