Skip to content
Snippets Groups Projects
Select Git revision
  • 059773b4333a8c997f54c2ac348f320104034a9b
  • master default protected
2 results

Engine.cpp

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    webapp.go 3.88 KiB
    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/"
    
    const apiRootSpeciesInCountry = "https://www.gbif.org/occurrence/search?country="
    //Root for species by country request
    const apiRootSpecies = "http://api.gbif.org/v1/species/{:speciesKey}"
    
    //We receive JSON code and we parse it
    //CntrInfo contains a response for restcountries endpoint
    type CntrInfo struct {
    	Flag string `json:flag`
    	Name string `json:"name"` //"country":"norway"
    	CountryCode string `json:"alpha2Code"`//"NO"
    
    }
    
    //Our json slice response
    type CollectionFromReq struct {
    	Count int `json:"count"`
    	Collection []Result `json:"result"`
    }
    
    // Info about each specie
    type Result struct {
    	Species string `json:"species"`
    	SpeciesKey int  `json:"speciesKey"`
    }
    
    /* Info about each key
    type SpeciesKeyBody struct{
    	key int
    	Kingdom string
    	family string
    	year string //Hint: For the year, check the /species/{key}/name path for a given key.
    }
    */
    
    //Should return the name Norway
    func getCountryName(cn string) (string, string, string) {
    
    	fmt.Println("First in func",cn)
    
    
        //Change hardcoded to func parameters "We pass them"
    	//countryCode := "NO"
    	//Implement passed parameters
    	countryName:= cn
    
    	url := apiRoot + countryName
    	fmt.Println("Dette er vår url",url)
    
    	//resp is json, so we need to parse it
    	//Sending get req to a given url
    	resp, err := http.Get(url)
    
    	fmt.Println("This is our response",resp)
    
    	if err !=nil {
    		fmt.Println("Error:we have a problem", err)
    		return "", "",""
    	}
    
    	//Closes when surrounded functions are finished
    	defer resp.Body.Close()
    
    
    	//Pointer to struct
    	ci := &CntrInfo{}
    
    	//resp.Body JSON
    	//NewDecoder returns a new decoder that reads from r
        json.NewDecoder(resp.Body).Decode(ci)
        fmt.Println("after json decoder",ci)
    
    	return ci.Name, ci.CountryCode, ci.Flag
    }
    
    // Should return species in exa: norway and key
    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
    
    
    	 //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
    
        keys :=make([]Result, 0)
    
             //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
    
    }
    
    
    
    func getSpecies () string {
    	/*
    	{
    	   "key": "<species key>",
    	   "kingdom": "<kingdom>",
    	   "phylum": "<phylum>",
    	   "order": "<order>",
    	   "family": "<family>",
    	   "genus": "<genus>",
    	   "scientificName":"<scientific name>",
    	   "canonicalName": "<canonical name>",
    	   "year": "<four-letter year>"
    	}
    
    	*/
    	return ""
    }