| 1 | package slack |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/url" |
| 6 | "strconv" |
| 7 | ) |
| 8 | |
| 9 | const ( |
| 10 | DEFAULT_LOGINS_COUNT = 100 |
| 11 | DEFAULT_LOGINS_PAGE = 1 |
| 12 | ) |
| 13 | |
| 14 | type TeamResponse struct { |
| 15 | Team TeamInfo `json:"team"` |
| 16 | SlackResponse |
| 17 | } |
| 18 | |
| 19 | type TeamInfo struct { |
| 20 | ID string `json:"id"` |
| 21 | Name string `json:"name"` |
| 22 | Domain string `json:"domain"` |
| 23 | EmailDomain string `json:"email_domain"` |
| 24 | Icon map[string]interface{} `json:"icon"` |
| 25 | } |
| 26 | |
| 27 | type LoginResponse struct { |
| 28 | Logins []Login `json:"logins"` |
| 29 | Paging `json:"paging"` |
| 30 | SlackResponse |
| 31 | } |
| 32 | |
| 33 | type Login struct { |
| 34 | UserID string `json:"user_id"` |
| 35 | Username string `json:"username"` |
| 36 | DateFirst int `json:"date_first"` |
| 37 | DateLast int `json:"date_last"` |
| 38 | Count int `json:"count"` |
| 39 | IP string `json:"ip"` |
| 40 | UserAgent string `json:"user_agent"` |
| 41 | ISP string `json:"isp"` |
| 42 | Country string `json:"country"` |
| 43 | Region string `json:"region"` |
| 44 | } |
| 45 | |
| 46 | type BillableInfoResponse struct { |
| 47 | BillableInfo map[string]BillingActive `json:"billable_info"` |
| 48 | SlackResponse |
| 49 | } |
| 50 | |
| 51 | type BillingActive struct { |
| 52 | BillingActive bool `json:"billing_active"` |
| 53 | } |
| 54 | |
| 55 | // AccessLogParameters contains all the parameters necessary (including the optional ones) for a GetAccessLogs() request |
| 56 | type AccessLogParameters struct { |
| 57 | Count int |
| 58 | Page int |
| 59 | } |
| 60 | |
| 61 | // NewAccessLogParameters provides an instance of AccessLogParameters with all the sane default values set |
| 62 | func NewAccessLogParameters() AccessLogParameters { |
| 63 | return AccessLogParameters{ |
| 64 | Count: DEFAULT_LOGINS_COUNT, |
| 65 | Page: DEFAULT_LOGINS_PAGE, |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | func (api *Client) teamRequest(ctx context.Context, path string, values url.Values) (*TeamResponse, error) { |
| 70 | response := &TeamResponse{} |
| 71 | err := api.postMethod(ctx, path, values, response) |
| 72 | if err != nil { |
| 73 | return nil, err |
| 74 | } |
| 75 | |
| 76 | return response, response.Err() |
| 77 | } |
| 78 | |
| 79 | func (api *Client) billableInfoRequest(ctx context.Context, path string, values url.Values) (map[string]BillingActive, error) { |
| 80 | response := &BillableInfoResponse{} |
| 81 | err := api.postMethod(ctx, path, values, response) |
| 82 | if err != nil { |
| 83 | return nil, err |
| 84 | } |
| 85 | |
| 86 | return response.BillableInfo, response.Err() |
| 87 | } |
| 88 | |
| 89 | func (api *Client) accessLogsRequest(ctx context.Context, path string, values url.Values) (*LoginResponse, error) { |
| 90 | response := &LoginResponse{} |
| 91 | err := api.postMethod(ctx, path, values, response) |
| 92 | if err != nil { |
| 93 | return nil, err |
| 94 | } |
| 95 | return response, response.Err() |
| 96 | } |
| 97 | |
| 98 | // GetTeamInfo gets the Team Information of the user |
| 99 | func (api *Client) GetTeamInfo() (*TeamInfo, error) { |
| 100 | return api.GetTeamInfoContext(context.Background()) |
| 101 | } |
| 102 | |
| 103 | // GetTeamInfoContext gets the Team Information of the user with a custom context |
| 104 | func (api *Client) GetTeamInfoContext(ctx context.Context) (*TeamInfo, error) { |
| 105 | values := url.Values{ |
| 106 | "token": {api.token}, |
| 107 | } |
| 108 | |
| 109 | response, err := api.teamRequest(ctx, "team.info", values) |
| 110 | if err != nil { |
| 111 | return nil, err |
| 112 | } |
| 113 | return &response.Team, nil |
| 114 | } |
| 115 | |
| 116 | // GetAccessLogs retrieves a page of logins according to the parameters given |
| 117 | func (api *Client) GetAccessLogs(params AccessLogParameters) ([]Login, *Paging, error) { |
| 118 | return api.GetAccessLogsContext(context.Background(), params) |
| 119 | } |
| 120 | |
| 121 | // GetAccessLogsContext retrieves a page of logins according to the parameters given with a custom context |
| 122 | func (api *Client) GetAccessLogsContext(ctx context.Context, params AccessLogParameters) ([]Login, *Paging, error) { |
| 123 | values := url.Values{ |
| 124 | "token": {api.token}, |
| 125 | } |
| 126 | if params.Count != DEFAULT_LOGINS_COUNT { |
| 127 | values.Add("count", strconv.Itoa(params.Count)) |
| 128 | } |
| 129 | if params.Page != DEFAULT_LOGINS_PAGE { |
| 130 | values.Add("page", strconv.Itoa(params.Page)) |
| 131 | } |
| 132 | |
| 133 | response, err := api.accessLogsRequest(ctx, "team.accessLogs", values) |
| 134 | if err != nil { |
| 135 | return nil, nil, err |
| 136 | } |
| 137 | return response.Logins, &response.Paging, nil |
| 138 | } |
| 139 | |
| 140 | // GetBillableInfo ... |
| 141 | func (api *Client) GetBillableInfo(user string) (map[string]BillingActive, error) { |
| 142 | return api.GetBillableInfoContext(context.Background(), user) |
| 143 | } |
| 144 | |
| 145 | // GetBillableInfoContext ... |
| 146 | func (api *Client) GetBillableInfoContext(ctx context.Context, user string) (map[string]BillingActive, error) { |
| 147 | values := url.Values{ |
| 148 | "token": {api.token}, |
| 149 | "user": {user}, |
| 150 | } |
| 151 | |
| 152 | return api.billableInfoRequest(ctx, "team.billableInfo", values) |
| 153 | } |
| 154 | |
| 155 | // GetBillableInfoForTeam returns the billing_active status of all users on the team. |
| 156 | func (api *Client) GetBillableInfoForTeam() (map[string]BillingActive, error) { |
| 157 | return api.GetBillableInfoForTeamContext(context.Background()) |
| 158 | } |
| 159 | |
| 160 | // GetBillableInfoForTeamContext returns the billing_active status of all users on the team with a custom context |
| 161 | func (api *Client) GetBillableInfoForTeamContext(ctx context.Context) (map[string]BillingActive, error) { |
| 162 | values := url.Values{ |
| 163 | "token": {api.token}, |
| 164 | } |
| 165 | |
| 166 | return api.billableInfoRequest(ctx, "team.billableInfo", values) |
| 167 | } |