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
1c48f086
Commit
1c48f086
authored
5 years ago
by
andmag
Browse files
Options
Downloads
Patches
Plain Diff
got year in species, structured code differently
parent
d4e18109
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
cmd/main.go
+1
-1
1 addition, 1 deletion
cmd/main.go
country.go
+52
-0
52 additions, 0 deletions
country.go
func.go
+81
-14
81 additions, 14 deletions
func.go
species.go
+72
-0
72 additions, 0 deletions
species.go
struct.go
+12
-8
12 additions, 8 deletions
struct.go
with
218 additions
and
23 deletions
cmd/main.go
+
1
−
1
View file @
1c48f086
...
...
@@ -18,7 +18,7 @@ func main() {
http
.
HandleFunc
(
"/"
,
assignment1
.
HandlerNil
)
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/diag/"
,
assignment1
.
HandlerDiag
)
fmt
.
Println
(
"Listening on port "
+
port
)
log
.
Fatal
(
http
.
ListenAndServe
(
":"
+
port
,
nil
))
...
...
This diff is collapsed.
Click to expand it.
country.go
0 → 100644
+
52
−
0
View file @
1c48f086
package
assignment1
import
(
"encoding/json"
"fmt"
"net/http"
"strings"
)
func
replyCountry
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
url
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
()
country
:=
&
Country
{}
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
country
)
json
.
NewEncoder
(
w
)
.
Encode
(
country
)
}
// 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
]
replyCountry
(
w
,
r
,
APIURL
)
}
This diff is collapsed.
Click to expand it.
func.go
+
81
−
14
View file @
1c48f086
package
assignment1
import
(
"encoding/json"
"fmt"
"net/http"
"strings"
)
// HandlerNil kjhfkerjhgk
...
...
@@ -13,7 +11,8 @@ func HandlerNil(w http.ResponseWriter, r *http.Request) {
http
.
Error
(
w
,
"Invalid request"
,
http
.
StatusBadRequest
)
}
func
replyRequest
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
url
string
,
c
string
)
{
/*
func replyRequest(w http.ResponseWriter, r *http.Request, url string, url2 string, c string) {
http.Header.Add(w.Header(), "content-type", "application/json")
...
...
@@ -36,13 +35,25 @@ func replyRequest(w http.ResponseWriter, r *http.Request, url string, c string)
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)
json
.
NewEncoder
(
w
)
.
Encode
(
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) {
...
...
@@ -53,19 +64,69 @@ func HandlerCountry(w http.ResponseWriter, r *http.Request) {
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
}
*/
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
)
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
}
...
...
@@ -78,8 +139,14 @@ func HandlerSpecies(w http.ResponseWriter, r *http.Request) {
parts := strings.Split(r.URL.Path, "/")
APIURL += parts[4]
c
:=
"species"
APIURL2 := APIURL + "/name"
replySpecies(w, r, APIURL, APIURL2)
}
*/
replyRequest
(
w
,
r
,
APIURL
,
c
)
// HandlerDiag dsjfbgdfjgb
func
HandlerDiag
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
}
This diff is collapsed.
Click to expand it.
species.go
0 → 100644
+
72
−
0
View file @
1c48f086
package
assignment1
import
(
"encoding/json"
"fmt"
"net/http"
"strings"
)
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
)
}
This diff is collapsed.
Click to expand it.
struct.go
+
12
−
8
View file @
1c48f086
...
...
@@ -11,16 +11,20 @@ type Country struct {
// Species comment endpoint
type
Species
struct
{
Key
int
`json:"key"`
Kingdom
string
`json:"kingdom"`
Phylum
string
`json:"phylum"`
Order
string
`json:"order"`
Family
string
`json:"family"`
Genus
string
`json:"genus"`
//SpeciesName string `json:"genericName"`
Key
int
`json:"key"`
Kingdom
string
`json:"kingdom"`
Phylum
string
`json:"phylum"`
Order
string
`json:"order"`
Family
string
`json:"family"`
Genus
string
`json:"genus"`
ScientificName
string
`json:"scientificName"`
CanonicalName
string
`json:"canonicalName"`
//Extinct bool `json:"extinct"`
Year
string
`json:"year"`
}
// Year lkerjgflkjtgj
type
Year
struct
{
Year
string
`json:"bracketYear"`
}
// Diag comment endpoint
...
...
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