diff --git a/assignment1 b/assignment1 index 6a9967d1f9ae8ffd03ee94c38ae72983f9075b3a..eb3fc8d8af7fa1cecf95742744c5625a6e1d1fd6 100755 Binary files a/assignment1 and b/assignment1 differ diff --git a/webapp.go b/webapp.go index a8d9dc7daf8770dc4b012775ae2d6aeaababad76..49756793293c2083fe1db5fa223b8e7962e2abcc 100644 --- a/webapp.go +++ b/webapp.go @@ -3,8 +3,11 @@ package main import ( "encoding/json" "fmt" + "log" "net/http" + "io/ioutil" ) + //This works see below This https://restcountries.eu/rest/v2/name/ does not const apiRoot = "https://restcountries.eu/rest/v2/alpha/" @@ -46,16 +49,7 @@ type SpeciesKeyBody struct{ func getCountryName(cn string) (string, string, string) { fmt.Println("First in func",cn) - /* - { - "code": "<country code in 2-letter ISO format>", - "countryname": "<English human-readable country name>", - "countryflag": "<link to svg version of country flag>", - "species": [], - "speciesKey": [] - } - */ //Change hardcoded to func parameters "We pass them" //countryCode := "NO" @@ -79,6 +73,7 @@ func getCountryName(cn string) (string, string, string) { //Closes when surrounded functions are finished defer resp.Body.Close() + //Pointer to struct ci := &CntrInfo{} @@ -91,33 +86,65 @@ func getCountryName(cn string) (string, string, string) { } // Should return species in exa: norway and key -func getSpeciesByCountry(Ccode string) (string, int){ +func getSpeciesByCountry(Ccode string) (string, int) { + + + url := apiRootSpeciesInCountry + Ccode resp, err := http.Get(url) + fmt.Printf("This is our url for species %s\n and this is our resp %s\n",url,resp) if err !=nil { fmt.Println("Error:we have a problem", err) //Returns 0--> find better solution return "", 0 } defer resp.Body.Close() + //https://medium.com/@masnun/making-http-requests-in-golang-dd123379efe7 + body, err := ioutil.ReadAll(resp.Body) + if err !=nil { + log.Fatalln(err) + } + log.Println(string(body)) + crf :=&CollectionFromReq{} + var result map[string]interface{} + json.NewDecoder(resp.Body).Decode(&result) + log.Println(result["form"]) //Pointer to struct // will this also point to the slices and what the slices contain?? // Points to struct containing two slices of struct - crf :=&CollectionFromReq{} + + //fmt.Printf("The crf.Count is: "crf.Count) + //fmt.Printf("This is after crf %s\n", crf) //resp.Body JSON from API //Decode resp.Body and put into .decode(HERE) //We need the resp.Body to be slices - json.NewDecoder(resp.Body).Decode(crf) - fmt.Println("after json decoder",resp.Body) + keys :=make([]Result, 0) - for i := 0; i < crf.Count; i++ { + //we need to make crf.Count work + //crf.Count does not work https://gobyexample.com/range + //for _ , s := range { + //appends more capacity to our []Result + // keys = append(keys,s) + // } + + + //json.NewDecoder(resp.Body).Decode(crf) + json.NewDecoder(resp.Body).Decode(&keys) + //fmt.Printf("after json decoder ----keys----",resp.Body) + fmt.Printf("This is our keys %#v",keys) + fmt.Printf("This is crf.count %d",crf.Count) + + //for i := 0; i < 10; i++ { + + // return crf.Collection[i].Species, crf.Collection[i].SpeciesKey + //} + + + return "", crf.Count - return crf.Collection[i].Species, crf.Collection[i].SpeciesKey - } - return "", 0 }