forked from rwestlund/quickbooks-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
class.go
81 lines (74 loc) · 1.96 KB
/
class.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
package quickbooks
import (
"encoding/json"
"net/http"
"net/url"
"strconv"
)
type Class struct {
ID string `json:"Id,omitempty"`
Name string `json:",omitempty"`
SyncToken string `json:",omitempty"`
ParentRef ParentRef `json:",omitempty"`
SubClass bool `json:",omitempty"`
FullyQualifiedName string `json:",omitempty"`
}
type ParentRef struct {
Value string `json:"value"`
}
// GetClasses fetches classes based on a page size
func (c *Client) GetClasses(startpos int, pagesize int) ([]Class, error) {
q := "SELECT * FROM Class ORDERBY Id STARTPOSITION " +
strconv.Itoa(startpos) + " MAXRESULTS " + strconv.Itoa(pagesize)
return c.QueryClasses(q)
}
// QueryClasses runs a select statement for classes
func (c *Client) QueryClasses(selectStatement string) ([]Class, error) {
var r struct {
QueryResponse struct {
Class []Class
StartPosition int `json:"startPosition"`
MaxResults int `json:"totalCount"`
}
}
err := c.query(selectStatement, &r)
if err != nil {
return nil, err
}
if r.QueryResponse.Class == nil {
r.QueryResponse.Class = make([]Class, 0)
}
return r.QueryResponse.Class, nil
}
// GetClassByID returns an account with a given ID.
func (c *Client) GetClassByID(id string) (*Class, error) {
var u, err = url.Parse(string(c.Endpoint))
if err != nil {
return nil, err
}
u.Path = "/v3/company/" + c.RealmID + "/class/" + id
var v = url.Values{}
v.Add("minorversion", minorVersion)
u.RawQuery = v.Encode()
var req *http.Request
req, err = http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/json")
var res *http.Response
res, err = c.Client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, parseFailure(res)
}
var r struct {
Class Class
Time Date
}
err = json.NewDecoder(res.Body).Decode(&r)
return &r.Class, err
}