Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
Assignment 1 imt2681 cloud
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
Benjamin skinstad
Assignment 1 imt2681 cloud
Commits
e4ef2418
Commit
e4ef2418
authored
5 years ago
by
Benjamin skinstad
Browse files
Options
Downloads
Patches
Plain Diff
fixed canoical error
parent
31d838b9
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
main.go
+145
-115
145 additions, 115 deletions
main.go
with
145 additions
and
115 deletions
main.go
+
145
−
115
View file @
e4ef2418
...
...
@@ -17,11 +17,25 @@ import (
"net/http"
)
type
canonicalNameJson
struct
{
CanonicalName
float64
`json:"canonicalName"`
}
//struct for holding the species array in country
type
arraySpeciesJSON
struct
{
ArryJson
[]
speciesJSON2
`json:"results"`
}
type
arraySpeciesKeyJSON
struct
{
ArryJson
[]
speciskey
`json:"results"`
}
type
speciskey
struct
{
SpecKey
float64
`json:"speciesKey"`
}
//just a small struct used to make it easier to get year
type
specisYearJson
struct
{
...
...
@@ -34,7 +48,7 @@ type countryJSON struct {
Countryname
string
`json:"countryname"`
CountryFlag
string
`json:"countryflag"`
Species
[]
speciesJSON2
`json:"species"`
SpeciesKey
[]
specis
YearJson
`json:"speciesKey"`
SpeciesKey
[]
specis
key
`json:"speciesKey"`
}
...
...
@@ -52,6 +66,8 @@ type speciesJSON2 struct {
ScientificName
string
`json:"scientificName"`
CanonicalName
string
`json:"canonicalName"`
Year
float64
`json:"year"`
SpecKey
float64
`json:"speciesKey,omitempty"`
//used to get the specieskey to check for duplicates, will not be printed out
//due to omiteempty
}
type
speciesJSON
struct
{
...
...
@@ -124,11 +140,15 @@ func undefined(w http.ResponseWriter, r *http.Request){
func
country
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
){
var
input
=
r
.
URL
.
String
()
//takes in an raw url string to keep the question mark
filtered
:=
strings
.
Replace
(
input
,
countryFilter
,
""
,
-
1
)
//removes the unessesary parts of the url
if
filtered
==
""
{
io
.
WriteString
(
w
,
"404"
)
}
else
{
countryCode
:=
string
(
filtered
[
0
:
2
])
//saves away country parameter, its always 2 letters
filterNumber
:=
strings
.
Replace
(
filtered
,
countryCode
+
"&limit="
,
""
,
-
1
)
//removes more of the url to only get the filter number
...
...
@@ -142,8 +162,6 @@ func country(w http.ResponseWriter, r *http.Request){
}
}
resp
,
err
:=
http
.
Get
(
"https://restcountries.eu/rest/v2/alpha/"
+
countryCode
)
//get full cpountry json responce
if
err
!=
nil
{
print
(
"error
\n
"
)
...
...
@@ -177,18 +195,13 @@ func country(w http.ResponseWriter, r *http.Request){
// }
// var infoSpecis map[string]interface{}
// json.Unmarshal([]byte(bodySpecis),&infoSpecis)
// numberResponce := infoSpecis["limit"]
// var intNumberResponce int = int(numberResponce.(float64))
// var countryJsonArray []speciesJSON
/* var objmap map[string]*json.RawMessage
...
...
@@ -203,13 +216,10 @@ func country(w http.ResponseWriter, r *http.Request){
//err = json.Unmarshal(s[1], &test)
// str := fmt.Sprintf("%v", s)
// var test map[string]interface{}
// io.WriteString(w,string(str))
// str := fmt.Sprintf("%v", respSpecis.Body)
...
...
@@ -217,12 +227,12 @@ func country(w http.ResponseWriter, r *http.Request){
// println(str)
var
spectest
arraySpeciesJSON
// var canonicalNameTest canonicalNameJson
// var specKeyArray arraySpeciesKeyJSON
err
=
json
.
NewDecoder
(
respSpecis
.
Body
)
.
Decode
(
&
spectest
)
//decodes the json into spectest
if
err
!=
nil
{
print
(
"error
\n
"
)
log
.
Fatalln
(
err
)
...
...
@@ -230,16 +240,14 @@ func country(w http.ResponseWriter, r *http.Request){
}
var
countryJsonArray
[]
speciesJSON2
var
year
JsonArray
[]
specis
YearJson
var
key
JsonArray
[]
specis
key
var
duplicateCheck
=
""
//builds an string with all entires and checks for dupes after
intFilternumber
,
_
:=
strconv
.
Atoi
(
filterNumber
)
//go has some werid problems with conversion of types
for
i
:=
0
;
i
<
intFilternumber
;
i
++
{
keyTest
:=
fmt
.
Sprintf
(
"%f"
,
spectest
.
ArryJson
[
i
]
.
Key
)
//gets the key
keyTest
:=
fmt
.
Sprintf
(
"%f"
,
spectest
.
ArryJson
[
i
]
.
Spec
Key
)
//gets the key
if
strings
.
Contains
(
duplicateCheck
,
keyTest
)
{
//check if key already exists, go out of loop if it does
...
...
@@ -248,7 +256,6 @@ func country(w http.ResponseWriter, r *http.Request){
duplicateCheck
+=
keyTest
+
","
key
:=
spectest
.
ArryJson
[
i
]
.
Key
kingdom
:=
spectest
.
ArryJson
[
i
]
.
Kingdom
phylum
:=
spectest
.
ArryJson
[
i
]
.
Phylum
...
...
@@ -258,23 +265,56 @@ func country(w http.ResponseWriter, r *http.Request){
scientificName
:=
spectest
.
ArryJson
[
i
]
.
ScientificName
canonicalName
:=
spectest
.
ArryJson
[
i
]
.
CanonicalName
year
:=
spectest
.
ArryJson
[
i
]
.
Year
specKey
:=
spectest
.
ArryJson
[
i
]
.
SpecKey
if
canonicalName
==
""
{
/*
sometimes the json call does not contain canonicalName, and we have to run another request to gbif with the species key as paramter
*/
tempJson
:=
speciesJSON2
{
float64
(
key
),
kingdom
,
phylum
,
order
,
family
,
genus
,
scientificName
,
canonicalName
,
year
}
var
y
int
=
int
(
spectest
.
ArryJson
[
i
]
.
SpecKey
)
tempYearJson
:=
specisYearJson
{
key
}
countryJsonArray
=
append
(
countryJsonArray
,
tempJson
)
yearJsonArray
=
append
(
yearJsonArray
,
tempYearJson
)
specKeyString
:=
strconv
.
Itoa
(
y
)
canonicalNameResp
,
err
:=
http
.
Get
(
"http://api.gbif.org/v1/species/"
+
specKeyString
+
"/name"
)
if
err
!=
nil
{
print
(
"error
\n
"
)
log
.
Fatalln
(
err
)
}
defer
canonicalNameResp
.
Body
.
Close
()
canonicalNamebody
,
err
:=
ioutil
.
ReadAll
(
canonicalNameResp
.
Body
)
if
err
!=
nil
{
print
(
"error
\n
"
)
log
.
Fatalln
(
err
)
}
var
canonicalNameMap
map
[
string
]
interface
{}
json
.
Unmarshal
([]
byte
(
canonicalNamebody
),
&
canonicalNameMap
)
//again this poor implementation of json, but it works
println
(
specKeyString
)
canonicalName
=
canonicalNameMap
[
"canonicalName"
]
.
(
string
)
}
tempJson
:=
speciesJSON2
{
float64
(
key
),
kingdom
,
phylum
,
order
,
family
,
genus
,
scientificName
,
canonicalName
,
year
,
specKey
}
tempKeyJson
:=
speciskey
{
specKey
}
countryJsonArray
=
append
(
countryJsonArray
,
tempJson
)
keyJsonArray
=
append
(
keyJsonArray
,
tempKeyJson
)
}
defer
resp
.
Body
.
Close
()
body
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
...
...
@@ -289,16 +329,10 @@ func country(w http.ResponseWriter, r *http.Request){
//implementation.
json
.
Unmarshal
([]
byte
(
body
),
&
info
)
tempCode
:=
info
[
"alpha2Code"
]
tempCountryname
:=
info
[
"name"
]
tempCountryFlag
:=
info
[
"flag"
]
//this is so i can collapse this test /
{
if
tempCode
==
nil
{
...
...
@@ -313,21 +347,17 @@ func country(w http.ResponseWriter, r *http.Request){
}
createdJson
:=
countryJSON
{
tempCode
.
(
string
),
tempCountryname
.
(
string
),
tempCountryFlag
.
(
string
),
countryJsonArray
,
yearJsonArray
}
countryJsonArray
,
keyJsonArray
}
e
,
err
:=
json
.
Marshal
(
createdJson
)
// fmt.Fprint(w,(json.MarshalIndent(string(e), "", " ")))
w
.
Header
()
.
Add
(
"content-type"
,
"application/json"
)
io
.
WriteString
(
w
,
string
(
e
))
}
}
func
species
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
){
...
...
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