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
77b8239f
Commit
77b8239f
authored
5 years ago
by
andmag
Browse files
Options
Downloads
Patches
Plain Diff
Finalizing comments
parent
ee17aec5
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
country.go
+5
-5
5 additions, 5 deletions
country.go
diagnostics.go
+1
-2
1 addition, 2 deletions
diagnostics.go
func.go
+3
-3
3 additions, 3 deletions
func.go
species.go
+3
-4
3 additions, 4 deletions
species.go
struct.go
+1
-1
1 addition, 1 deletion
struct.go
uptime.go
+2
-1
2 additions, 1 deletion
uptime.go
with
15 additions
and
16 deletions
country.go
+
5
−
5
View file @
77b8239f
...
...
@@ -2,7 +2,6 @@ package assignment1
import
(
"encoding/json"
"fmt"
"net/http"
"strings"
)
...
...
@@ -16,7 +15,7 @@ func countryRequest(url string, c *http.Client, country *Country) {
// decodes the response into the struct
err
:=
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
&
country
)
if
err
!=
nil
{
fmt
.
Println
(
"Error"
,
err
.
Error
()
)
panic
(
err
)
}
// closes the body
...
...
@@ -35,7 +34,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
{
fmt
.
Println
(
"Error"
,
err
.
Error
()
)
panic
(
err
)
}
// closes the body
...
...
@@ -67,6 +66,7 @@ func HandlerCountry(w http.ResponseWriter, r *http.Request) {
urlQuery
:=
r
.
URL
.
RawQuery
parts
:=
strings
.
Split
(
r
.
URL
.
Path
,
"/"
)
// checks
if
len
(
parts
)
!=
5
||
parts
[
1
]
!=
"conservation"
||
parts
[
2
]
!=
"v1"
||
parts
[
3
]
!=
"country"
||
parts
[
4
]
==
""
{
status
:=
http
.
StatusBadRequest
http
.
Error
(
w
,
"Expecting format /conservation/v1/country/'AlphaCode'"
,
status
)
...
...
@@ -77,7 +77,7 @@ func HandlerCountry(w http.ResponseWriter, r *http.Request) {
APIURL
+=
parts
[
4
]
APIURL2
:=
"http://api.gbif.org/v1/occurrence/search?country="
+
parts
[
4
]
+
"&"
+
urlQuery
//empty country struct
//
empty country struct
country
:=
&
Country
{}
// makes a client
...
...
@@ -90,6 +90,6 @@ func HandlerCountry(w http.ResponseWriter, r *http.Request) {
// encodes everything to the browser
err
:=
json
.
NewEncoder
(
w
)
.
Encode
(
country
)
if
err
!=
nil
{
fmt
.
Println
(
"Error"
,
err
.
Error
()
)
panic
(
err
)
}
}
This diff is collapsed.
Click to expand it.
diagnostics.go
+
1
−
2
View file @
77b8239f
...
...
@@ -2,7 +2,6 @@ package assignment1
import
(
"encoding/json"
"fmt"
"net/http"
)
...
...
@@ -30,6 +29,6 @@ func HandlerDiag(w http.ResponseWriter, r *http.Request) {
// encodes everything to the browser
err
:=
json
.
NewEncoder
(
w
)
.
Encode
(
diagnostics
)
if
err
!=
nil
{
fmt
.
Println
(
"Error"
,
err
.
Error
()
)
panic
(
err
)
}
}
This diff is collapsed.
Click to expand it.
func.go
+
3
−
3
View file @
77b8239f
...
...
@@ -11,17 +11,17 @@ func HandlerNil(w http.ResponseWriter, r *http.Request) {
http
.
Error
(
w
,
"Invalid request"
,
http
.
StatusBadRequest
)
}
// handles
all
the requests and returns the response
// handles the requests
from the apis
and returns the response
func
doRequest
(
url
string
,
c
*
http
.
Client
)
*
http
.
Response
{
req
,
err
:=
http
.
NewRequest
(
http
.
MethodGet
,
url
,
nil
)
if
err
!=
nil
{
fmt
.
Println
(
"Error"
,
err
)
panic
(
err
)
}
resp
,
err
:=
c
.
Do
(
req
)
if
err
!=
nil
{
fmt
.
Println
(
"Error"
,
err
)
panic
(
err
)
}
return
resp
...
...
This diff is collapsed.
Click to expand it.
species.go
+
3
−
4
View file @
77b8239f
...
...
@@ -2,7 +2,6 @@ package assignment1
import
(
"encoding/json"
"fmt"
"net/http"
"strings"
)
...
...
@@ -16,7 +15,7 @@ func speciesRequest(url string, c *http.Client, species *Species) {
// decodes the response into the struct
err
:=
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
&
species
)
if
err
!=
nil
{
fmt
.
Println
(
"Error"
,
err
.
Error
()
)
panic
(
err
)
}
// closes the body
...
...
@@ -35,7 +34,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
{
fmt
.
Println
(
"Error"
,
err
.
Error
()
)
panic
(
err
)
}
// puts the year from the year struct into the species struct
...
...
@@ -79,6 +78,6 @@ func HandlerSpecies(w http.ResponseWriter, r *http.Request) {
// encodes species which now contains year as well
err
:=
json
.
NewEncoder
(
w
)
.
Encode
(
species
)
if
err
!=
nil
{
fmt
.
Println
(
"Error"
,
err
.
Error
()
)
panic
(
err
)
}
}
This diff is collapsed.
Click to expand it.
struct.go
+
1
−
1
View file @
77b8239f
...
...
@@ -38,7 +38,7 @@ type Year struct {
Year
string
`json:"bracketYear"`
}
// Diag struct:
// Diag struct:
struct for diagnostics
type
Diag
struct
{
Gbif
int
Restcountries
int
...
...
This diff is collapsed.
Click to expand it.
uptime.go
+
2
−
1
View file @
77b8239f
...
...
@@ -4,7 +4,8 @@ import (
"time"
)
var
startTime
time
.
Time
// to measure the time since the last service restart
// to measure the time since the last service restart
var
startTime
time
.
Time
// Uptime function: returns time since start time in seconds
func
Uptime
()
float64
{
...
...
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