Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Aksel Baardsen
Assignment 1
Commits
31883b10
Commit
31883b10
authored
Oct 19, 2019
by
Aksel Baardsen
Browse files
added status codes as strings diag
parent
f785ee22
Changes
1
Hide whitespace changes
Inline
Side-by-side
pkg/diag.go
View file @
31883b10
package
pkg
import
(
"fmt"
"strconv"
"strings"
"time"
)
...
...
@@ -16,8 +17,8 @@ func init() {
// struct for returning requested data
type
Diag
struct
{
Gbif
in
t
`json:"gbif,omitempty"`
Restcountries
in
t
`json:"restcountries,omitempty"`
Gbif
str
in
g
`json:"gbif,omitempty"`
Restcountries
str
in
g
`json:"restcountries,omitempty"`
Version
string
`json:"version,omitempty"`
Uptime
int
`json:"uptime,omitempty"`
}
...
...
@@ -27,12 +28,22 @@ func GetDiag(d* Diag) error {
// gets status of GBIF api, returns appropriate error
if
err
:=
getGbifStatus
(
d
);
err
!=
nil
{
return
fmt
.
Errorf
(
"error occured while contacting GBIF API: %s"
,
err
)
// if it was a timeout caused by the http.client
if
strings
.
Contains
(
err
.
Error
(),
"error occured while contacting"
)
{
d
.
Gbif
=
"503: Request timed out"
}
else
{
return
err
}
}
// gets status of restcountries api, returns appropriate error
if
err
:=
getRestStatus
(
d
);
err
!=
nil
{
return
fmt
.
Errorf
(
"error occured while contacting Restcountries API: %s"
,
err
)
// if it was a timeout caused by the http.client
if
strings
.
Contains
(
err
.
Error
(),
"error occured while contacting"
)
{
d
.
Gbif
=
"503: Request timed out"
}
else
{
return
err
}
}
d
.
Version
=
"v1"
...
...
@@ -48,7 +59,8 @@ func getGbifStatus(d *Diag) error {
if
err
!=
nil
{
return
err
}
d
.
Gbif
=
resp
.
StatusCode
defer
resp
.
Body
.
Close
()
d
.
Gbif
=
strconv
.
Itoa
(
resp
.
StatusCode
)
return
nil
}
...
...
@@ -58,7 +70,8 @@ func getRestStatus(d *Diag) error {
if
err
!=
nil
{
return
err
}
d
.
Restcountries
=
resp
.
StatusCode
defer
resp
.
Body
.
Close
()
d
.
Restcountries
=
strconv
.
Itoa
(
resp
.
StatusCode
)
return
nil
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment