Skip to content
Snippets Groups Projects
Commit 09a21a97 authored by andmag's avatar andmag
Browse files

last push

parent 77b8239f
Branches
No related tags found
No related merge requests found
...@@ -7,14 +7,15 @@ import ( ...@@ -7,14 +7,15 @@ import (
) )
// sends a request to the api and decodes the response into the country struct // 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 // sends a request for country and gets the response
resp := doRequest(url, c) resp := doRequest(w, url, c)
// decodes the response into the struct // decodes the response into the struct
err := json.NewDecoder(resp.Body).Decode(&country) err := json.NewDecoder(resp.Body).Decode(&country)
if err != nil { if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
panic(err) panic(err)
} }
...@@ -23,10 +24,10 @@ func countryRequest(url string, c *http.Client, country *Country) { ...@@ -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 // 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 // sends a request for species in a country and gets the response
resp := doRequest(url, c) resp := doRequest(w, url, c)
// empty result struct // empty result struct
var nameAndKey = &Results{} var nameAndKey = &Results{}
...@@ -34,6 +35,7 @@ func speciesInCountryRequest(url string, c *http.Client, country *Country) { ...@@ -34,6 +35,7 @@ func speciesInCountryRequest(url string, c *http.Client, country *Country) {
// decodes the response into the array of results // decodes the response into the array of results
err := json.NewDecoder(resp.Body).Decode(nameAndKey) err := json.NewDecoder(resp.Body).Decode(nameAndKey)
if err != nil { if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
panic(err) panic(err)
} }
...@@ -84,12 +86,13 @@ func HandlerCountry(w http.ResponseWriter, r *http.Request) { ...@@ -84,12 +86,13 @@ func HandlerCountry(w http.ResponseWriter, r *http.Request) {
client := http.DefaultClient client := http.DefaultClient
// makes a request for country and species in a country and decodes everything // makes a request for country and species in a country and decodes everything
countryRequest(APIURL, client, country) countryRequest(w, APIURL, client, country)
speciesInCountryRequest(APIURL2, client, country) speciesInCountryRequest(w, APIURL2, client, country)
// encodes everything to the browser // encodes everything to the browser
err := json.NewEncoder(w).Encode(country) err := json.NewEncoder(w).Encode(country)
if err != nil { if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
panic(err) panic(err)
} }
} }
...@@ -12,15 +12,17 @@ func HandlerNil(w http.ResponseWriter, r *http.Request) { ...@@ -12,15 +12,17 @@ func HandlerNil(w http.ResponseWriter, r *http.Request) {
} }
// handles the requests from the apis and returns the response // 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) req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil { if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
panic(err) panic(err)
} }
resp, err := c.Do(req) resp, err := c.Do(req)
if err != nil { if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
panic(err) panic(err)
} }
......
...@@ -7,14 +7,15 @@ import ( ...@@ -7,14 +7,15 @@ import (
) )
// sends a request to the api and decodes the response into the species struct // 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 // 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 // decodes the response into the struct
err := json.NewDecoder(resp.Body).Decode(&species) err := json.NewDecoder(resp.Body).Decode(&species)
if err != nil { if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
panic(err) panic(err)
} }
...@@ -23,10 +24,10 @@ func speciesRequest(url string, c *http.Client, species *Species) { ...@@ -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 // 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 // sends a request to the api and gets a response
resp := doRequest(url, c) resp := doRequest(w, url, c)
// empty year struct // empty year struct
year := &Year{} year := &Year{}
...@@ -34,6 +35,7 @@ func yearRequest(url string, c *http.Client, species *Species) { ...@@ -34,6 +35,7 @@ func yearRequest(url string, c *http.Client, species *Species) {
// decodes the response into the struct // decodes the response into the struct
err := json.NewDecoder(resp.Body).Decode(&year) err := json.NewDecoder(resp.Body).Decode(&year)
if err != nil { if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
panic(err) panic(err)
} }
...@@ -72,12 +74,13 @@ func HandlerSpecies(w http.ResponseWriter, r *http.Request) { ...@@ -72,12 +74,13 @@ func HandlerSpecies(w http.ResponseWriter, r *http.Request) {
client := http.DefaultClient client := http.DefaultClient
// sends requests to the apis, and decodes everything // sends requests to the apis, and decodes everything
speciesRequest(APIURL, client, species) speciesRequest(w, APIURL, client, species)
yearRequest(APIURL2, client, species) yearRequest(w, APIURL2, client, species)
// encodes species which now contains year as well // encodes species which now contains year as well
err := json.NewEncoder(w).Encode(species) err := json.NewEncoder(w).Encode(species)
if err != nil { if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
panic(err) panic(err)
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment