"fmt"
"os"
- "git.vexingworkshop.com/signal9/dead-tooter/pkg/mastodon"
+ "git.vexingworkshop.com/signal9/dead-tooter/pkg/mastodon/api"
"github.com/spf13/cobra"
)
var host string
-var token mastodon.Token
+var token api.Token
func init() {
rootCmd.PersistentFlags().StringVarP(&host,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
var err error
- token, err = mastodon.LoadToken()
+ token, err = api.LoadToken()
if err != nil {
fmt.Println("No authentication token found.")
}
"net/http"
"net/url"
"os/exec"
+
+ "git.vexingworkshop.com/signal9/dead-tooter/pkg/mastodon/api"
)
// Account represents a user of Mastodon and their associated profile.
// containing a bearer token necessary to make future, authenticated requests
// on the part of the user.
func (a *Account) RequestToken(
- host string, app Application, code string) (token Token, err error) {
+ host string, app Application, code string) (token api.Token, err error) {
v := url.Values{}
v.Set("client_id", app.ClientID)
// VerifyCredentials hydrates the account object by validating the bearer token
// against the Mastodon API
-func (a *Account) VerifyCredentials(host string, token Token) (err error) {
+func (a *Account) VerifyCredentials(host string, token api.Token) (err error) {
u := url.URL{
Host: host,
Path: "api/v1/accounts/verify_credentials",
}
u.Scheme = "https"
- body, err := Get(u, host, token)
+ body, err := api.Get(u, host, token)
if err != nil {
return err
}
// GetAccount returns the details of account specified by ID.
func GetAccount(
- ID string, host string, token Token) (account Account, err error) {
+ ID string, host string, token api.Token) (account Account, err error) {
id := url.PathEscape(ID)
path, err := url.JoinPath("api/v1/accounts", id)
}
u.Scheme = "https"
- body, err := Get(u, host, token)
+ body, err := api.Get(u, host, token)
if err != nil {
return account, err
}
}
// Get returns the currently logged in account.
-func (a *Account) Get(host string, token Token) (account Account, err error) {
+func (a *Account) Get(host string, token api.Token) (account Account, err error) {
return GetAccount(a.ID, host, token)
}
// GetFollowers returns a list of all accounts following the logged in user
func (a *Account) GetFollowers(
- host string, token Token) (followers []Account, err error) {
+ host string, token api.Token) (followers []Account, err error) {
id := url.PathEscape(a.ID)
path, err := url.JoinPath("api/v1/accounts", id, "followers")
}
u.Scheme = "https"
- body, err := Get(u, host, token)
+ body, err := api.Get(u, host, token)
if err != nil {
return followers, err
}
// GetFollowing returns a list of all accounts followed by the logged in user
func (a *Account) GetFollowing(
- host string, token Token) (following []Account, err error) {
+ host string, token api.Token) (following []Account, err error) {
id := url.PathEscape(a.ID)
path, err := url.JoinPath("api/v1/accounts", id, "following")
}
u.Scheme = "https"
- body, err := Get(u, host, token)
+ body, err := api.Get(u, host, token)
if err != nil {
return following, err
}
+++ /dev/null
-package mastodon
-
-import (
- "errors"
- "io"
- "net/http"
- "net/url"
-)
-
-// Get provides a request
-func Get(u url.URL, host string, token Token) (body []byte, err error) {
- client := &http.Client{}
-
- req, err := http.NewRequest("GET", u.String(), nil)
- if err != nil {
- return
- }
- req.Header.Add("Authorization", "Bearer "+token.AccessToken)
-
- resp, err := client.Do(req)
- if err != nil {
- return
- }
- if resp.StatusCode != 200 {
- err = errors.New(resp.Status)
- return
- }
- defer resp.Body.Close()
-
- body, err = io.ReadAll(resp.Body)
- if err != nil {
- return
- }
-
- return body, err
-}
--- /dev/null
+package api
+
+import (
+ "errors"
+ "io"
+ "net/http"
+ "net/url"
+)
+
+// Get provides a request
+func Get(u url.URL, host string, token Token) (body []byte, err error) {
+ client := &http.Client{}
+
+ req, err := http.NewRequest("GET", u.String(), nil)
+ if err != nil {
+ return
+ }
+ req.Header.Add("Authorization", "Bearer "+token.AccessToken)
+
+ resp, err := client.Do(req)
+ if err != nil {
+ return
+ }
+ if resp.StatusCode != 200 {
+ err = errors.New(resp.Status)
+ return
+ }
+ defer resp.Body.Close()
+
+ body, err = io.ReadAll(resp.Body)
+ if err != nil {
+ return
+ }
+
+ return body, err
+}
--- /dev/null
+package api
+
+import (
+ "encoding/json"
+ "os"
+ "path/filepath"
+)
+
+// Token struct contains the data returned by the Application login request.
+type Token struct {
+ AccessToken string `json:"access_token"`
+ TokenType string `json:"token_type"`
+ Scope string `json:"scope"`
+ CreatedAt int `json:"created_at"`
+}
+
+// LoadToken deserializes the application authentication token from disc, if it
+// exists.
+func LoadToken() (token Token, err error) {
+ configdir, err := os.UserConfigDir()
+ if err != nil {
+ return
+ }
+ tokenfile := filepath.Join(configdir, "dead-tooter", "token")
+
+ data, err := os.ReadFile(tokenfile)
+ if err != nil && os.IsNotExist(err) {
+ return
+ }
+
+ err = json.Unmarshal(data, &token)
+ if err != nil {
+ return
+ }
+
+ return
+}
+
+// Save serializes the application token to disk.
+func (t *Token) Save() (err error) {
+ data, err := json.Marshal(t)
+ if err != nil {
+ return
+ }
+
+ configdir, err := os.UserConfigDir()
+ if err != nil {
+ return
+ }
+ tokenfile := filepath.Join(configdir, "dead-tooter", "token")
+
+ err = os.WriteFile(tokenfile, data, 0666)
+ if err != nil {
+ return
+ }
+
+ return
+}
"net/url"
"os"
"path/filepath"
+
+ "git.vexingworkshop.com/signal9/dead-tooter/pkg/mastodon/api"
)
// RedirectUris when passed to the redirect_uris parameter, will
// Login authenticates the application to the Mastodon API, returning
// a bearer token to be used with future requests.
-func (a *Application) Login(host string) (token Token, err error) {
+func (a *Application) Login(host string) (token api.Token, err error) {
v := url.Values{}
v.Set("client_id", a.ClientID)
v.Set("client_secret", a.ClientSecret)
// VerifyCredentials accepts a Token object and validates the contained
// token against the Mastodon API.
-func (a *Application) VerifyCredentials(host string, token Token) (err error) {
+func (a *Application) VerifyCredentials(host string, token api.Token) (err error) {
client := &http.Client{}
u := url.URL{
import (
"encoding/json"
"net/url"
+
+ "git.vexingworkshop.com/signal9/dead-tooter/pkg/mastodon/api"
)
// List represents a list of some users that the authenticated user follows.
}
// GetList returns a list specified by id.
-func GetList(ID string, host string, token Token) (list List, err error) {
+func GetList(ID string, host string, token api.Token) (list List, err error) {
id := url.PathEscape(ID)
path, err := url.JoinPath("api/v1/lists", id)
if err != nil {
}
u.Scheme = "https"
- body, err := Get(u, host, token)
+ body, err := api.Get(u, host, token)
if err != nil {
return
}
}
// GetAccounts returns the accounts associated with a list.
-func (l *List) GetAccounts(host string, token Token) (accounts []Account, err error) {
+func (l *List) GetAccounts(host string, token api.Token) (accounts []Account, err error) {
id := url.PathEscape(l.ID)
path, err := url.JoinPath("api/v1/lists", id, "accounts")
if err != nil {
}
u.Scheme = "https"
- body, err := Get(u, host, token)
+ body, err := api.Get(u, host, token)
if err != nil {
return
}
+++ /dev/null
-package mastodon
-
-import (
- "encoding/json"
- "os"
- "path/filepath"
-)
-
-// Token struct contains the data returned by the Application login request.
-type Token struct {
- AccessToken string `json:"access_token"`
- TokenType string `json:"token_type"`
- Scope string `json:"scope"`
- CreatedAt int `json:"created_at"`
-}
-
-// LoadToken deserializes the application authentication token from disc, if it
-// exists.
-func LoadToken() (token Token, err error) {
- configdir, err := os.UserConfigDir()
- if err != nil {
- return
- }
- tokenfile := filepath.Join(configdir, "dead-tooter", "token")
-
- data, err := os.ReadFile(tokenfile)
- if err != nil && os.IsNotExist(err) {
- return
- }
-
- err = json.Unmarshal(data, &token)
- if err != nil {
- return
- }
-
- return
-}
-
-// Save serializes the application token to disk.
-func (t *Token) Save() (err error) {
- data, err := json.Marshal(t)
- if err != nil {
- return
- }
-
- configdir, err := os.UserConfigDir()
- if err != nil {
- return
- }
- tokenfile := filepath.Join(configdir, "dead-tooter", "token")
-
- err = os.WriteFile(tokenfile, data, 0666)
- if err != nil {
- return
- }
-
- return
-}