diff --git a/README.md b/README.md index 70a2d86..ab158a4 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ if err != nil { * [x] GET /api/v1/accounts/:id * [x] GET /api/v1/accounts/verify_credentials +* [ ] PATCH /api/v1/accounts/update_credentials * [x] GET /api/v1/accounts/:id/followers * [x] GET /api/v1/accounts/:id/following * [ ] GET /api/v1/accounts/:id/statuses diff --git a/accounts.go b/accounts.go index 3de8000..fa94a1f 100644 --- a/accounts.go +++ b/accounts.go @@ -46,6 +46,42 @@ func (c *Client) GetAccountCurrentUser() (*Account, error) { return &account, nil } +// Profile is a struct for updating profiles. +type Profile struct { + // If it is nil it will not be updated. + // If it is empty, update it with empty. + DisplayName *string + Note *string + + // Set the base64 encoded character string of the image. + Avatar string + Header string +} + +// AccountUpdate updates the information of the current user. +func (c *Client) AccountUpdate(profile *Profile) (*Account, error) { + params := url.Values{} + if profile.DisplayName != nil { + params.Set("display_name", *profile.DisplayName) + } + if profile.Note != nil { + params.Set("note", *profile.Note) + } + if profile.Avatar != "" { + params.Set("avatar", profile.Avatar) + } + if profile.Header != "" { + params.Set("header", profile.Header) + } + + var account Account + err := c.doAPI(http.MethodPatch, "/api/v1/accounts/update_credentials", params, &account) + if err != nil { + return nil, err + } + return &account, nil +} + // GetAccountFollowers return followers list. func (c *Client) GetAccountFollowers(id int64) ([]*Account, error) { var accounts []*Account diff --git a/accounts_test.go b/accounts_test.go index 0dc8a82..e7bc167 100644 --- a/accounts_test.go +++ b/accounts_test.go @@ -7,6 +7,33 @@ import ( "testing" ) +func TestAccountUpdate(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, `{"Username": "zzz"}`) + return + })) + defer ts.Close() + + client := NewClient(&Config{ + Server: ts.URL, + ClientID: "foo", + ClientSecret: "bar", + AccessToken: "zoo", + }) + a, err := client.AccountUpdate(&Profile{ + DisplayName: String("display_name"), + Note: String("note"), + Avatar: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUoAAADrCAYAAAA...", + Header: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUoAAADrCAYAAAA...", + }) + if err != nil { + t.Fatalf("should not be fail: %v", err) + } + if a.Username != "zzz" { + t.Fatalf("want %q but %q", "zzz", a.Username) + } +} + func TestGetBlocks(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, `[{"Username": "foo"}, {"Username": "bar"}]`) diff --git a/helper.go b/helper.go new file mode 100644 index 0000000..c9fefe9 --- /dev/null +++ b/helper.go @@ -0,0 +1,38 @@ +package mastodon + +import ( + "encoding/base64" + "net/http" + "os" +) + +// Base64EncodeFileName returns the base64 data URI format string of the file with the file name. +func Base64EncodeFileName(filename string) (string, error) { + file, err := os.Open(filename) + if err != nil { + return "", err + } + defer file.Close() + + return Base64Encode(file) +} + +// Base64Encode returns the base64 data URI format string of the file. +func Base64Encode(file *os.File) (string, error) { + fi, err := file.Stat() + if err != nil { + return "", err + } + + d := make([]byte, fi.Size()) + _, err = file.Read(d) + if err != nil { + return "", err + } + + return "data:" + http.DetectContentType(d) + + ";base64," + base64.StdEncoding.EncodeToString(d), nil +} + +// String is a helper function to get the pointer value of a string. +func String(v string) *string { return &v } diff --git a/helper_test.go b/helper_test.go new file mode 100644 index 0000000..a4a6429 --- /dev/null +++ b/helper_test.go @@ -0,0 +1,13 @@ +package mastodon + +import ( + "testing" +) + +func TestString(t *testing.T) { + s := "test" + sp := String(s) + if *sp != s { + t.Fatalf("want %q but %q", s, *sp) + } +} diff --git a/mastodon.go b/mastodon.go index a8660ab..e3fbb88 100644 --- a/mastodon.go +++ b/mastodon.go @@ -36,6 +36,9 @@ func (c *Client) doAPI(method string, uri string, params url.Values, res interfa return err } req.Header.Set("Authorization", "Bearer "+c.config.AccessToken) + if params != nil { + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + } resp, err = c.Do(req) if err != nil { return err