Commit bb784ec5 authored by Aksel Baardsen's avatar Aksel Baardsen
Browse files

Species 'Key' is now returned as string

parent d77fea3a
package pkg
import "strconv"
const speciesApi = "http://api.gbif.org/v1/species/"
// struct for returning correct json keys
// struct for getting correct json keys
type Specie struct {
Key int `json:"key,omitempty"`
Kingdom string `json:"kingdom,omitempty"`
......@@ -15,31 +17,55 @@ type Specie struct {
Year string `json:"year,omitempty"`
}
// struct for returning proper format to caller (Key (int) => string): See README.md for assumptions
type SpecieReturn struct {
Key string `json:"key,omitempty"`
Kingdom string `json:"kingdom,omitempty"`
Phylum string `json:"phylum,omitempty"`
Order string `json:"order,omitempty"`
Family string `json:"family,omitempty"`
Genus string `json:"genus,omitempty"`
ScientificName string `json:"scientificName,omitempty"`
CanonicalName string `json:"canonicalName,omitempty"`
Year string `json:"year,omitempty"`
}
// gets all relevant species information
func GetSpecies(key string) (Specie, error) {
func GetSpecies(key string) (SpecieReturn, error) {
var specie Specie
url := speciesApi + key
// fills in the body of the specie, returns error & empty specie when appropriate
err := getBody(url, &specie)
if err != nil {
return Specie{}, err
return SpecieReturn{}, err
}
// supplies the json keys not provided in the first request (year)
url = url + "/name"
// using a temp-Specie to only get 'year', not replacing any other variable (just in case of varying info)
// using a temp-Specie to only get 'year', not replacing any other variable
// (just in case of varying info such as 'Key')
// I interpreted the task as only asking for 'year', not 'bracketYear' as well
var tempSpecie Specie
err = getBody(url, &tempSpecie)
if err != nil {
return Specie{}, err
return SpecieReturn{}, err
}
// sets year (if it occurred; has ",omitifempty" in json format)
specie.Year = tempSpecie.Year
// specie that has correct json format
returnVal := SpecieReturn{
Key: strconv.Itoa(specie.Key),
Kingdom: specie.Kingdom,
Phylum: specie.Phylum,
Order: specie.Order,
Family: specie.Family,
Genus: specie.Genus,
ScientificName: specie.ScientificName,
CanonicalName: specie.CanonicalName,
Year: tempSpecie.Year,
}
// all went well
return specie, nil
return returnVal, nil
}
\ 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