Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
Assignment 1
Manage
Activity
Members
Plan
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Repository 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
Aksel Baardsen
Assignment 1
Commits
075fea9a
Commit
075fea9a
authored
Oct 3, 2019
by
Aksel Baardsen
Browse files
Options
Downloads
Patches
Plain Diff
models function.go file, getSpecies works (janky)
parent
e0a96817
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
main.go
+4
-4
4 additions, 4 deletions
main.go
models/countries.go
+1
-34
1 addition, 34 deletions
models/countries.go
models/funcs.go
+40
-0
40 additions, 0 deletions
models/funcs.go
models/species.go
+24
-5
24 additions, 5 deletions
models/species.go
with
69 additions
and
43 deletions
main.go
+
4
−
4
View file @
075fea9a
...
...
@@ -9,6 +9,7 @@ import (
"net/http"
)
//returns json ready for parsing as string
func
main
()
{
r
:=
mux
.
NewRouter
()
...
...
@@ -67,10 +68,9 @@ func sHandler(w http.ResponseWriter, r *http.Request) {
vars
:=
mux
.
Vars
(
r
)
s
:=
models
.
GetSpecies
(
vars
[
"speciesKey"
])
s
pecie
:=
models
.
GetSpecies
(
vars
[
"speciesKey"
])
fmt
.
Fprintln
(
w
,
s
)
//plain keys
fmt
.
Fprintln
(
w
,
json
.
NewEncoder
(
w
)
.
Encode
(
s
))
s
,
_
:=
json
.
Marshal
(
specie
)
fmt
.
Fprintln
(
w
,
string
(
s
))
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
models/countries.go
+
1
−
34
View file @
075fea9a
...
...
@@ -2,9 +2,7 @@ package models
import
(
"encoding/json"
"io/ioutil"
"log"
"net/http"
)
const
countryApi
=
"https://restcountries.eu/rest/v2/alpha/"
...
...
@@ -34,8 +32,8 @@ type Results struct {
func
GetCountryByCode
(
code
,
limit
string
)
Country
{
// gets country info
var
country
Country
// gets country info
urlCountry
:=
countryApi
+
code
cBody
:=
getString
(
urlCountry
)
err
:=
json
.
Unmarshal
([]
byte
(
cBody
),
&
country
)
...
...
@@ -68,36 +66,5 @@ func GetCountryByCode(code, limit string) Country {
}
func
contains
(
s
[]
int
,
e
int
)
bool
{
for
_
,
a
:=
range
s
{
if
a
==
e
{
return
true
}
}
return
false
}
//returns json ready for parsing as string
func
getString
(
url
string
)
string
{
resp
,
err
:=
http
.
Get
(
url
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
defer
resp
.
Body
.
Close
()
var
bodyString
string
if
resp
.
StatusCode
==
http
.
StatusOK
{
bodyBytes
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
return
"err0r"
}
bodyString
=
string
(
bodyBytes
)
}
else
{
return
"err0r"
}
return
bodyString
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
models/funcs.go
0 → 100644
+
40
−
0
View file @
075fea9a
package
models
import
(
"io/ioutil"
"log"
"net/http"
)
func
getString
(
url
string
)
string
{
resp
,
err
:=
http
.
Get
(
url
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
defer
resp
.
Body
.
Close
()
var
bodyString
string
if
resp
.
StatusCode
==
http
.
StatusOK
{
bodyBytes
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
return
"err0r"
}
bodyString
=
string
(
bodyBytes
)
}
else
{
return
"err0r"
}
return
bodyString
}
func
contains
(
s
[]
int
,
e
int
)
bool
{
for
_
,
a
:=
range
s
{
if
a
==
e
{
return
true
}
}
return
false
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
models/species.go
+
24
−
5
View file @
075fea9a
...
...
@@ -2,7 +2,7 @@ package models
import
(
"encoding/json"
"
net/http
"
"
log
"
)
const
speciesApi
=
"http://api.gbif.org/v1/species/"
...
...
@@ -18,24 +18,43 @@ type Specie struct {
Species
string
`json:"species"`
ScientificName
string
`json:"scientificName"`
CanonicalName
string
`json:"canonicalName"`
extinct
bool
`json:"extinct
"`
Year
string
`json:"year
"`
}
func
GetSpecies
(
key
string
)
Specie
{
resp
,
err
:=
http
.
Get
(
speciesApi
+
key
+
"/"
)
var
specie
Specie
url
:=
speciesApi
+
key
sBody
:=
getString
(
url
)
err
:=
json
.
Unmarshal
([]
byte
(
sBody
),
&
specie
)
if
err
!=
nil
{
// handle pls
log
.
Fatal
(
err
)
}
url
=
url
+
"/name"
sBody
=
getString
(
url
)
err
=
json
.
Unmarshal
([]
byte
(
sBody
),
&
specie
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
/*
resp, err:= http.Get(speciesApi + key)
if err != nil {
log.Fatal(err)
}
specie := &Specie{}
err = json.NewDecoder(resp.Body).Decode(specie)
if err != nil {
// handle pls
}
defer resp.Body.Close()
*/
return
*
specie
return
specie
}
\ No newline at end of file
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