Merge pull request #13 from 178inaba/update_credentials

Add AccountUpdate
pull/16/head
mattn 2017-04-16 20:01:22 +09:00 committed by GitHub
commit 0d53c41623
6 changed files with 118 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -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"}]`)

38
helper.go 100644
View File

@ -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 }

13
helper_test.go 100644
View File

@ -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)
}
}

View File

@ -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