Skip to content
Snippets Groups Projects
Select Git revision
  • 09a21a972966e8c8d8ab41a8e8f3c9a268614e17
  • master default protected
2 results

species.go

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    species.go 2.33 KiB
    package assignment1
    
    import (
    	"encoding/json"
    	"net/http"
    	"strings"
    )
    
    // sends a request to the api and decodes the response into the species struct
    func speciesRequest(w http.ResponseWriter, url string, c *http.Client, species *Species) {
    
    	// sends a request to the api and gets a response
    	resp := doRequest(w, url, c)
    
    	// decodes the response into the struct
    	err := json.NewDecoder(resp.Body).Decode(&species)
    	if err != nil {
    		http.Error(w, "Internal server error", http.StatusInternalServerError)
    		panic(err)
    	}
    
    	// closes the body
    	defer resp.Body.Close()
    }
    
    // sends a request to the api and decodes the response into the year struct
    func yearRequest(w http.ResponseWriter, url string, c *http.Client, species *Species) {
    
    	// sends a request to the api and gets a response
    	resp := doRequest(w, url, c)
    
    	// empty year struct
    	year := &Year{}
    
    	// decodes the response into the struct
    	err := json.NewDecoder(resp.Body).Decode(&year)
    	if err != nil {
    		http.Error(w, "Internal server error", http.StatusInternalServerError)
    		panic(err)
    	}
    
    	// puts the year from the year struct into the species struct
    	species.Year = year.Year
    
    	// closes the body
    	defer resp.Body.Close()
    }
    
    // HandlerSpecies function: handles the species endpoint
    func HandlerSpecies(w http.ResponseWriter, r *http.Request) {
    
    	http.Header.Add(w.Header(), "content-type", "application/json")
    
    	// empty struct that will eventually contain the species values
    	species := &Species{}
    
    	APIURL := "http://api.gbif.org/v1/species/"
    
    	// splits the url so we can append user input to the apiurl
    	parts := strings.Split(r.URL.Path, "/")
    
    	// checks if it is a valid path
    	if len(parts) != 5 || parts[1] != "conservation" || parts[2] != "v1" || parts[3] != "species" || parts[4] == "" {
    		status := http.StatusBadRequest
    		http.Error(w, "Expecting format /conservation/v1/species/'speciesNumber'", status)
    		return
    	}
    
    	// Appends userinput to the apiurl
    	APIURL += parts[4]
    	APIURL2 := APIURL + "/name"
    
    	// makes a client
    	client := http.DefaultClient
    
    	// sends requests to the apis, and decodes everything
    	speciesRequest(w, APIURL, client, species)
    	yearRequest(w, APIURL2, client, species)
    
    	// encodes species which now contains year as well
    	err := json.NewEncoder(w).Encode(species)
    	if err != nil {
    		http.Error(w, "Internal server error", http.StatusInternalServerError)
    		panic(err)
    	}
    }