]> Vexing Labs - dead-tooter.git/commitdiff
#13, add support for proxied requests
authorAdam Shamblin <adam@vexingworkshop.com>
Tue, 3 Jan 2023 23:15:16 +0000 (16:15 -0700)
committerAdam Shamblin <adam@vexingworkshop.com>
Tue, 3 Jan 2023 23:15:16 +0000 (16:15 -0700)
cmd/account.go
cmd/list.go
cmd/login.go
cmd/root.go
pkg/mastodon/account.go
pkg/mastodon/api/api.go
pkg/mastodon/list.go

index 049c99d3128c192800c42feb6ffff7ced70a39e4..4c80f364de08cd16dfb52c881e1cd18609003a2f 100644 (file)
@@ -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)
index 8ac42e4f22246bc1c04959cfc20ca1020ffc1e9b..5750ab44894451d9f246edbf5607f43594f7179f 100644 (file)
@@ -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())
                }
index 2333c066f77d3dc31cd12b7eed621e0a792742ee..f3f37b2cecdeebf2708fe256553c9c562221c818 100644 (file)
@@ -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())
        }
index c960ee456571b224a7e4beaf5421f42de43d1983..ba28e026ca8a96a93f46dfce65336cb78ef83c51 100644 (file)
@@ -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())
                }
index 5b562117b9b9b1e5c7530568e84b27f0cbbf02aa..aaaf1d1924dc1e41a0662b85a50c375a7a3db85d 100644 (file)
@@ -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
        }
index 9d6369224b88b282926b1809f90c045d695d18aa..9652b977dc10d0b91c363da3913eadda1c7ae9cf 100644 (file)
@@ -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 {
index 3a4a919ef321c28e1e523b944efc3567ab7769a3..a954e7ac037d880059971667d2ea3e6fa4a7074a 100644 (file)
@@ -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
        }