diff --git a/country.go b/country.go index 05fa12613af163f4693ce3c1ebd23697019c0329..e411d0be911143e44318ec2bcf0b799cd76f0cdb 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 af66e733cc13034ce1f700fa93a73f3674abe901..933d977bdf5e27829052f5fd293751895aa18703 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 52c1d7aba8dff767f1ccf90166bbcb5a39c8df39..4cd59da7a24f7b6a6d46945c3aeda33310fcc1ff 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) } }