69 lines
2.1 KiB
Go
69 lines
2.1 KiB
Go
package users
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"io"
|
||
"log/slog"
|
||
"net/http"
|
||
"net/url"
|
||
|
||
"github.com/google/go-querystring/query"
|
||
)
|
||
|
||
type GetUsersParams struct {
|
||
// The ID of the user to get.
|
||
// To specify more than one user, include the id parameter for each user to get. For example, id=1234&id=5678.
|
||
// The maximum number of IDs you may specify is 100.
|
||
IDs []string `url:"id,omitempty"`
|
||
|
||
// The login name of the user to get.
|
||
// To specify more than one user, include the login parameter for each user to get. For example, login=foo&login=bar.
|
||
// The maximum number of login names you may specify is 100.
|
||
Logins []string `url:"login,omitempty"`
|
||
}
|
||
|
||
type GetUsersResponse struct {
|
||
// The list of users.
|
||
Data []User `json:"data"`
|
||
}
|
||
|
||
// Gets information about one or more users.
|
||
//
|
||
// You may look up users using their user ID, login name, or both but the sum total of the number of users you may look up is 100.
|
||
// For example, you may specify 50 IDs and 50 names or 100 IDs or names, but you cannot specify 100 IDs and 100 names.
|
||
//
|
||
// If you don’t specify IDs or login names, the request returns information about the user in the access token if you specify a user access token.
|
||
//
|
||
// To include the user’s verified email address in the response, you must use a user access token that includes the user:read:email scope.
|
||
//
|
||
// Requires an app access token or user access token.
|
||
func (u *Users) GetUsers(ctx context.Context, params *GetUsersParams) (*GetUsersResponse, error) {
|
||
v, _ := query.Values(params)
|
||
endpoint := u.baseUrl.ResolveReference(&url.URL{Path: "users", RawQuery: v.Encode()})
|
||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), nil)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
res, err := u.client.Do(req)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
defer res.Body.Close()
|
||
|
||
body, err := io.ReadAll(res.Body)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
var data GetUsersResponse
|
||
if err := json.Unmarshal(body, &data); err != nil {
|
||
slog.Debug("failed to unmarshal", "error", err, "body", string(body))
|
||
return nil, err
|
||
}
|
||
|
||
return &data, nil
|
||
}
|