Skip to content

Commit

Permalink
feat: Update user profile image (#302)
Browse files Browse the repository at this point in the history
* feat: Update user profile image

* Fix typo

Co-authored-by: Giannis Katsanos <[email protected]>

---------

Co-authored-by: Giannis Katsanos <[email protected]>
  • Loading branch information
spacycoder and gkats authored Jul 9, 2024
1 parent b5e437c commit ac7c642
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
5 changes: 5 additions & 0 deletions user/api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions user/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
package user

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"strconv"
Expand Down Expand Up @@ -112,6 +115,45 @@ func (c *Client) Update(ctx context.Context, id string, params *UpdateParams) (*
return resource, err
}

type UpdateProfileImageParams struct {
clerk.APIParams
File multipart.File `json:"-"`
}

func (params *UpdateProfileImageParams) ToMultipart() ([]byte, string, error) {
var buf bytes.Buffer
w := multipart.NewWriter(&buf)

file, err := w.CreateFormFile("file", "profileImage")
if err != nil {
return nil, "", err
}
defer params.File.Close()

_, err = io.Copy(file, params.File)
if err != nil {
return nil, "", err
}
err = w.Close()
if err != nil {
return nil, "", err
}
return buf.Bytes(), w.FormDataContentType(), nil
}

// UpdateProfileImage sets or replaces the user's profile image.
func (c *Client) UpdateProfileImage(ctx context.Context, id string, params *UpdateProfileImageParams) (*clerk.User, error) {
path, err := clerk.JoinPath(path, id, "/profile_image")
if err != nil {
return nil, err
}
req := clerk.NewMultipartAPIRequest(http.MethodPost, path)
req.SetParams(params)
resource := &clerk.User{}
err = c.Backend.Call(ctx, req, resource)
return resource, err
}

type UpdateMetadataParams struct {
clerk.APIParams
PublicMetadata *json.RawMessage `json:"public_metadata,omitempty"`
Expand Down
29 changes: 29 additions & 0 deletions user/client_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package user

import (
"bytes"
"context"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -182,6 +183,34 @@ func TestUserClientUpdate(t *testing.T) {
require.Equal(t, username, *user.Username)
}

type testFile struct {
bytes.Reader
}

func (_ *testFile) Close() error {
return nil
}

func TestUserClientUpdateProfileImage(t *testing.T) {
t.Parallel()
userID := "user_123"
config := &clerk.ClientConfig{}
config.HTTPClient = &http.Client{
Transport: &clerktest.RoundTripper{
T: t,
Out: json.RawMessage(fmt.Sprintf(`{"id":"%s"}`, userID)),
Method: http.MethodPost,
Path: "/v1/users/" + userID + "/profile_image",
},
}
client := NewClient(config)
user, err := client.UpdateProfileImage(context.Background(), userID, &UpdateProfileImageParams{
File: &testFile{Reader: *bytes.NewReader([]byte{})},
})
require.NoError(t, err)
require.Equal(t, userID, user.ID)
}

func TestUserClientUpdateMetadata(t *testing.T) {
t.Parallel()
id := "user_123"
Expand Down

0 comments on commit ac7c642

Please sign in to comment.