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
Johannes Tangen
Assignment1
Commits
169d5383
Commit
169d5383
authored
5 years ago
by
jotangen
Browse files
Options
Downloads
Patches
Plain Diff
Trying to read gbif api and get it up to a server
parent
ad534dd3
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
assignment1
+0
-0
0 additions, 0 deletions
assignment1
webapp.go
+44
-17
44 additions, 17 deletions
webapp.go
with
44 additions
and
17 deletions
assignment1
+
0
−
0
View file @
169d5383
No preview for this file type
This diff is collapsed.
Click to expand it.
webapp.go
+
44
−
17
View file @
169d5383
...
...
@@ -3,8 +3,11 @@ package main
import
(
"encoding/json"
"fmt"
"log"
"net/http"
"io/ioutil"
)
//This works see below This https://restcountries.eu/rest/v2/name/ does not
const
apiRoot
=
"https://restcountries.eu/rest/v2/alpha/"
...
...
@@ -46,16 +49,7 @@ type SpeciesKeyBody struct{
func
getCountryName
(
cn
string
)
(
string
,
string
,
string
)
{
fmt
.
Println
(
"First in func"
,
cn
)
/*
{
"code": "<country code in 2-letter ISO format>",
"countryname": "<English human-readable country name>",
"countryflag": "<link to svg version of country flag>",
"species": [],
"speciesKey": []
}
*/
//Change hardcoded to func parameters "We pass them"
//countryCode := "NO"
...
...
@@ -79,6 +73,7 @@ func getCountryName(cn string) (string, string, string) {
//Closes when surrounded functions are finished
defer
resp
.
Body
.
Close
()
//Pointer to struct
ci
:=
&
CntrInfo
{}
...
...
@@ -91,33 +86,65 @@ func getCountryName(cn string) (string, string, string) {
}
// Should return species in exa: norway and key
func
getSpeciesByCountry
(
Ccode
string
)
(
string
,
int
){
func
getSpeciesByCountry
(
Ccode
string
)
(
string
,
int
)
{
url
:=
apiRootSpeciesInCountry
+
Ccode
resp
,
err
:=
http
.
Get
(
url
)
fmt
.
Printf
(
"This is our url for species %s
\n
and this is our resp %s
\n
"
,
url
,
resp
)
if
err
!=
nil
{
fmt
.
Println
(
"Error:we have a problem"
,
err
)
//Returns 0--> find better solution
return
""
,
0
}
defer
resp
.
Body
.
Close
()
//https://medium.com/@masnun/making-http-requests-in-golang-dd123379efe7
body
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
if
err
!=
nil
{
log
.
Fatalln
(
err
)
}
log
.
Println
(
string
(
body
))
crf
:=&
CollectionFromReq
{}
var
result
map
[
string
]
interface
{}
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
&
result
)
log
.
Println
(
result
[
"form"
])
//Pointer to struct
// will this also point to the slices and what the slices contain??
// Points to struct containing two slices of struct
crf
:=&
CollectionFromReq
{}
//fmt.Printf("The crf.Count is: "crf.Count)
//fmt.Printf("This is after crf %s\n", crf)
//resp.Body JSON from API
//Decode resp.Body and put into .decode(HERE)
//We need the resp.Body to be slices
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
crf
)
fmt
.
Println
(
"after json decoder"
,
resp
.
Body
)
keys
:=
make
([]
Result
,
0
)
for
i
:=
0
;
i
<
crf
.
Count
;
i
++
{
//we need to make crf.Count work
//crf.Count does not work https://gobyexample.com/range
//for _ , s := range {
//appends more capacity to our []Result
// keys = append(keys,s)
// }
//json.NewDecoder(resp.Body).Decode(crf)
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
&
keys
)
//fmt.Printf("after json decoder ----keys----",resp.Body)
fmt
.
Printf
(
"This is our keys %#v"
,
keys
)
fmt
.
Printf
(
"This is crf.count %d"
,
crf
.
Count
)
//for i := 0; i < 10; i++ {
// return crf.Collection[i].Species, crf.Collection[i].SpeciesKey
//}
return
""
,
crf
.
Count
return
crf
.
Collection
[
i
]
.
Species
,
crf
.
Collection
[
i
]
.
SpeciesKey
}
return
""
,
0
}
...
...
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