Commit 2247f095 authored by Aksel Baardsen's avatar Aksel Baardsen
Browse files

replaced marshalling with decode && introduced interface for cleaner code

parent caa22088
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
}
......
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
package pkg
import (
"encoding/json"
"log"
)
import "log"
const speciesApi = "http://api.gbif.org/v1/species/"
const speciesApi = "http://api.gbif.org/v1/species/"
type Specie struct {
Key int `json:"key"`
......@@ -23,24 +20,32 @@ type Specie struct {
func GetSpecies(key string) (Specie, int) {
var specie Specie
url := speciesApi + key
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)
}
*/
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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment