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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Andrea Magnussen
Assignment1
Commits
5abe3c27
Commit
5abe3c27
authored
Oct 10, 2019
by
andmag
Browse files
Options
Downloads
Patches
Plain Diff
species and speciesKey is in country struct
parent
f2a57b75
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
cmd/main.go
+0
-1
0 additions, 1 deletion
cmd/main.go
country.go
+14
-44
14 additions, 44 deletions
country.go
struct.go
+7
-19
7 additions, 19 deletions
struct.go
with
21 additions
and
64 deletions
cmd/main.go
+
0
−
1
View file @
5abe3c27
...
...
@@ -19,7 +19,6 @@ 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
+
14
−
44
View file @
5abe3c27
...
...
@@ -7,7 +7,7 @@ import (
"strings"
)
func
replyCountry
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
url
string
)
{
func
replyCountry
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
url
string
,
url2
string
)
{
http
.
Header
.
Add
(
w
.
Header
(),
"content-type"
,
"application/json"
)
...
...
@@ -26,6 +26,9 @@ func replyCountry(w http.ResponseWriter, r *http.Request, url string) {
country
:=
&
Country
{}
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
country
)
replySpeciesCountry
(
w
,
r
,
url2
,
country
)
json
.
NewEncoder
(
w
)
.
Encode
(
country
)
}
...
...
@@ -45,29 +48,16 @@ func HandlerCountry(w http.ResponseWriter, r *http.Request) {
}
APIURL
+=
parts
[
4
]
APIURL2
:=
"http://api.gbif.org/v1/occurrence/search?country="
APIURL2
+=
parts
[
4
]
replyCountry
(
w
,
r
,
APIURL
)
replyCountry
(
w
,
r
,
APIURL
,
APIURL2
)
}
// 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
,
"/"
)
if
len
(
parts
)
!=
5
||
parts
[
1
]
!=
"conservation"
||
parts
[
2
]
!=
"v1"
||
parts
[
3
]
!=
"spec"
||
parts
[
4
]
==
""
{
status
:=
http
.
StatusBadRequest
http
.
Error
(
w
,
"Expecting format /conservation/v1/spec/'alphacode'"
,
status
)
return
}
APIURL
+=
parts
[
4
]
func
replySpeciesCountry
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
url
string
,
country
*
Country
)
{
http
.
Header
.
Add
(
w
.
Header
(),
"content-type"
,
"application/json"
)
req
,
err
:=
http
.
NewRequest
(
http
.
MethodGet
,
APIURL
,
nil
)
req
,
err
:=
http
.
NewRequest
(
http
.
MethodGet
,
url
,
nil
)
if
err
!=
nil
{
fmt
.
Println
(
"Error"
,
err
)
}
...
...
@@ -82,13 +72,9 @@ func HandlerOnlySpecies(w http.ResponseWriter, r *http.Request) {
type
Result
Results
var
name
=
&
Result
{}
//v, _ := ioutil.ReadAll(resp.Body)
//fmt.Println("Body: ", string(v))
var
nameAndKey
=
&
Result
{}
err2
:=
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
name
)
err2
:=
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
name
AndKey
)
if
err2
!=
nil
{
fmt
.
Println
(
"Error"
,
err2
.
Error
())
}
...
...
@@ -96,30 +82,14 @@ func HandlerOnlySpecies(w http.ResponseWriter, r *http.Request) {
var
spec
[]
string
var
specKey
[]
int
for
k
,
v
:=
range
name
.
Res1
{
for
k
,
v
:=
range
name
AndKey
.
Res1
{
fmt
.
Println
(
k
)
fmt
.
Println
(
v
.
Species
)
spec
=
append
(
spec
,
v
.
Species
)
specKey
=
append
(
specKey
,
v
.
SpeciesKey
)
fmt
.
Println
(
v
.
SpeciesKey
)
/*for k1, v1 := range v {
fmt.Println(k1, v1)
}*/
fmt
.
Println
(
v
)
}
fmt
.
Println
(
spec
)
result
:=
Results2
{}
result
.
Res2
=
spec
result
.
Res3
=
specKey
err3
:=
json
.
NewEncoder
(
w
)
.
Encode
(
result
)
if
err3
!=
nil
{
fmt
.
Println
(
"error2"
,
err3
.
Error
())
}
country
.
Species
=
spec
country
.
SpeciesKey
=
specKey
}
This diff is collapsed.
Click to expand it.
struct.go
+
7
−
19
View file @
5abe3c27
...
...
@@ -5,30 +5,18 @@ type Country struct {
Code
string
`json:"alpha2Code"`
Countryname
string
`json:"name"`
Countryflag
string
`json:"flag"`
//
Species []
Spec
//
SpeciesKey []
Spec
Key
Species
[]
string
`json:"species"`
SpeciesKey
[]
int
`json:"species
Key
"`
}
// Name jdhgkjdng
type
Name
struct
{
type
Both
struct
{
Species
string
`json:"species"`
SpeciesKey
int
`json:"speciesKey"`
}
/*
type Key struct {
//SpeciesKey int `json:"speciesKey`
}
*/
type
Results2
struct
{
Res2
[]
string
`json:"species"`
Res3
[]
int
`json:"speciesKey"`
}
type
Results
struct
{
Res1
[]
Name
`json:"results"`
//Res2 []Key
Res1
[]
Both
`json:"results"`
}
// Species 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