From 39aa7c8e83c8595170a181bd7a2e4f4e69eb0dc1 Mon Sep 17 00:00:00 2001 From: Robert Sigler Date: Thu, 23 Feb 2023 14:32:45 -0500 Subject: [PATCH] Adding accounts lookup API endpoint --- accounts.go | 11 +++++++++++ accounts_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/accounts.go b/accounts.go index 0b43e4c..1e4d0cb 100644 --- a/accounts.go +++ b/accounts.go @@ -70,6 +70,17 @@ func (c *Client) GetAccountCurrentUser(ctx context.Context) (*Account, error) { return &account, nil } +func (c *Client) AccountLookup(ctx context.Context, acct string) (*Account, error) { + var account Account + params := url.Values{} + params.Set("acct", acct) + err := c.doAPI(ctx, http.MethodGet, "/api/v1/accounts/lookup", params, &account, nil) + if err != nil { + return nil, err + } + return &account, nil +} + // Profile is a struct for updating profiles. type Profile struct { // If it is nil it will not be updated. diff --git a/accounts_test.go b/accounts_test.go index 47e0310..850fd5b 100644 --- a/accounts_test.go +++ b/accounts_test.go @@ -38,6 +38,40 @@ func TestGetAccount(t *testing.T) { } } +func TestAccountLookup(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/api/v1/accounts/lookup" { + http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) + return + } + acct := r.URL.Query().Get("acct") + if acct != "foo@bar" { + http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) + return + } + fmt.Fprintln(w, `{"username": "foo@bar"}`) + })) + defer ts.Close() + + client := NewClient(&Config{ + Server: ts.URL, + ClientID: "foo", + ClientSecret: "bar", + AccessToken: "zoo", + }) + _, err := client.AccountLookup(context.Background(), "123") + if err == nil { + t.Fatalf("should be fail: %v", err) + } + a, err := client.AccountLookup(context.Background(), "foo@bar") + if err != nil { + t.Fatalf("should not be fail: %v", err) + } + if a.Username != "foo@bar" { + t.Fatalf("want %q but %q", "foo@bar", a.Username) + } +} + func TestGetAccountCurrentUser(t *testing.T) { canErr := true ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {