add UploadMedia
This commit is contained in:
parent
959459eb42
commit
af67397430
47
mastodon.go
47
mastodon.go
|
@ -1,12 +1,17 @@
|
||||||
package mastodon
|
package mastodon
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -24,21 +29,53 @@ type Client struct {
|
||||||
config *Config
|
config *Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) doAPI(ctx context.Context, method string, uri string, params url.Values, res interface{}) error {
|
func (c *Client) doAPI(ctx context.Context, method string, uri string, params interface{}, res interface{}) error {
|
||||||
u, err := url.Parse(c.config.Server)
|
u, err := url.Parse(c.config.Server)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
u.Path = path.Join(u.Path, uri)
|
u.Path = path.Join(u.Path, uri)
|
||||||
|
|
||||||
req, err := http.NewRequest(method, u.String(), strings.NewReader(params.Encode()))
|
var req *http.Request
|
||||||
if err != nil {
|
ct := "application/x-www-form-urlencoded"
|
||||||
return err
|
if values, ok := params.(url.Values); ok {
|
||||||
|
req, err = http.NewRequest(method, u.String(), strings.NewReader(values.Encode()))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else if file, ok := params.(string); ok {
|
||||||
|
f, err := os.Open(file)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
var buf bytes.Buffer
|
||||||
|
mw := multipart.NewWriter(&buf)
|
||||||
|
part, err := mw.CreateFormFile("file", filepath.Base(file))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = io.Copy(part, f)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = mw.Close()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
req, err = http.NewRequest(method, u.String(), &buf)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
ct = mw.FormDataContentType()
|
||||||
|
} else {
|
||||||
|
req, err = http.NewRequest(method, u.String(), nil)
|
||||||
}
|
}
|
||||||
req.WithContext(ctx)
|
req.WithContext(ctx)
|
||||||
req.Header.Set("Authorization", "Bearer "+c.config.AccessToken)
|
req.Header.Set("Authorization", "Bearer "+c.config.AccessToken)
|
||||||
if params != nil {
|
if params != nil {
|
||||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
req.Header.Set("Content-Type", ct)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := c.Do(req)
|
resp, err := c.Do(req)
|
||||||
|
|
10
status.go
10
status.go
|
@ -201,3 +201,13 @@ func (c *Client) Search(ctx context.Context, q string, resolve bool) (*Results,
|
||||||
}
|
}
|
||||||
return &results, nil
|
return &results, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PostMedia upload a media attachment.
|
||||||
|
func (c *Client) UploadMedia(ctx context.Context, file string) (*Attachment, error) {
|
||||||
|
var attachment Attachment
|
||||||
|
err := c.doAPI(ctx, http.MethodPost, "/api/v1/media", file, &attachment)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &attachment, nil
|
||||||
|
}
|
||||||
|
|
|
@ -257,3 +257,33 @@ func TestUnfavourite(t *testing.T) {
|
||||||
t.Fatalf("want %q but %q", "zzz", status.Content)
|
t.Fatalf("want %q but %q", "zzz", status.Content)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUploadMedia(t *testing.T) {
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.URL.Path != "/api/v1/media" {
|
||||||
|
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if r.Method != "POST" {
|
||||||
|
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Fprintln(w, `{"ID": 123}`)
|
||||||
|
return
|
||||||
|
}))
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
client := NewClient(&Config{
|
||||||
|
Server: ts.URL,
|
||||||
|
ClientID: "foo",
|
||||||
|
ClientSecret: "bar",
|
||||||
|
AccessToken: "zoo",
|
||||||
|
})
|
||||||
|
attachment, err := client.UploadMedia(context.Background(), "testdata/logo.png")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("should not be fail: %v", err)
|
||||||
|
}
|
||||||
|
if attachment.ID != 123 {
|
||||||
|
t.Fatalf("want %q but %q", 123, attachment.ID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user