forked from XeroAPI/xerogolang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
88 lines (67 loc) · 2.71 KB
/
user.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
package accounting
import (
"encoding/json"
"time"
"github.com/XeroAPI/xerogolang"
"github.com/markbates/goth"
)
//User represents a login that has been granted access to the Xero organisation you're connected to
type User struct {
// Xero identifier
UserID string `json:"UserID,omitempty" xml:"UserID,omitempty"`
// Email address of user
EmailAddress string `json:"EmailAddress,omitempty" xml:"EmailAddress,omitempty"`
// First name of user
FirstName string `json:"FirstName,omitempty" xml:"FirstName,omitempty"`
// Last name of user
LastName string `json:"LastName,omitempty" xml:"LastName,omitempty"`
// Timestamp of last change to user
UpdatedDateUTC string `json:"UpdatedDateUTC,omitempty" xml:"UpdatedDateUTC,omitempty"`
// Boolean to indicate if user is the subscriber
IsSubscriber bool `json:"IsSubscriber,omitempty" xml:"IsSubscriber,omitempty"`
// User role (see Types)
OrganisationRole string `json:"OrganisationRole,omitempty" xml:"OrganisationRole,omitempty"`
}
//Users is a collection of Users
type Users struct {
Users []User `json:"Users,omitempty" xml:"User,omitempty"`
}
func unmarshalUsers(userResponseBytes []byte) (*Users, error) {
var userResponse *Users
err := json.Unmarshal(userResponseBytes, &userResponse)
if err != nil {
return nil, err
}
return userResponse, err
}
//FindUsersModifiedSince will get all users modified after a specified date
//additional querystringParameters such as where and order can be added as a map
func FindUsersModifiedSince(provider xerogolang.IProvider, session goth.Session, modifiedSince time.Time, querystringParameters map[string]string) (*Users, error) {
additionalHeaders := map[string]string{
"Accept": "application/json",
}
if !modifiedSince.Equal(dayZero) {
additionalHeaders["If-Modified-Since"] = modifiedSince.Format(time.RFC3339)
}
userResponseBytes, err := provider.Find(session, "Users", additionalHeaders, querystringParameters)
if err != nil {
return nil, err
}
return unmarshalUsers(userResponseBytes)
}
//FindUsers will get all users
//additional querystringParameters such as where and order can be added as a map
func FindUsers(provider xerogolang.IProvider, session goth.Session, querystringParameters map[string]string) (*Users, error) {
return FindUsersModifiedSince(provider, session, dayZero, querystringParameters)
}
//FindUser will get a single user - UserID must be a GUID for a user
func FindUser(provider xerogolang.IProvider, session goth.Session, userID string) (*Users, error) {
additionalHeaders := map[string]string{
"Accept": "application/json",
}
userResponseBytes, err := provider.Find(session, "Users/"+userID, additionalHeaders, nil)
if err != nil {
return nil, err
}
return unmarshalUsers(userResponseBytes)
}