Skip to content
Snippets Groups Projects
Commit 169d5383 authored by jotangen's avatar jotangen
Browse files

Trying to read gbif api and get it up to a server

parent ad534dd3
No related branches found
No related tags found
No related merge requests found
No preview for this file type
......@@ -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
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment