From 09a21a972966e8c8d8ab41a8e8f3c9a268614e17 Mon Sep 17 00:00:00 2001
From: andmag <andmag@stud.ntnu.no>
Date: Sat, 19 Oct 2019 13:46:34 +0200
Subject: [PATCH] last push

---
 country.go | 15 +++++++++------
 func.go    |  4 +++-
 species.go | 15 +++++++++------
 3 files changed, 21 insertions(+), 13 deletions(-)

diff --git a/country.go b/country.go
index 05fa126..e411d0b 100644
--- a/country.go
+++ b/country.go
@@ -7,14 +7,15 @@ import (
 )
 
 // sends a request to the api and decodes the response into the country struct
-func countryRequest(url string, c *http.Client, country *Country) {
+func countryRequest(w http.ResponseWriter, url string, c *http.Client, country *Country) {
 
 	// sends a request for country and gets the response
-	resp := doRequest(url, c)
+	resp := doRequest(w, url, c)
 
 	// decodes the response into the struct
 	err := json.NewDecoder(resp.Body).Decode(&country)
 	if err != nil {
+		http.Error(w, "Internal server error", http.StatusInternalServerError)
 		panic(err)
 	}
 
@@ -23,10 +24,10 @@ func countryRequest(url string, c *http.Client, country *Country) {
 }
 
 // sends a request to the api and decodes the respone into the structs
-func speciesInCountryRequest(url string, c *http.Client, country *Country) {
+func speciesInCountryRequest(w http.ResponseWriter, url string, c *http.Client, country *Country) {
 
 	// sends a request for species in a country and gets the response
-	resp := doRequest(url, c)
+	resp := doRequest(w, url, c)
 
 	// empty result struct
 	var nameAndKey = &Results{}
@@ -34,6 +35,7 @@ func speciesInCountryRequest(url string, c *http.Client, country *Country) {
 	// decodes the response into the array of results
 	err := json.NewDecoder(resp.Body).Decode(nameAndKey)
 	if err != nil {
+		http.Error(w, "Internal server error", http.StatusInternalServerError)
 		panic(err)
 	}
 
@@ -84,12 +86,13 @@ func HandlerCountry(w http.ResponseWriter, r *http.Request) {
 	client := http.DefaultClient
 
 	// makes a request for country and species in a country and decodes everything
-	countryRequest(APIURL, client, country)
-	speciesInCountryRequest(APIURL2, client, country)
+	countryRequest(w, APIURL, client, country)
+	speciesInCountryRequest(w, APIURL2, client, country)
 
 	// encodes everything to the browser
 	err := json.NewEncoder(w).Encode(country)
 	if err != nil {
+		http.Error(w, "Internal server error", http.StatusInternalServerError)
 		panic(err)
 	}
 }
diff --git a/func.go b/func.go
index af66e73..933d977 100644
--- a/func.go
+++ b/func.go
@@ -12,15 +12,17 @@ func HandlerNil(w http.ResponseWriter, r *http.Request) {
 }
 
 // handles the requests from the apis and returns the response
-func doRequest(url string, c *http.Client) *http.Response {
+func doRequest(w http.ResponseWriter, url string, c *http.Client) *http.Response {
 
 	req, err := http.NewRequest(http.MethodGet, url, nil)
 	if err != nil {
+		http.Error(w, "Internal server error", http.StatusInternalServerError)
 		panic(err)
 	}
 
 	resp, err := c.Do(req)
 	if err != nil {
+		http.Error(w, "Internal server error", http.StatusInternalServerError)
 		panic(err)
 	}
 
diff --git a/species.go b/species.go
index 52c1d7a..4cd59da 100644
--- a/species.go
+++ b/species.go
@@ -7,14 +7,15 @@ import (
 )
 
 // sends a request to the api and decodes the response into the species struct
-func speciesRequest(url string, c *http.Client, species *Species) {
+func speciesRequest(w http.ResponseWriter, url string, c *http.Client, species *Species) {
 
 	// sends a request to the api and gets a response
-	resp := doRequest(url, c)
+	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)
 	}
 
@@ -23,10 +24,10 @@ func speciesRequest(url string, c *http.Client, species *Species) {
 }
 
 // sends a request to the api and decodes the response into the year struct
-func yearRequest(url string, c *http.Client, species *Species) {
+func yearRequest(w http.ResponseWriter, url string, c *http.Client, species *Species) {
 
 	// sends a request to the api and gets a response
-	resp := doRequest(url, c)
+	resp := doRequest(w, url, c)
 
 	// empty year struct
 	year := &Year{}
@@ -34,6 +35,7 @@ func yearRequest(url string, c *http.Client, species *Species) {
 	// 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)
 	}
 
@@ -72,12 +74,13 @@ func HandlerSpecies(w http.ResponseWriter, r *http.Request) {
 	client := http.DefaultClient
 
 	// sends requests to the apis, and decodes everything
-	speciesRequest(APIURL, client, species)
-	yearRequest(APIURL2, client, species)
+	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)
 	}
 }
-- 
GitLab