From bb90d3dd5260909d013aa735b8423ca3f1506db5 Mon Sep 17 00:00:00 2001 From: Adam Shamblin Date: Tue, 3 Jan 2023 08:19:13 -0700 Subject: [PATCH] #13 - WIP --- cmd/root.go | 3 +++ pkg/mastodon/account.go | 32 ++++++++++++++++++-------------- pkg/mastodon/api/api.go | 30 ++++++++++++++++++++++++++++-- 3 files changed, 49 insertions(+), 16 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 8884e3b..c960ee4 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -10,12 +10,15 @@ import ( var host string var token api.Token +var proxyStr string func init() { rootCmd.PersistentFlags().StringVarP(&host, "host", "H", "", "Mastodon host where your account lives.") rootCmd.MarkPersistentFlagRequired("host") + rootCmd.PersistentFlags().StringVarP(&proxyStr, + "proxy", "", "", "Address of proxy server.") } var rootCmd = &cobra.Command{ diff --git a/pkg/mastodon/account.go b/pkg/mastodon/account.go index 2b21a0c..5b56211 100644 --- a/pkg/mastodon/account.go +++ b/pkg/mastodon/account.go @@ -175,14 +175,15 @@ 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) (err error) { +func (a *Account) VerifyCredentials( + host string, token api.Token, proxy string) (err error) { u := url.URL{ Host: host, Path: "api/v1/accounts/verify_credentials", } u.Scheme = "https" - body, err := api.Get(u, host, token) + body, err := api.Get(u, host, token, proxy) if err != nil { return err } @@ -197,7 +198,7 @@ func (a *Account) VerifyCredentials(host string, token api.Token) (err error) { // GetAccount returns the details of account specified by ID. func GetAccount( - ID string, host string, token api.Token) (account Account, err error) { + ID string, host string, token api.Token, proxy string) (account Account, err error) { id := url.PathEscape(ID) path, err := url.JoinPath("api/v1/accounts", id) @@ -211,7 +212,7 @@ func GetAccount( } u.Scheme = "https" - body, err := api.Get(u, host, token) + body, err := api.Get(u, host, token, proxy) if err != nil { return account, err } @@ -225,13 +226,14 @@ func GetAccount( } // Get returns the currently logged in account. -func (a *Account) Get(host string, token api.Token) (account Account, err error) { - return GetAccount(a.ID, host, token) +func (a *Account) Get( + host string, token api.Token, proxy string) (account Account, err error) { + return GetAccount(a.ID, host, token, proxy) } // GetFollowers returns a list of all accounts following the logged in user func (a *Account) GetFollowers( - host string, token api.Token) (followers AccountCollection, err error) { + host string, token api.Token, proxy string) (followers AccountCollection, err error) { id := url.PathEscape(a.ID) path, err := url.JoinPath("api/v1/accounts", id, "followers") @@ -245,7 +247,7 @@ func (a *Account) GetFollowers( } u.Scheme = "https" - body, err := api.Get(u, host, token) + body, err := api.Get(u, host, token, proxy) if err != nil { return followers, err } @@ -260,7 +262,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) (following AccountCollection, err error) { + host string, token api.Token, proxy string) (following AccountCollection, err error) { id := url.PathEscape(a.ID) path, err := url.JoinPath("api/v1/accounts", id, "following") @@ -274,7 +276,7 @@ func (a *Account) GetFollowing( } u.Scheme = "https" - body, err := api.Get(u, host, token) + body, err := api.Get(u, host, token, proxy) if err != nil { return } @@ -288,14 +290,16 @@ func (a *Account) GetFollowing( } // GetLists fetches all lists the account owns -func (a *Account) GetLists(host string, token api.Token) (lists ListCollection, err error) { +func (a *Account) GetLists( + host string, token api.Token, proxy string) (lists ListCollection, err error) { + u := url.URL{ Host: host, Path: "api/v1/lists", } u.Scheme = "https" - body, err := api.Get(u, host, token) + body, err := api.Get(u, host, token, proxy) if err != nil { return } @@ -310,7 +314,7 @@ func (a *Account) GetLists(host string, token api.Token) (lists ListCollection, // GetStatuses fetches a list of statuses by the account. func (a *Account) GetStatuses( - host string, token api.Token) (statuses StatusCollection, err error) { + host string, token api.Token, proxy string) (statuses StatusCollection, err error) { id := url.PathEscape(a.ID) path, err := url.JoinPath("api/v1/accounts", id, "statuses") @@ -324,7 +328,7 @@ func (a *Account) GetStatuses( } u.Scheme = "https" - body, err := api.Get(u, host, token) + body, err := api.Get(u, host, token, proxy) if err != nil { return } diff --git a/pkg/mastodon/api/api.go b/pkg/mastodon/api/api.go index 8dcd928..9d63692 100644 --- a/pkg/mastodon/api/api.go +++ b/pkg/mastodon/api/api.go @@ -7,9 +7,35 @@ import ( "net/url" ) +// API provides connection details necessary for all API requests. +type API struct { + host string + token Token + proxyURL url.URL +} + +func getClient(proxyStr string) (client *http.Client, err error) { + proxyURL, err := url.Parse(proxyStr) + if err != nil { + client = &http.Client{} + return + } + + transport := &http.Transport{ + Proxy: http.ProxyURL(proxyURL)} + + client = &http.Client{ + Transport: transport} + + return client, err +} + // Get provides a request -func Get(u url.URL, host string, token Token) (body []byte, err error) { - client := &http.Client{} +func Get(u url.URL, host string, token Token, proxyStr string) (body []byte, err error) { + client, err := getClient(proxyStr) + if err != nil { + return + } req, err := http.NewRequest("GET", u.String(), nil) if err != nil { -- 2.39.5