]> Vexing Labs - dead-tooter.git/commitdiff
#13 - WIP
authorAdam Shamblin <adam@vexingworkshop.com>
Tue, 3 Jan 2023 15:19:13 +0000 (08:19 -0700)
committerAdam Shamblin <adam@vexingworkshop.com>
Tue, 3 Jan 2023 15:19:13 +0000 (08:19 -0700)
cmd/root.go
pkg/mastodon/account.go
pkg/mastodon/api/api.go

index 8884e3b4157ad438e0283dd0aa6377efacb7e93a..c960ee456571b224a7e4beaf5421f42de43d1983 100644 (file)
@@ -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{
index 2b21a0cec3c3fa531a8fefe941be08acd9a9715d..5b562117b9b9b1e5c7530568e84b27f0cbbf02aa 100644 (file)
@@ -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
        }
index 8dcd9285135fbdf2aa59f95c459c802887cb329e..9d6369224b88b282926b1809f90c045d695d18aa 100644 (file)
@@ -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 {