also convert to string in attachments, pagenation

This commit is contained in:
Yasuhiro Matsumoto 2017-10-25 10:22:39 +09:00
parent e03b8ccefb
commit 730317321a
7 changed files with 50 additions and 50 deletions

View File

@ -25,7 +25,7 @@ func cmdFollowers(c *cli.Context) error {
return err return err
} }
followers = append(followers, fs...) followers = append(followers, fs...)
if pg.MaxID == 0 { if pg.MaxID == "" {
break break
} }
time.Sleep(10 * time.Second) time.Sleep(10 * time.Second)

View File

@ -3,6 +3,7 @@ package main
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"github.com/mattn/go-mastodon" "github.com/mattn/go-mastodon"
"github.com/urfave/cli" "github.com/urfave/cli"
@ -26,7 +27,7 @@ func cmdToot(c *cli.Context) error {
client := c.App.Metadata["client"].(*mastodon.Client) client := c.App.Metadata["client"].(*mastodon.Client)
_, err := client.PostStatus(context.Background(), &mastodon.Toot{ _, err := client.PostStatus(context.Background(), &mastodon.Toot{
Status: toot, Status: toot,
InReplyToID: c.Int64("i"), InReplyToID: mastodon.ID(fmt.Sprint(c.Int64("i"))),
}) })
return err return err
} }

View File

@ -56,7 +56,7 @@ func ExamplePagination() {
log.Fatal(err) log.Fatal(err)
} }
followers = append(followers, fs...) followers = append(followers, fs...)
if pg.MaxID == 0 { if pg.MaxID == "" {
break break
} }
time.Sleep(10 * time.Second) time.Sleep(10 * time.Second)

View File

