From af6c89cc88371f55f2969f38279864aee0f84afc Mon Sep 17 00:00:00 2001 From: Adam Shamblin Date: Tue, 3 Jan 2023 16:15:16 -0700 Subject: [PATCH] #13, add support for proxied requests --- cmd/account.go | 20 ++++++++--------- cmd/list.go | 10 ++++----- cmd/login.go | 10 ++++----- cmd/root.go | 12 +++++------ pkg/mastodon/account.go | 48 ++++++++++++++++++----------------------- pkg/mastodon/api/api.go | 16 ++++++++------ pkg/mastodon/list.go | 18 ++++++++-------- 7 files changed, 64 insertions(+), 70 deletions(-) diff --git a/cmd/account.go b/cmd/account.go index 049c99d..4c80f36 100644 --- a/cmd/account.go +++ b/cmd/account.go @@ -36,7 +36,7 @@ var getAccountCmd = &cobra.Command{ return information on the current logged in user.`, PreRun: func(cmd *cobra.Command, args []string) { - err := account.VerifyCredentials(host, token) + err := account.VerifyCredentials(&mAPI) if err != nil { fmt.Println("Failed to verify credentials.") os.Exit(1) @@ -48,7 +48,7 @@ return information on the current logged in user.`, accountID = account.ID } - acct, err := mastodon.GetAccount(accountID, host, token) + acct, err := mastodon.GetAccount(accountID, &mAPI) if err != nil { fmt.Println("Failed to get account.") os.Exit(1) @@ -64,7 +64,7 @@ var getFollowersCmd = &cobra.Command{ Long: "Get a list of followers for the current account.", PreRun: func(cmd *cobra.Command, args []string) { - err := account.VerifyCredentials(host, token) + err := account.VerifyCredentials(&mAPI) if err != nil { fmt.Println("Failed to verify credentials.") os.Exit(1) @@ -72,7 +72,7 @@ var getFollowersCmd = &cobra.Command{ }, Run: func(cmd *cobra.Command, args []string) { - followers, err := account.GetFollowers(host, token) + followers, err := account.GetFollowers(&mAPI) if err != nil { panic(err.Error()) } @@ -87,7 +87,7 @@ var getFollowingCmd = &cobra.Command{ Long: "Get a list of followed accounts for the current account.", PreRun: func(cmd *cobra.Command, args []string) { - err := account.VerifyCredentials(host, token) + err := account.VerifyCredentials(&mAPI) if err != nil { fmt.Println("Failed to verify credentials.") os.Exit(1) @@ -95,7 +95,7 @@ var getFollowingCmd = &cobra.Command{ }, Run: func(cmd *cobra.Command, args []string) { - following, err := account.GetFollowing(host, token) + following, err := account.GetFollowing(&mAPI) if err != nil { fmt.Println("Failed to get following list.") os.Exit(1) @@ -111,7 +111,7 @@ var getListsCmd = &cobra.Command{ Long: "Fetch all lists the account owns", PreRun: func(cmd *cobra.Command, args []string) { - err := account.VerifyCredentials(host, token) + err := account.VerifyCredentials(&mAPI) if err != nil { fmt.Println("Failed to verify credentials.") os.Exit(1) @@ -119,7 +119,7 @@ var getListsCmd = &cobra.Command{ }, Run: func(cmd *cobra.Command, args []string) { - lists, err := account.GetLists(host, token) + lists, err := account.GetLists(&mAPI) if err != nil { fmt.Println("Failed to get following list.") os.Exit(1) @@ -135,7 +135,7 @@ var getStatusesCmd = &cobra.Command{ Long: "Fetch statuses posted by account.", PreRun: func(cmd *cobra.Command, args []string) { - err := account.VerifyCredentials(host, token) + err := account.VerifyCredentials(&mAPI) if err != nil { fmt.Println("Failed to verify credentials.") os.Exit(1) @@ -143,7 +143,7 @@ var getStatusesCmd = &cobra.Command{ }, Run: func(cmd *cobra.Command, args []string) { - statuses, err := account.GetStatuses(host, token) + statuses, err := account.GetStatuses(&mAPI) if err != nil { fmt.Println("Failed to retrieve statuses.") os.Exit(1) diff --git a/cmd/list.go b/cmd/list.go index 8ac42e4..5750ab4 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -34,7 +34,7 @@ var listAccountsCmd = &cobra.Command{ }, PreRun: func(cmd *cobra.Command, args []string) { - err := account.VerifyCredentials(host, token) + err := account.VerifyCredentials(&mAPI) if err != nil { fmt.Printf("Unable to verify credentials: %s\n", err.Error()) return @@ -42,12 +42,12 @@ var listAccountsCmd = &cobra.Command{ }, Run: func(cmd *cobra.Command, args []string) { - list, err := mastodon.GetList(args[0], host, token) + list, err := mastodon.GetList(args[0], &mAPI) if err != nil { panic(err.Error()) } - accounts, err := list.GetAccounts(host, token) + accounts, err := list.GetAccounts(&mAPI) if err != nil { panic(err.Error()) } @@ -62,7 +62,7 @@ var listsContainingCmd = &cobra.Command{ Long: "Get lists containing a specified account", PreRun: func(cmd *cobra.Command, args []string) { - err := account.VerifyCredentials(host, token) + err := account.VerifyCredentials(&mAPI) if err != nil { panic(err.Error()) } @@ -77,7 +77,7 @@ var listsContainingCmd = &cobra.Command{ id = args[0] } - lists, err := mastodon.ListsContainingAccount(id, host, token) + lists, err := mastodon.ListsContainingAccount(id, &mAPI) if err != nil { panic(err.Error()) } diff --git a/cmd/login.go b/cmd/login.go index 2333c06..f3f37b2 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -30,7 +30,7 @@ func login() { Website: "https://dead-tooter.vexingworkshop.com", } - app, err = mastodon.Create(host, client) + app, err = mastodon.Create(mAPI.Host, client) if err != nil { fmt.Printf("Failed to create client: %s\n", err.Error()) } @@ -42,22 +42,22 @@ func login() { } var account mastodon.Account - code, err := account.Authorize(host, app) + code, err := account.Authorize(mAPI.Host, app) if err != nil { fmt.Printf("Failed to authorize account: %s\n", err.Error()) } - token, err = account.RequestToken(host, app, code) + mAPI.Token, err = account.RequestToken(mAPI.Host, app, code) if err != nil { fmt.Printf("Failed to acquire request token: %s\n", err.Error()) } - err = token.Save() + err = mAPI.Token.Save() if err != nil { fmt.Printf("Failed to store access token: %s\n", err.Error()) } - err = account.VerifyCredentials(host, token) + err = account.VerifyCredentials(&mAPI) if err != nil { fmt.Printf("Failed to verify credentials: %s\n", err.Error()) } diff --git a/cmd/root.go b/cmd/root.go index c960ee4..ba28e02 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -8,16 +8,14 @@ import ( "github.com/spf13/cobra" ) -var host string -var token api.Token -var proxyStr string +var mAPI = api.API{} func init() { - rootCmd.PersistentFlags().StringVarP(&host, + rootCmd.PersistentFlags().StringVarP(&mAPI.Host, "host", "H", "", "Mastodon host where your account lives.") rootCmd.MarkPersistentFlagRequired("host") - rootCmd.PersistentFlags().StringVarP(&proxyStr, + rootCmd.PersistentFlags().StringVarP(&mAPI.ProxyURL, "proxy", "", "", "Address of proxy server.") } @@ -29,14 +27,14 @@ be present in the Mastodon web UI.`, PersistentPreRun: func(cmd *cobra.Command, args []string) { var err error - token, err = api.LoadToken() + mAPI.Token, err = api.LoadToken() if err != nil { fmt.Println("No authentication token found.") } }, PersistentPostRun: func(cmd *cobra.Command, args []string) { - err := token.Save() + err := mAPI.Token.Save() if err != nil { panic(err.Error()) } diff --git a/pkg/mastodon/account.go b/pkg/mastodon/account.go index 5b56211..aaaf1d1 100644 --- a/pkg/mastodon/account.go +++ b/pkg/mastodon/account.go @@ -175,16 +175,16 @@ func (a *Account) RequestToken( // VerifyCredentials hydrates the account object by validating the bearer token // against the Mastodon API -func (a *Account) VerifyCredentials( - host string, token api.Token, proxy string) (err error) { +func (a *Account) VerifyCredentials(mAPI *api.API) (err error) { u := url.URL{ - Host: host, + Host: mAPI.Host, Path: "api/v1/accounts/verify_credentials", } u.Scheme = "https" - body, err := api.Get(u, host, token, proxy) + body, err := api.Get(u, mAPI) if err != nil { + fmt.Printf("%+v", err) return err } @@ -197,8 +197,7 @@ func (a *Account) VerifyCredentials( } // GetAccount returns the details of account specified by ID. -func GetAccount( - ID string, host string, token api.Token, proxy string) (account Account, err error) { +func GetAccount(ID string, mAPI *api.API) (account Account, err error) { id := url.PathEscape(ID) path, err := url.JoinPath("api/v1/accounts", id) @@ -207,12 +206,12 @@ func GetAccount( } u := url.URL{ - Host: host, + Host: mAPI.Host, Path: path, } u.Scheme = "https" - body, err := api.Get(u, host, token, proxy) + body, err := api.Get(u, mAPI) if err != nil { return account, err } @@ -226,14 +225,12 @@ func GetAccount( } // Get returns the currently logged in account. -func (a *Account) Get( - host string, token api.Token, proxy string) (account Account, err error) { - return GetAccount(a.ID, host, token, proxy) +func (a *Account) Get(mAPI *api.API) (account Account, err error) { + return GetAccount(a.ID, mAPI) } // GetFollowers returns a list of all accounts following the logged in user -func (a *Account) GetFollowers( - host string, token api.Token, proxy string) (followers AccountCollection, err error) { +func (a *Account) GetFollowers(mAPI *api.API) (followers AccountCollection, err error) { id := url.PathEscape(a.ID) path, err := url.JoinPath("api/v1/accounts", id, "followers") @@ -242,12 +239,12 @@ func (a *Account) GetFollowers( } u := url.URL{ - Host: host, + Host: mAPI.Host, Path: path, } u.Scheme = "https" - body, err := api.Get(u, host, token, proxy) + body, err := api.Get(u, mAPI) if err != nil { return followers, err } @@ -261,8 +258,7 @@ func (a *Account) GetFollowers( } // GetFollowing returns a list of all accounts followed by the logged in user -func (a *Account) GetFollowing( - host string, token api.Token, proxy string) (following AccountCollection, err error) { +func (a *Account) GetFollowing(mAPI *api.API) (following AccountCollection, err error) { id := url.PathEscape(a.ID) path, err := url.JoinPath("api/v1/accounts", id, "following") @@ -271,12 +267,12 @@ func (a *Account) GetFollowing( } u := url.URL{ - Host: host, + Host: mAPI.Host, Path: path, } u.Scheme = "https" - body, err := api.Get(u, host, token, proxy) + body, err := api.Get(u, mAPI) if err != nil { return } @@ -290,16 +286,15 @@ func (a *Account) GetFollowing( } // GetLists fetches all lists the account owns -func (a *Account) GetLists( - host string, token api.Token, proxy string) (lists ListCollection, err error) { +func (a *Account) GetLists(mAPI *api.API) (lists ListCollection, err error) { u := url.URL{ - Host: host, + Host: mAPI.Host, Path: "api/v1/lists", } u.Scheme = "https" - body, err := api.Get(u, host, token, proxy) + body, err := api.Get(u, mAPI) if err != nil { return } @@ -313,8 +308,7 @@ func (a *Account) GetLists( } // GetStatuses fetches a list of statuses by the account. -func (a *Account) GetStatuses( - host string, token api.Token, proxy string) (statuses StatusCollection, err error) { +func (a *Account) GetStatuses(mAPI *api.API) (statuses StatusCollection, err error) { id := url.PathEscape(a.ID) path, err := url.JoinPath("api/v1/accounts", id, "statuses") @@ -323,12 +317,12 @@ func (a *Account) GetStatuses( } u := url.URL{ - Host: host, + Host: mAPI.Host, Path: path, } u.Scheme = "https" - body, err := api.Get(u, host, token, proxy) + body, err := api.Get(u, mAPI) if err != nil { return } diff --git a/pkg/mastodon/api/api.go b/pkg/mastodon/api/api.go index 9d63692..9652b97 100644 --- a/pkg/mastodon/api/api.go +++ b/pkg/mastodon/api/api.go @@ -9,15 +9,17 @@ import ( // API provides connection details necessary for all API requests. type API struct { - host string - token Token - proxyURL url.URL + Host string + Token Token + ProxyURL string } func getClient(proxyStr string) (client *http.Client, err error) { proxyURL, err := url.Parse(proxyStr) + if proxyURL.String() == "" { + return &http.Client{}, nil + } if err != nil { - client = &http.Client{} return } @@ -31,8 +33,8 @@ func getClient(proxyStr string) (client *http.Client, err error) { } // Get provides a request -func Get(u url.URL, host string, token Token, proxyStr string) (body []byte, err error) { - client, err := getClient(proxyStr) +func Get(u url.URL, mAPI *API) (body []byte, err error) { + client, err := getClient(mAPI.ProxyURL) if err != nil { return } @@ -41,7 +43,7 @@ func Get(u url.URL, host string, token Token, proxyStr string) (body []byte, err if err != nil { return } - req.Header.Add("Authorization", "Bearer "+token.AccessToken) + req.Header.Add("Authorization", "Bearer "+mAPI.Token.AccessToken) resp, err := client.Do(req) if err != nil { diff --git a/pkg/mastodon/list.go b/pkg/mastodon/list.go index 3a4a919..a954e7a 100644 --- a/pkg/mastodon/list.go +++ b/pkg/mastodon/list.go @@ -36,7 +36,7 @@ func (ll ListCollection) Display() { } // GetList returns a list specified by id. -func GetList(ID string, host string, token api.Token) (list List, err error) { +func GetList(ID string, mAPI *api.API) (list List, err error) { id := url.PathEscape(ID) path, err := url.JoinPath("api/v1/lists", id) if err != nil { @@ -44,12 +44,12 @@ func GetList(ID string, host string, token api.Token) (list List, err error) { } u := url.URL{ - Host: host, + Host: mAPI.Host, Path: path, } u.Scheme = "https" - body, err := api.Get(u, host, token) + body, err := api.Get(u, mAPI) if err != nil { return } @@ -64,7 +64,7 @@ func GetList(ID string, host string, token api.Token) (list List, err error) { // ListsContainingAccount returns lists containing this account. func ListsContainingAccount( - accountID string, host string, token api.Token) (lists ListCollection, err error) { + accountID string, mAPI *api.API) (lists ListCollection, err error) { id := url.PathEscape(accountID) path, err := url.JoinPath("api/v1/accounts", id, "lists") @@ -73,12 +73,12 @@ func ListsContainingAccount( } u := url.URL{ - Host: host, + Host: mAPI.Host, Path: path, } u.Scheme = "https" - body, err := api.Get(u, host, token) + body, err := api.Get(u, mAPI) if err != nil { return } @@ -92,7 +92,7 @@ func ListsContainingAccount( } // GetAccounts returns the accounts associated with a list. -func (l *List) GetAccounts(host string, token api.Token) (accounts AccountCollection, err error) { +func (l *List) GetAccounts(mAPI *api.API) (accounts AccountCollection, err error) { id := url.PathEscape(l.ID) path, err := url.JoinPath("api/v1/lists", id, "accounts") if err != nil { @@ -100,12 +100,12 @@ func (l *List) GetAccounts(host string, token api.Token) (accounts AccountCollec } u := url.URL{ - Host: host, + Host: mAPI.Host, Path: path, } u.Scheme = "https" - body, err := api.Get(u, host, token) + body, err := api.Get(u, mAPI) if err != nil { return } -- 2.39.5