go-twitch/api/users/update_user.go

56 lines
1.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package users
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/google/go-querystring/query"
"go.fifitido.net/twitch/api/endpoint"
)
type UpdateUserParams struct {
// The string to update the channels description to. The description is limited to a maximum of 300 characters.
//
// To remove the description, specify this parameter but dont set its 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 users information. The user ID in the OAuth token identifies the user whose information you want to update.
//
// To include the users 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)
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, endpoint.Make(u.baseUrl, "users", v), nil)
if err != nil {
return nil, err
}
res, err := u.client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
statusOK := res.StatusCode >= 200 && res.StatusCode < 300
if !statusOK {
return nil, fmt.Errorf("failed to update user (%d)", res.StatusCode)
}
var data UpdateUserResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}