2024-03-03 18:34:26 -05:00
|
|
|
|
package users
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
2024-03-07 19:41:05 -05:00
|
|
|
|
"fmt"
|
2024-03-03 18:34:26 -05:00
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/google/go-querystring/query"
|
2024-03-07 20:52:42 -05:00
|
|
|
|
"go.fifitido.net/twitch/api/endpoint"
|
2024-03-03 18:34:26 -05:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type UpdateUserParams struct {
|
|
|
|
|
// The string to update the channel’s description to. The description is limited to a maximum of 300 characters.
|
|
|
|
|
//
|
|
|
|
|
// To remove the description, specify this parameter but don’t set it’s value (for example, ?description=).
|
|
|
|
|
Description *string `url:"description,omitempty"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type UpdateUserResponse struct {
|
|
|
|
|
// A list contains the single user that you updated.
|
|
|
|
|
Data []User `json:"data"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Updates the specified user’s information. The user ID in the OAuth token identifies the user whose information you want to update.
|
|
|
|
|
//
|
|
|
|
|
// To include the user’s verified email address in the response, the user access token must also include the user:read:email scope.
|
|
|
|
|
//
|
|
|
|
|
// Requires a user access token that includes the user:edit scope.
|
|
|
|
|
func (u *Users) UpdateUser(ctx context.Context, params *UpdateUserParams) (*UpdateUserResponse, error) {
|
|
|
|
|
v, _ := query.Values(params)
|
|
|
|
|
|
2024-03-07 20:52:42 -05:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, endpoint.Make(u.baseUrl, "users", v), nil)
|
2024-03-03 18:34:26 -05:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res, err := u.client.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
2024-03-07 19:41:05 -05:00
|
|
|
|
statusOK := res.StatusCode >= 200 && res.StatusCode < 300
|
|
|
|
|
if !statusOK {
|
|
|
|
|
return nil, fmt.Errorf("failed to update user (%d)", res.StatusCode)
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-03 18:34:26 -05:00
|
|
|
|
var data UpdateUserResponse
|
|
|
|
|
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &data, nil
|
|
|
|
|
}
|