@ -174,8 +174,8 @@ func (c *Client) Authenticate(ctx context.Context, username, password string) er
// Toot is struct to post status. // Toot is struct to post status.
type Toot struct { type Toot struct {
Status string `json:"status"` Status string `json:"status"`
InReplyToID int64 `json:"in_reply_to_id"` InReplyToID ID `json:"in_reply_to_id"`
MediaIDs []int64 `json:"media_ids"` MediaIDs []ID `json:"media_ids"`
Sensitive bool `json:"sensitive"` Sensitive bool `json:"sensitive"`
SpoilerText string `json:"spoiler_text"` SpoilerText string `json:"spoiler_text"`
Visibility string `json:"visibility"` Visibility string `json:"visibility"`
@ -186,7 +186,7 @@ type Mention struct {
URL string `json:"url"` URL string `json:"url"`
Username string `json:"username"` Username string `json:"username"`
Acct string `json:"acct"` Acct string `json:"acct"`
ID int64 `json:"id"` ID ID `json:"id"`
} }
// Tag hold information for tag. // Tag hold information for tag.
@ -197,7 +197,7 @@ type Tag struct {
// Attachment hold information for attachment. // Attachment hold information for attachment.
type Attachment struct { type Attachment struct {
ID int64 `json:"id"` ID ID `json:"id"`
Type string `json:"type"` Type string `json:"type"`
URL string `json:"url"` URL string `json:"url"`
RemoteURL string `json:"remote_url"` RemoteURL string `json:"remote_url"`
@ -214,8 +214,8 @@ type Results struct {
// Pagination is a struct for specifying the get range. // Pagination is a struct for specifying the get range.
type Pagination struct { type Pagination struct {
MaxID int64 MaxID ID
SinceID int64 SinceID ID
Limit int64 Limit int64
} }
@ -245,18 +245,18 @@ func newPagination(rawlink string) (*Pagination, error) {
return p, nil return p, nil
} }
func getPaginationID(rawurl, key string) (int64, error) { func getPaginationID(rawurl, key string) (ID, error) {
u, err := url.Parse(rawurl) u, err := url.Parse(rawurl)
if err != nil { if err != nil {
return 0, err return "", err
} }
id, err := strconv.ParseInt(u.Query().Get(key), 10, 64) id, err := strconv.ParseInt(u.Query().Get(key), 10, 64)
if err != nil { if err != nil {
return 0, err return "", err
} }
return id, nil return ID(fmt.Sprint(id)), nil
} }
func (p *Pagination) toValues() url.Values { func (p *Pagination) toValues() url.Values {
@ -264,10 +264,10 @@ func (p *Pagination) toValues() url.Values {
} }
func (p *Pagination) setValues(params url.Values) url.Values { func (p *Pagination) setValues(params url.Values) url.Values {
if p.MaxID > 0 { if p.MaxID != "" {
params.Set("max_id", fmt.Sprint(p.MaxID)) params.Set("max_id", string(p.MaxID))
} else if p.SinceID > 0 { } else if p.SinceID != "" {
params.Set("since_id", fmt.Sprint(p.SinceID)) params.Set("since_id", string(p.SinceID))
} }
if p.Limit > 0 { if p.Limit > 0 {
params.Set("limit", fmt.Sprint(p.Limit)) params.Set("limit", fmt.Sprint(p.Limit))

View File

@ -25,26 +25,26 @@ func TestDoAPI(t *testing.T) {
c := NewClient(&Config{Server: ts.URL}) c := NewClient(&Config{Server: ts.URL})
var accounts []Account var accounts []Account
err := c.doAPI(context.Background(), http.MethodGet, "/", nil, &accounts, &Pagination{ err := c.doAPI(context.Background(), http.MethodGet, "/", nil, &accounts, &Pagination{
MaxID: 999, MaxID: "999",
}) })
if err == nil { if err == nil {
t.Fatalf("should be fail: %v", err) t.Fatalf("should be fail: %v", err)
} }
pg := &Pagination{ pg := &Pagination{
MaxID: 123, MaxID: "123",
SinceID: 789, SinceID: "789",
Limit: 10, Limit: 10,
} }
err = c.doAPI(context.Background(), http.MethodGet, "/", url.Values{}, &accounts, pg) err = c.doAPI(context.Background(), http.MethodGet, "/", url.Values{}, &accounts, pg)
if err != nil { if err != nil {
t.Fatalf("should not be fail: %v", err) t.Fatalf("should not be fail: %v", err)
} }
if pg.MaxID != 234 { if pg.MaxID != "234" {
t.Fatalf("want %d but %d", 234, pg.MaxID) t.Fatalf("want %q but %q", "234", pg.MaxID)
} }
if pg.SinceID != 890 { if pg.SinceID != "890" {
t.Fatalf("want %d but %d", 890, pg.SinceID) t.Fatalf("want %q but %q", "890", pg.SinceID)
} }
if accounts[0].Username != "foo" { if accounts[0].Username != "foo" {
t.Fatalf("want %q but %q", "foo", accounts[0].Username) t.Fatalf("want %q but %q", "foo", accounts[0].Username)
@ -54,19 +54,19 @@ func TestDoAPI(t *testing.T) {
} }
pg = &Pagination{ pg = &Pagination{
MaxID: 123, MaxID: "123",
SinceID: 789, SinceID: "789",
Limit: 10, Limit: 10,
} }
err = c.doAPI(context.Background(), http.MethodGet, "/", nil, &accounts, pg) err = c.doAPI(context.Background(), http.MethodGet, "/", nil, &accounts, pg)
if err != nil { if err != nil {
t.Fatalf("should not be fail: %v", err) t.Fatalf("should not be fail: %v", err)
} }
if pg.MaxID != 234 { if pg.MaxID != "234" {
t.Fatalf("want %d but %d", 234, pg.MaxID) t.Fatalf("want %q but %q", "234", pg.MaxID)
} }
if pg.SinceID != 890 { if pg.SinceID != "890" {
t.Fatalf("want %d but %d", 890, pg.SinceID) t.Fatalf("want %q but %q", "890", pg.SinceID)
} }
if accounts[0].Username != "foo" { if accounts[0].Username != "foo" {
t.Fatalf("want %q but %q", "foo", accounts[0].Username) t.Fatalf("want %q but %q", "foo", accounts[0].Username)
@ -297,11 +297,11 @@ func TestNewPagination(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("should not be fail: %v", err) t.Fatalf("should not be fail: %v", err)
} }
if pg.MaxID != 123 { if pg.MaxID != "123" {
t.Fatalf("want %d but %d", 123, pg.MaxID) t.Fatalf("want %q but %q", "123", pg.MaxID)
} }
if pg.SinceID != 789 { if pg.SinceID != "789" {
t.Fatalf("want %d but %d", 789, pg.SinceID) t.Fatalf("want %q but %q", "789", pg.SinceID)
} }
} }
@ -320,15 +320,15 @@ func TestGetPaginationID(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("should not be fail: %v", err) t.Fatalf("should not be fail: %v", err)
} }
if id != 123 { if id != "123" {
t.Fatalf("want %d but %d", 123, id) t.Fatalf("want %q but %q", "123", id)
} }
} }
func TestPaginationSetValues(t *testing.T) { func TestPaginationSetValues(t *testing.T) {
p := &Pagination{ p := &Pagination{
MaxID: 123, MaxID: "123",
SinceID: 789, SinceID: "789",
Limit: 10, Limit: 10,
} }
before := url.Values{"key": {"value"}} before := url.Values{"key": {"value"}}
@ -347,8 +347,8 @@ func TestPaginationSetValues(t *testing.T) {
} }
p = &Pagination{ p = &Pagination{
MaxID: 0, MaxID: "",
SinceID: 789, SinceID: "789",
} }
before = url.Values{} before = url.Values{}
after = p.setValues(before) after = p.setValues(before)

View File

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"net/url" "net/url"
"strconv"
"time" "time"
) )
@ -208,12 +207,12 @@ func (c *Client) GetTimelineMedia(ctx context.Context, isLocal bool, pg *Paginat
func (c *Client) PostStatus(ctx context.Context, toot *Toot) (*Status, error) { func (c *Client) PostStatus(ctx context.Context, toot *Toot) (*Status, error) {
params := url.Values{} params := url.Values{}
params.Set("status", toot.Status) params.Set("status", toot.Status)
if toot.InReplyToID > 0 { if toot.InReplyToID != "" {
params.Set("in_reply_to_id", fmt.Sprint(toot.InReplyToID)) params.Set("in_reply_to_id", string(toot.InReplyToID))
} }
if toot.MediaIDs != nil { if toot.MediaIDs != nil {
for _, media := range toot.MediaIDs { for _, media := range toot.MediaIDs {
params.Add("media_ids[]", strconv.FormatInt(media, 10)) params.Add("media_ids[]", string(media))
} }
} }
if toot.Visibility != "" { if toot.Visibility != "" {

View File

@ -534,7 +534,7 @@ func TestUploadMedia(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("should not be fail: %v", err) t.Fatalf("should not be fail: %v", err)
} }
if attachment.ID != 123 { if attachment.ID != "123" {
t.Fatalf("want %q but %q", 123, attachment.ID) t.Fatalf("want %q but %q", "123", attachment.ID)
} }
} }