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
5be4f0af
Commit
5be4f0af
authored
5 years ago
by
andmag
Browse files
Options
Downloads
Patches
Plain Diff
diag added, optimalization
parent
1c48f086
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
cmd/main.go
+1
-0
1 addition, 0 deletions
cmd/main.go
country.go
+34
-1
34 additions, 1 deletion
country.go
diagnostics.go
+33
-0
33 additions, 0 deletions
diagnostics.go
func.go
+0
-152
0 additions, 152 deletions
func.go
species.go
+25
-36
25 additions, 36 deletions
species.go
struct.go
+11
-4
11 additions, 4 deletions
struct.go
with
104 additions
and
193 deletions
cmd/main.go
+
1
−
0
View file @
5be4f0af
...
...
@@ -19,6 +19,7 @@ func main() {
http
.
HandleFunc
(
"/conservation/v1/country/"
,
assignment1
.
HandlerCountry
)
http
.
HandleFunc
(
"/conservation/v1/species/"
,
assignment1
.
HandlerSpecies
)
http
.
HandleFunc
(
"/conservation/v1/diag/"
,
assignment1
.
HandlerDiag
)
//http.HandleFunc("/conservation/v1/species2/", assignment1.HandlerOnlySpecies)
fmt
.
Println
(
"Listening on port "
+
port
)
log
.
Fatal
(
http
.
ListenAndServe
(
":"
+
port
,
nil
))
...
...
This diff is collapsed.
Click to expand it.
country.go
+
34
−
1
View file @
5be4f0af
...
...
@@ -38,7 +38,6 @@ func HandlerCountry(w http.ResponseWriter, r *http.Request) {
//SJEKK LENGDE 4
parts
:=
strings
.
Split
(
r
.
URL
.
Path
,
"/"
)
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
)
...
...
@@ -50,3 +49,37 @@ func HandlerCountry(w http.ResponseWriter, r *http.Request) {
replyCountry
(
w
,
r
,
APIURL
)
}
/*
// HandlerOnlySpecies skjdfhkshfkl
func HandlerOnlySpecies(w http.ResponseWriter, r *http.Request) {
APIURL := "http://api.gbif.org/v1/occurrence/search?country="
parts := strings.Split(r.URL.Path, "/")
APIURL += parts[4]
http.Header.Add(w.Header(), "content-type", "application/json")
req, err := http.NewRequest(http.MethodGet, APIURL, nil)
if err != nil {
fmt.Println("Error", err)
}
client := http.DefaultClient
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error", err)
}
defer resp.Body.Close()
var species []SpeciesByCountry
//species := []SpeciesByCountry{}
//mySpeciesList := make([]SBCList, 0)
json.NewDecoder(resp.Body).Decode(&species)
json.NewEncoder(w).Encode(species)
}
*/
This diff is collapsed.
Click to expand it.
diagnostics.go
0 → 100644
+
33
−
0
View file @
5be4f0af
package
assignment1
import
(
"encoding/json"
"fmt"
"net/http"
)
// HandlerNil kjhfkerjhgk
func
HandlerNil
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
fmt
.
Println
(
"Default Handler: Invalid request received."
)
http
.
Error
(
w
,
"Invalid request"
,
http
.
StatusBadRequest
)
}
// HandlerDiag dsjfbgdfjgb
func
HandlerDiag
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
http
.
Header
.
Add
(
w
.
Header
(),
"content-type"
,
"application/json"
)
diagnostics
:=
&
Diag
{}
diagnostics
.
Version
=
"v1"
diagnostics
.
Restcountries
=
getStatusCode
(
"https://restcountries.eu/"
)
diagnostics
.
Gbif
=
getStatusCode
(
"http://api.gbif.org/v1/"
)
json
.
NewEncoder
(
w
)
.
Encode
(
diagnostics
)
}
func
getStatusCode
(
URL
string
)
int
{
resp
,
err
:=
http
.
Get
(
URL
)
if
err
!=
nil
{
panic
(
err
)
}
return
resp
.
StatusCode
}
This diff is collapsed.
Click to expand it.
func.go
deleted
100644 → 0
+
0
−
152
View file @
1c48f086
package
assignment1
import
(
"fmt"
"net/http"
)
// HandlerNil kjhfkerjhgk
func
HandlerNil
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
fmt
.
Println
(
"Default Handler: Invalid request received."
)
http
.
Error
(
w
,
"Invalid request"
,
http
.
StatusBadRequest
)
}
/*
func replyRequest(w http.ResponseWriter, r *http.Request, url string, url2 string, c string) {
http.Header.Add(w.Header(), "content-type", "application/json")
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
fmt.Println("Error", err)
}
client := http.DefaultClient
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error", err)
}
defer resp.Body.Close()
switch c {
case "country":
country := &Country{}
json.NewDecoder(resp.Body).Decode(country)
json.NewEncoder(w).Encode(country)
case "species":
species := &Species{}
year := &Year{}
species.Year = year.Year
json.NewDecoder(resp.Body).Decode(species)
//both := &Both{species.Key, species.Kingdom, species.Phylum, species.Order, species.Family, species.Genus, species.ScientificName, species.CanonicalName, year.Year}
both := dealWithYear(species, url2)
json.NewEncoder(w).Encode(year)
}
}
*/
/*
// HandlerCountry dskjfhskjfhk
func HandlerCountry(w http.ResponseWriter, r *http.Request) {
APIURL := "https://restcountries.eu/rest/v2/alpha/"
//SJEKK LENGDE 4
parts := strings.Split(r.URL.Path, "/")
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)
return
}
APIURL += parts[4]
c := "country"
replyRequest(w, r, APIURL, "", c)
}
*/
/*
func replySpecies(w http.ResponseWriter, r *http.Request, url string, url2 string) {
species := &Species{}
http.Header.Add(w.Header(), "content-type", "application/json")
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
fmt.Println("Error", err)
}
client := http.DefaultClient
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error", err)
}
defer resp.Body.Close()
json.NewDecoder(resp.Body).Decode(species)
replyWithYear(species, url2)
json.NewEncoder(w).Encode(species)
}
func replyWithYear(species *Species, url string) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
fmt.Println("Error", err)
}
client := http.DefaultClient
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error", err)
}
defer resp.Body.Close()
year := &Year{}
json.NewDecoder(resp.Body).Decode(&year)
species.Year = year.Year
}
// HandlerSpecies dfkjekjgh
func HandlerSpecies(w http.ResponseWriter, r *http.Request) {
APIURL := "http://api.gbif.org/v1/species/"
//SJEKK LENGDE 4
parts := strings.Split(r.URL.Path, "/")
APIURL += parts[4]
APIURL2 := APIURL + "/name"
replySpecies(w, r, APIURL, APIURL2)
}
*/
// HandlerDiag dsjfbgdfjgb
func
HandlerDiag
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
}
This diff is collapsed.
Click to expand it.
species.go
+
25
−
36
View file @
5be4f0af
...
...
@@ -7,33 +7,35 @@ import (
"strings"
)
func
replySpecies
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
url
string
,
url2
string
)
{
species
:=
&
Species
{}
// HandlerSpecies kdsjfhkdjghlfdkhn
func
HandlerSpecies
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
http
.
Header
.
Add
(
w
.
Header
(),
"content-type"
,
"application/json"
)
req
,
err
:=
http
.
NewRequest
(
http
.
MethodGet
,
url
,
nil
)
if
err
!=
nil
{
fmt
.
Println
(
"Error"
,
err
)
}
species
:=
&
Species
{}
client
:=
http
.
DefaultClient
resp
,
err
:=
client
.
Do
(
req
)
if
err
!=
nil
{
fmt
.
Println
(
"Error"
,
err
)
APIURL
:=
"http://api.gbif.org/v1/species/"
parts
:=
strings
.
Split
(
r
.
URL
.
Path
,
"/"
)
if
len
(
parts
)
!=
5
||
parts
[
1
]
!=
"conservation"
||
parts
[
2
]
!=
"v1"
||
parts
[
3
]
!=
"species"
||
parts
[
4
]
==
""
{
status
:=
http
.
StatusBadRequest
http
.
Error
(
w
,
"Expecting format /conservation/v1/species/'speciesNumber'"
,
status
)
return
}
defer
resp
.
Body
.
Close
()
APIURL
+=
parts
[
4
]
APIURL2
:=
APIURL
+
"/name"
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
species
)
s
:=
"species"
y
:=
"year"
replyWithYear
(
species
,
url2
)
replySpecies
(
w
,
r
,
APIURL
,
s
,
species
)
replySpecies
(
w
,
r
,
APIURL2
,
y
,
species
)
json
.
NewEncoder
(
w
)
.
Encode
(
species
)
}
func
reply
WithYear
(
species
*
Speci
es
,
url
string
)
{
func
reply
Species
(
w
http
.
ResponseWriter
,
r
*
http
.
Requ
es
t
,
url
string
,
str
string
,
species
*
Species
)
{
req
,
err
:=
http
.
NewRequest
(
http
.
MethodGet
,
url
,
nil
)
if
err
!=
nil
{
...
...
@@ -48,25 +50,12 @@ func replyWithYear(species *Species, url string) {
defer
resp
.
Body
.
Close
()
year
:=
&
Year
{}
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
&
year
)
species
.
Year
=
year
.
Year
}
// HandlerSpecies dfkjekjgh
func
HandlerSpecies
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
APIURL
:=
"http://api.gbif.org/v1/species/"
//SJEKK LENGDE 4
parts
:=
strings
.
Split
(
r
.
URL
.
Path
,
"/"
)
APIURL
+=
parts
[
4
]
APIURL2
:=
APIURL
+
"/name"
replySpecies
(
w
,
r
,
APIURL
,
APIURL2
)
switch
str
{
case
"species"
:
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
&
species
)
case
"year"
:
year
:=
&
Year
{}
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
&
year
)
species
.
Year
=
year
.
Year
}
}
This diff is collapsed.
Click to expand it.
struct.go
+
11
−
4
View file @
5be4f0af
...
...
@@ -9,6 +9,13 @@ type Country struct {
//Speciescategorycount string `json:"`
}
/*
type SpeciesByCountry struct {
Species string `json:"species"`
SpeciesKey int `json:"speciesKey"`
}
*/
// Species comment endpoint
type
Species
struct
{
Key
int
`json:"key"`
...
...
@@ -29,8 +36,8 @@ type Year struct {
// Diag comment endpoint
type
Diag
struct
{
Gbif
string
`json:""`
Restcountries
string
`json:""`
Version
string
`json:""`
uptime
string
`json:""`
Gbif
int
Restcountries
int
Version
string
uptime
int
}
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