From 8e6d0743c30505b3ba4337ccddc89cfe32662260 Mon Sep 17 00:00:00 2001 From: Steven Polley Date: Sun, 22 Jul 2018 03:35:55 -0600 Subject: [PATCH] Add Users and User-Metrics --- itglue/user-metrics.go | 53 +++++++++++++++++++++++++++++++++++ itglue/users.go | 63 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 itglue/user-metrics.go create mode 100644 itglue/users.go diff --git a/itglue/user-metrics.go b/itglue/user-metrics.go new file mode 100644 index 0000000..0a8e936 --- /dev/null +++ b/itglue/user-metrics.go @@ -0,0 +1,53 @@ +package itglue + +import ( + "encoding/json" + "fmt" +) + +type UserMetricData struct { + ID string `json:"id"` + Type string `json:"type"` + Attributes struct { + UserID int `json:"user-id"` + OrganizationID int `json:"organization-id"` + Created int `json:"created"` + Viewed int `json:"viewed"` + Edited int `json:"edited"` + Deleted int `json:"deleted"` + Date string `json:"date"` + ResourceType string `json:"resource-type"` + } `json:"attributes"` +} + +type UserMetric struct { + Data struct{ UserMetricData } `json:"data"` + Meta struct{ Metadata } `json:"metadata"` + Links struct{ Links } `json:"links"` +} + +type UserMetricList struct { + Data []struct{ UserMetricData } `json:"data"` + Meta struct{ Metadata } `json:"metadata"` + Links struct{ Links } `json:"links"` +} + +func (itg *ITGAPI) GetUserMetrics() (*UserMetricList, error) { + itgurl, err := itg.BuildURL("/user_metrics") + if err != nil { + return nil, fmt.Errorf("could not build URL: %s", err) + } + body, err := itg.GetRequest(itgurl) + + if err != nil { + return nil, fmt.Errorf("request failed: %s", err) + } + + userMetrics := &UserMetricList{} + err = json.Unmarshal(body, userMetrics) + if err != nil { + return nil, fmt.Errorf("could not get users: %s", err) + } + + return userMetrics, nil +} diff --git a/itglue/users.go b/itglue/users.go new file mode 100644 index 0000000..41fbd03 --- /dev/null +++ b/itglue/users.go @@ -0,0 +1,63 @@ +package itglue + +import ( + "encoding/json" + "fmt" + "time" +) + +type UserData struct { + ID string `json:"id"` + Type string `json:"type"` + Attributes struct { + FirstName string `json:"first-name"` + LastName string `json:"last-name"` + Name string `json:"name"` + RoleName string `json:"role-name"` + Email string `json:"email"` + InvitationSentAt time.Time `json:"invitation-sent-at"` + InvitationAcceptedAt time.Time `json:"invitation-accepted-at"` + CurrentSignInAt time.Time `json:"current-sign-in-at"` + CurrentSignInIP string `json:"current-sign-in-ip"` + LastSignInAt time.Time `json:"last-sign-in-at"` + LastSignInIP string `json:"last-sign-in-ip"` + Reputation int `json:"reputation"` + CreatedAt time.Time `json:"created-at"` + UpdatedAt time.Time `json:"updated-at"` + MyGlueAccountID interface{} `json:"my-glue-account-id"` + Avatar string `json:"avatar"` + MyGlue bool `json:"my-glue"` + } `json:"attributes"` +} + +type User struct { + Data struct{ UserData } `json:"data"` + Meta struct{ Metadata } `json:"metadata"` + Links struct{ Links } `json:"links"` +} + +type UserList struct { + Data []struct{ UserData } `json:"data"` + Meta struct{ Metadata } `json:"metadata"` + Links struct{ Links } `json:"links"` +} + +func (itg *ITGAPI) GetUsers() (*UserList, error) { + itgurl, err := itg.BuildURL("/users") + if err != nil { + return nil, fmt.Errorf("could not build URL: %s", err) + } + body, err := itg.GetRequest(itgurl) + + if err != nil { + return nil, fmt.Errorf("request failed: %s", err) + } + + users := &UserList{} + err = json.Unmarshal(body, users) + if err != nil { + return nil, fmt.Errorf("could not get users: %s", err) + } + + return users, nil +}