/
ndb.go
113 lines (96 loc) · 2.71 KB
/
ndb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//Package gondb provides structs and functions for accessing the National Nutrient Database for Standard Reference API.
//
//Successful API queries return native Go structs that can be used immediately,
//with no need for type assertions.
//
//Authentication
//
//A data.gov API key must be incorporated into each API request. Sign up (http://ndb.nal.usda.gov/ndb/doc/#) now if you do not have a key.
//
// api := NewClient(nil, "your-api-key")
//
//
//Queries
//
//Executing queries is simple.
//
// result, _ := api.Search("cheese", nil)
// for _, item := range result.Items {
// fmt.Println(item.Ndbno)
// }
//
//The endpoints allow separate optional parameter; if desired, these can be passed as the final parameter.
// v := url.Values{}
// v.Set("ndbno", "01009")
// v.Set("type", "f")
//
// nutrientIDs := []string{"204", "205", "269"}
//
// report, _ := api.GetNutrientReport(nutrientIDs, v)
//
//Endpoints
//
//gondb implements the endpoints defined in the documentation: http://ndb.nal.usda.gov/ndb/doc/.
//
//More detailed information about the behavior of each particular endpoint can be found at the official documentation.
package gondb
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
)
//BaseURL represents the base URL for API requests.
const (
BaseURL = "http://api.nal.usda.gov/ndb/"
)
//Client represents an NDB API client.
type Client struct {
HTTPClient *http.Client
//APIKey required to use the NDB API. Must be a data.gov registered API key.
APIKey string
}
//APIError represents an code/message error pair returned by the API.
type APIError struct {
Code string `json:"code"`
Message string `json:"message"`
}
//NewClient returns a new NDB API client. http.Default will be used if no httpClient is provided.
func NewClient(httpClient *http.Client, apiKey string) *Client {
if httpClient == nil {
httpClient = http.DefaultClient
}
if len(apiKey) == 0 {
panic("No API key was supplied. Get one at http://api.nal.usda.gov")
}
return &Client{
APIKey: apiKey,
HTTPClient: httpClient,
}
}
func (c *Client) apiGet(endpoint string, form url.Values, data interface{}) error {
if form == nil {
form = url.Values{}
}
form.Set("api_key", c.APIKey)
form.Set("format", "json")
urlStr := BaseURL + endpoint + form.Encode()
req, err := http.NewRequest("GET", urlStr, nil)
if err != nil {
return err
}
resp, err := c.HTTPClient.Do(req)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
var apiError map[string]APIError
json.NewDecoder(resp.Body).Decode(&apiError) //This ain't cool.
return apiError["error"]
}
defer resp.Body.Close()
return json.NewDecoder(resp.Body).Decode(data)
}
func (e APIError) Error() string {
return fmt.Sprintf("%s : %s", e.Code, e.Message)
}