From a25056d2ccc3b7252ce26252bec7c3b51bbeb95c Mon Sep 17 00:00:00 2001 From: 178inaba <178inaba@users.noreply.github.com> Date: Sun, 16 Apr 2017 00:47:23 +0900 Subject: [PATCH 1/6] Add Content-Type header to doAPI --- mastodon.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mastodon.go b/mastodon.go index 2919e66..06f460b 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 From 77ce92929093b31109737a31c894a4ca8fffb532 Mon Sep 17 00:00:00 2001 From: 178inaba <178inaba@users.noreply.github.com> Date: Sun, 16 Apr 2017 00:47:44 +0900 Subject: [PATCH 2/6] Add AccountUpdate --- accounts.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) 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 From 62318331a3e6f6aaf4c007bafd85610536858ac6 Mon Sep 17 00:00:00 2001 From: 178inaba <178inaba@users.noreply.github.com> Date: Sun, 16 Apr 2017 01:01:15 +0900 Subject: [PATCH 3/6] Add TestAccountUpdate --- accounts_test.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/accounts_test.go b/accounts_test.go index 0dc8a82..a369c3b 100644 --- a/accounts_test.go +++ b/accounts_test.go @@ -7,6 +7,35 @@ 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 String(v string) *string { return &v } + func TestGetBlocks(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, `[{"Username": "foo"}, {"Username": "bar"}]`) From e62f1adaae0719e54571dbb72d70357575a30547 Mon Sep 17 00:00:00 2001 From: 178inaba <178inaba@users.noreply.github.com> Date: Sun, 16 Apr 2017 02:00:30 +0900 Subject: [PATCH 4/6] Add helper --- accounts_test.go | 2 -- helper.go | 36 ++++++++++++++++++++++++++++++++++++ helper_test.go | 13 +++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 helper.go create mode 100644 helper_test.go diff --git a/accounts_test.go b/accounts_test.go index a369c3b..e7bc167 100644 --- a/accounts_test.go +++ b/accounts_test.go @@ -34,8 +34,6 @@ func TestAccountUpdate(t *testing.T) { } } -func String(v string) *string { return &v } - 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..b606695 --- /dev/null +++ b/helper.go @@ -0,0 +1,36 @@ +package mastodon + +import ( + "encoding/base64" + "net/http" + "os" +) + +func Base64EncodeFileName(filename string) (string, error) { + file, err := os.Open(filename) + if err != nil { + return "", err + } + defer file.Close() + + return Base64Encode(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) + } +} From 727e6d8c543307c823af702d16b38fc91fc77cd1 Mon Sep 17 00:00:00 2001 From: 178inaba <178inaba@users.noreply.github.com> Date: Sun, 16 Apr 2017 02:24:46 +0900 Subject: [PATCH 5/6] Add godoc to Base64EncodeFileName and Base64Encode --- helper.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/helper.go b/helper.go index b606695..c9fefe9 100644 --- a/helper.go +++ b/helper.go @@ -6,6 +6,7 @@ import ( "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 { @@ -16,6 +17,7 @@ func Base64EncodeFileName(filename string) (string, error) { 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 { From 2f4edf000c51f96fb0d2c7d2ec666776e26bbc06 Mon Sep 17 00:00:00 2001 From: 178inaba <178inaba@users.noreply.github.com> Date: Sun, 16 Apr 2017 03:04:33 +0900 Subject: [PATCH 6/6] Add update_credentials to status of implementations --- README.md | 1 + 1 file changed, 1 insertion(+) 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