Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
Assignment1
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Andrea Magnussen
Assignment1
Commits
09a21a97
Commit
09a21a97
authored
5 years ago
by
andmag
Browse files
Options
Downloads
Patches
Plain Diff
last push
parent
77b8239f
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
country.go
+9
-6
9 additions, 6 deletions
country.go
func.go
+3
-1
3 additions, 1 deletion
func.go
species.go
+9
-6
9 additions, 6 deletions
species.go
with
21 additions
and
13 deletions
country.go
+
9
−
6
View file @
09a21a97
...
@@ -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
)
}
}
}
}
This diff is collapsed.
Click to expand it.
func.go
+
3
−
1
View file @
09a21a97
...
@@ -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
)
}
}
...
...
This diff is collapsed.
Click to expand it.
species.go
+
9
−
6
View file @
09a21a97
...
@@ -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
)
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment