cli: improve "mark_read" command

This commit is contained in:
Peter Stuifzand 2018-05-16 17:54:34 +02:00
parent 2fa8b521ba
commit b62f719221

View File

@ -6,6 +6,7 @@ import (
"log"
"net/http"
"net/url"
"strings"
"github.com/pstuifzand/ekster/microsub"
)
@ -58,6 +59,27 @@ func (c *Client) microsubPostRequest(action string, args map[string]string) (*ht
return client.Do(req)
}
func (c *Client) microsubPostFormRequest(action string, args map[string]string, data url.Values) (*http.Response, error) {
client := http.Client{}
u := *c.MicrosubEndpoint
q := u.Query()
q.Add("action", action)
for k, v := range args {
q.Add(k, v)
}
u.RawQuery = q.Encode()
req, err := http.NewRequest(http.MethodPost, u.String(), strings.NewReader(data.Encode()))
if err != nil {
return nil, err
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.Token))
return client.Do(req)
}
func (c *Client) ChannelsGetList() []microsub.Channel {
args := make(map[string]string)
res, err := c.microsubGetRequest("channels", args)
@ -220,22 +242,15 @@ func (c *Client) Search(query string) []microsub.Feed {
}
func (c *Client) MarkRead(channel string, uids []string) {
// TODO(peter): Add Authorization header
client := http.Client{}
u := *c.MicrosubEndpoint
q := u.Query()
q.Add("action", "mark_read")
q.Add("channel", channel)
args := make(map[string]string)
args["channel"] = channel
data := url.Values{}
for _, uid := range uids {
data.Add("entry[]", uid)
}
u.RawQuery = q.Encode()
res, err := client.PostForm(u.String(), data)
res, err := c.microsubPostFormRequest("mark_read", args, data)
if err == nil {
defer res.Body.Close()
}