Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
Assignment 1
Manage
Activity
Members
Plan
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Repository analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Aksel Baardsen
Assignment 1
Commits
34eae9b9
Commit
34eae9b9
authored
5 years ago
by
Aksel Baardsen
Browse files
Options
Downloads
Patches
Plain Diff
Now using http.Error() to send correct error code in header
parent
f35aa8fe
No related branches found
No related tags found
No related merge requests found
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
handler/country.go
+3
-5
3 additions, 5 deletions
handler/country.go
handler/diag.go
+1
-4
1 addition, 4 deletions
handler/diag.go
handler/species.go
+1
-3
1 addition, 3 deletions
handler/species.go
pkg/country.go
+12
-14
12 additions, 14 deletions
pkg/country.go
pkg/funcs.go
+17
-5
17 additions, 5 deletions
pkg/funcs.go
with
34 additions
and
31 deletions
handler/country.go
+
3
−
5
View file @
34eae9b9
...
@@ -3,7 +3,6 @@ package handler
...
@@ -3,7 +3,6 @@ package handler
import
(
import
(
"assignment-1/pkg"
"assignment-1/pkg"
"encoding/json"
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"github.com/gorilla/mux"
"log"
"log"
"net/http"
"net/http"
...
@@ -27,8 +26,7 @@ func Chandler(w http.ResponseWriter, r *http.Request) {
...
@@ -27,8 +26,7 @@ func Chandler(w http.ResponseWriter, r *http.Request) {
log
.
Fatal
(
err
)
log
.
Fatal
(
err
)
}
}
}
else
{
}
else
{
// need to check error for content before setting header
// need to check error for content when setting header (pkg.CorrectHeader)
w
.
WriteHeader
(
http
.
StatusInternalServerError
)
http
.
Error
(
w
,
"An error occured"
,
pkg
.
CorrectHeader
(
err
))
_
,
_
=
fmt
.
Fprintln
(
w
,
"An error occured"
)
}
}
}
}
This diff is collapsed.
Click to expand it.
handler/diag.go
+
1
−
4
View file @
34eae9b9
...
@@ -3,7 +3,6 @@ package handler
...
@@ -3,7 +3,6 @@ package handler
import
(
import
(
"assignment-1/pkg"
"assignment-1/pkg"
"encoding/json"
"encoding/json"
"fmt"
"log"
"log"
"net/http"
"net/http"
)
)
...
@@ -12,8 +11,7 @@ func Dhandler(w http.ResponseWriter, r *http.Request) {
...
@@ -12,8 +11,7 @@ func Dhandler(w http.ResponseWriter, r *http.Request) {
var
diagnostics
pkg
.
Diag
var
diagnostics
pkg
.
Diag
if
err
:=
pkg
.
GetDiag
(
&
diagnostics
);
err
!=
nil
{
if
err
:=
pkg
.
GetDiag
(
&
diagnostics
);
err
!=
nil
{
w
.
WriteHeader
(
http
.
StatusBadGateway
)
http
.
Error
(
w
,
"An error occured"
,
http
.
StatusBadGateway
)
_
,
_
=
fmt
.
Fprintln
(
w
,
"An error occured"
)
}
}
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json"
)
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json"
)
...
@@ -21,4 +19,3 @@ func Dhandler(w http.ResponseWriter, r *http.Request) {
...
@@ -21,4 +19,3 @@ func Dhandler(w http.ResponseWriter, r *http.Request) {
log
.
Fatal
(
err
)
log
.
Fatal
(
err
)
}
}
}
}
This diff is collapsed.
Click to expand it.
handler/species.go
+
1
−
3
View file @
34eae9b9
...
@@ -20,9 +20,7 @@ func Shandler(w http.ResponseWriter, r *http.Request) {
...
@@ -20,9 +20,7 @@ func Shandler(w http.ResponseWriter, r *http.Request) {
log
.
Fatal
(
err
)
log
.
Fatal
(
err
)
}
}
}
else
{
}
else
{
w
.
WriteHeader
(
http
.
StatusBadGateway
)
w
.
WriteHeader
(
pkg
.
CorrectHeader
(
err
)
)
_
,
_
=
fmt
.
Fprintln
(
w
,
"An error occured"
)
_
,
_
=
fmt
.
Fprintln
(
w
,
"An error occured"
)
}
}
}
}
This diff is collapsed.
Click to expand it.
pkg/country.go
+
12
−
14
View file @
34eae9b9
...
@@ -12,6 +12,7 @@ type Country struct {
...
@@ -12,6 +12,7 @@ type Country struct {
}
}
type
tempCountry
struct
{
type
tempCountry
struct
{
Code
string
`json:"alpha2code"`
Flag
string
`json:"flag"`
Flag
string
`json:"flag"`
Name
string
`json:"name"`
Name
string
`json:"name"`
}
}
...
@@ -32,7 +33,6 @@ func GetCountryByCode(code, limit string) (Country, error) {
...
@@ -32,7 +33,6 @@ func GetCountryByCode(code, limit string) (Country, error) {
var
tmpC
tempCountry
var
tmpC
tempCountry
var
species
response
var
species
response
urlCountry
:=
countryApi
+
code
urlCountry
:=
countryApi
+
code
urlSpecies
:=
occurrenceApi
+
"country="
+
code
+
"&limit="
+
limit
// gets country info to temp struct
// gets country info to temp struct
err
:=
getBody
(
urlCountry
,
&
tmpC
)
err
:=
getBody
(
urlCountry
,
&
tmpC
)
...
@@ -43,8 +43,9 @@ func GetCountryByCode(code, limit string) (Country, error) {
...
@@ -43,8 +43,9 @@ func GetCountryByCode(code, limit string) (Country, error) {
// add tmp info to country struct
// add tmp info to country struct
country
.
CountryFlag
=
tmpC
.
Flag
country
.
CountryFlag
=
tmpC
.
Flag
country
.
CountryName
=
tmpC
.
Name
country
.
CountryName
=
tmpC
.
Name
country
.
Code
=
code
// was passed with the func call
country
.
Code
=
tmpC
.
Code
// was passed with the func call
urlSpecies
:=
occurrenceApi
+
"country="
+
country
.
Code
+
"&limit="
+
limit
// gets species in specified country
// gets species in specified country
err
=
getBody
(
urlSpecies
,
&
species
)
err
=
getBody
(
urlSpecies
,
&
species
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -67,6 +68,3 @@ func GetCountryByCode(code, limit string) (Country, error) {
...
@@ -67,6 +68,3 @@ func GetCountryByCode(code, limit string) (Country, error) {
return
country
,
nil
return
country
,
nil
}
}
This diff is collapsed.
Click to expand it.
pkg/funcs.go
+
17
−
5
View file @
34eae9b9
...
@@ -6,7 +6,6 @@ import (
...
@@ -6,7 +6,6 @@ import (
"net/http"
"net/http"
)
)
// allowing access to "overloaded" functions
// allowing access to "overloaded" functions
func
unfold
(
m
mashup
,
response
*
http
.
Response
)
error
{
func
unfold
(
m
mashup
,
response
*
http
.
Response
)
error
{
return
m
.
unmarshal
(
response
)
return
m
.
unmarshal
(
response
)
...
@@ -35,7 +34,6 @@ func (s *Specie) unmarshal(resp *http.Response) error {
...
@@ -35,7 +34,6 @@ func (s *Specie) unmarshal(resp *http.Response) error {
return
err
return
err
}
}
// parses httpresponse as a string used later for parsing
// parses httpresponse as a string used later for parsing
func
getBody
(
url
string
,
m
mashup
)
error
{
func
getBody
(
url
string
,
m
mashup
)
error
{
resp
,
err
:=
http
.
Get
(
url
)
resp
,
err
:=
http
.
Get
(
url
)
...
@@ -47,7 +45,21 @@ func getBody(url string, m mashup) error {
...
@@ -47,7 +45,21 @@ func getBody(url string, m mashup) error {
return
err
return
err
}
}
if
resp
.
StatusCode
!=
http
.
StatusOK
{
if
resp
.
StatusCode
!=
http
.
StatusOK
{
return
fmt
.
Errorf
(
"
status not 200 OK, got:
%d"
,
resp
.
StatusCode
)
return
fmt
.
Errorf
(
"%d"
,
resp
.
StatusCode
)
}
}
return
nil
return
nil
}
}
// func for returning correct http.Head response code
func
CorrectHeader
(
err
error
)
int
{
switch
err
.
Error
()
{
case
"400"
,
"404"
:
return
http
.
StatusBadGateway
case
"408"
:
return
http
.
StatusGatewayTimeout
default
:
return
http
.
StatusInternalServerError
}
}
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