]> Vexing Labs - dead-tooter.git/commitdiff
Working call to get followers
authorAdam Shamblin <adam@vexingworkshop.com>
Wed, 14 Dec 2022 00:19:27 +0000 (17:19 -0700)
committerAdam Shamblin <adam@vexingworkshop.com>
Wed, 14 Dec 2022 00:19:27 +0000 (17:19 -0700)
main.go
pkg/mastodon/account.go
pkg/mastodon/application.go

diff --git a/main.go b/main.go
index 680f31a13719fac7c889b86ca9aee4d2bcdce75c..d030fd8e9815b13f95822d4da600ba376f60fae9 100644 (file)
--- a/main.go
+++ b/main.go
@@ -35,5 +35,10 @@ func main() {
                panic(err.Error())
        }
 
-       fmt.Printf("%+v\n", token)
+       followers, err := account.GetFollowers(token)
+       if err != nil {
+               panic(err.Error())
+       }
+
+       fmt.Printf("%+v\n", followers)
 }
index b651b1b814e96eecfb116a84fea97a6a820134c5..34c5ffd2c0b6cf3b08f6d0e4d8e81a6f85ca2388 100644 (file)
@@ -62,7 +62,7 @@ func (a *Account) Authorize(app Application) (code string) {
        v.Set("redirect_uri", RedirectUris)
 
        u := url.URL{
-               Host:     "hackers.town",
+               Host:     mastohost,
                Path:     "oauth/authorize",
                RawQuery: v.Encode(),
        }
@@ -91,7 +91,7 @@ func (a *Account) RequestToken(app Application, code string) (token Token, err e
        v.Set("grant_type", "authorization_code")
 
        u := url.URL{
-               Host:     "hackers.town",
+               Host:     mastohost,
                Path:     "oauth/token",
                RawQuery: v.Encode(),
        }
@@ -124,7 +124,7 @@ func (a *Account) VerifyCredentials(token Token) (err error) {
        client := &http.Client{}
 
        u := url.URL{
-               Host: "hackers.town",
+               Host: mastohost,
                Path: "api/v1/accounts/verify_credentials",
        }
        u.Scheme = "https"
@@ -157,3 +157,47 @@ func (a *Account) VerifyCredentials(token Token) (err error) {
 
        return
 }
+
+func (a *Account) GetFollowers(token Token) (followers []Account, err error) {
+       client := &http.Client{}
+
+       id := url.PathEscape(a.ID)
+       path, err := url.JoinPath("api/v1/accounts", id, "followers")
+       if err != nil {
+               return
+       }
+
+       u := url.URL{
+               Host: mastohost,
+               Path: path,
+       }
+       u.Scheme = "https"
+
+       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
+       }
+
+       err = json.Unmarshal(body, &followers)
+       if err != nil {
+               return
+       }
+
+       return
+}
index a57c587239e183fb716331035b20c1e068e6b104..b4250471a789f4dd81de017f94d4a84964321a79 100644 (file)
@@ -10,6 +10,7 @@ import (
 )
 
 const configdir = "foodir/"
+const mastohost = "hackers.town"
 
 // Token struct contains the data returned by the Application login request.
 type Token struct {
@@ -75,7 +76,7 @@ func (a *Application) Login() (token Token, err error) {
        v.Set("grant_type", "client_credentials")
 
        u := url.URL{
-               Host:     "hackers.town",
+               Host:     mastohost,
                Path:     "oauth/token",
                RawQuery: v.Encode(),
        }
@@ -111,7 +112,7 @@ func (a *Application) VerifyCredentials(token Token) (err error) {
        client := &http.Client{}
 
        u := url.URL{
-               Host: "hackers.town",
+               Host: mastohost,
                Path: "api/v1/apps/verify_credentials",
        }
        u.Scheme = "https"