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
2247f095
Commit
2247f095
authored
5 years ago
by
Aksel Baardsen
Browse files
Options
Downloads
Patches
Plain Diff
replaced marshalling with decode && introduced interface for cleaner code
parent
caa22088
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
pkg/country.go
+6
-13
6 additions, 13 deletions
pkg/country.go
pkg/funcs.go
+42
-5
42 additions, 5 deletions
pkg/funcs.go
pkg/species.go
+17
-12
17 additions, 12 deletions
pkg/species.go
with
65 additions
and
30 deletions
pkg/country.go
+
6
−
13
View file @
2247f095
package
pkg
import
(
"encoding/json"
"log"
)
...
...
@@ -34,21 +33,15 @@ func GetCountryByCode(code, limit string) (Country, int) {
urlSpecies
:=
occurrenceApi
+
"country="
+
code
+
"&limit="
+
limit
// gets country info
cBody
,
status
:=
getString
(
urlCountry
)
if
status
!=
200
{
return
country
,
status
}
err
:=
json
.
Unmarshal
([]
byte
(
cBody
),
&
country
)
resp
,
err
:=
getBody
(
urlCountry
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
Unfold
(
&
country
,
resp
)
// gets species in that country
rBody
,
status
:=
getString
(
urlSpecies
)
if
status
!=
200
{
return
country
,
status
}
err
=
json
.
Unmarshal
([]
byte
(
rBody
),
&
species
)
resp
,
err
=
getBody
(
urlSpecies
)
Unfold
(
&
species
,
resp
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
...
...
@@ -57,14 +50,14 @@ func GetCountryByCode(code, limit string) (Country, int) {
check
:=
make
(
map
[
Results
]
bool
)
for
i
,
_
:=
range
species
.
Results
{
if
check
[
species
.
Results
[
i
]]
{
continue
continue
//[continues the loop] replace check with ! and add content from below
}
check
[
species
.
Results
[
i
]]
=
true
country
.
SpeciesKey
=
append
(
country
.
SpeciesKey
,
species
.
Results
[
i
]
.
SpeciesKey
)
country
.
Species
=
append
(
country
.
Species
,
species
.
Results
[
i
]
.
Species
)
}
return
country
,
status
return
country
,
200
}
...
...
This diff is collapsed.
Click to expand it.
pkg/funcs.go
+
42
−
5
View file @
2247f095
package
pkg
import
(
"
io/ioutil
"
"
encoding/json
"
"log"
"net/http"
)
func
getString
(
url
string
)
(
string
,
int
)
{
resp
,
err
:=
http
.
Get
(
url
)
func
Unfold
(
m
Mashup
,
response
*
http
.
Response
)
{
m
.
unmarshal
(
response
)
}
type
Mashup
interface
{
unmarshal
(
response
*
http
.
Response
)
}
func
(
c
*
Country
)
unmarshal
(
resp
*
http
.
Response
)
{
err
:=
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
&
c
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
defer
resp
.
Body
.
Close
()
}
func
(
r
*
Response
)
unmarshal
(
resp
*
http
.
Response
)
{
err
:=
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
&
r
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
defer
resp
.
Body
.
Close
()
}
func
(
s
*
Specie
)
unmarshal
(
resp
*
http
.
Response
)
()
{
err
:=
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
&
s
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
defer
resp
.
Body
.
Close
()
}
// parses httpresponse as a string used later for parsing
func
getBody
(
url
string
)
(
*
http
.
Response
,
error
)
{
resp
,
err
:=
http
.
Get
(
url
)
if
err
!=
nil
{
return
nil
,
err
}
return
resp
,
nil
/*
var bodyString string
if resp.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(resp.Body)
...
...
@@ -23,11 +58,13 @@ func getString(url string) (string, int) {
}
return bodyString, resp.StatusCode
*/
}
/*
funcs for testing purposes
*/
func GetString(url string) (string, int) {
return getString(url)
}
\ No newline at end of file
}*/
\ No newline at end of file
This diff is collapsed.
Click to expand it.
pkg/species.go
+
17
−
12
View file @
2247f095
package
pkg
import
(
"encoding/json"
"log"
)
import
"log"
const
speciesApi
=
"http://api.gbif.org/v1/species/"
...
...
@@ -23,24 +20,32 @@ type Specie struct {
func
GetSpecies
(
key
string
)
(
Specie
,
int
)
{
var
specie
Specie
url
:=
speciesApi
+
key
sBody
,
code
:=
get
String
(
url
)
if
code
!=
200
{
return
specie
,
code
resp
,
err
:=
get
Body
(
url
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
Unfold
(
&
specie
,
resp
)
/*
err := json.Unmarshal([]byte(sBody), &specie)
if err != nil {
log.Fatal(err)
}
*/
url
=
url
+
"/name"
sBody
,
code
=
getString
(
url
)
if
code
!=
200
{
return
specie
,
code
resp
,
err
=
getBody
(
url
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
Unfold
(
&
specie
,
resp
)
/*
err = json.Unmarshal([]byte(sBody), &specie)
if err != nil {
log.Fatal(err)
}
*/
return
specie
,
code
return
specie
,
200
}
\ 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