This commit is contained in:
Robert Sigler 2023-02-23 14:54:35 -05:00 committed by GitHub
commit 6bbd47eabc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 3 deletions

View file

@ -70,6 +70,18 @@ func (c *Client) GetAccountCurrentUser(ctx context.Context) (*Account, error) {
return &account, nil return &account, nil
} }
// AccountLookup returns the Account of specified acct uri.
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. // Profile is a struct for updating profiles.
type Profile struct { type Profile struct {
// If it is nil it will not be updated. // If it is nil it will not be updated.

View file

@ -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) { func TestGetAccountCurrentUser(t *testing.T) {
canErr := true canErr := true
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

View file

@ -6,7 +6,7 @@ import (
"log" "log"
"time" "time"
"github.com/mattn/go-mastodon" "github.com/rmrfslashbin/go-mastodon"
) )
func ExampleRegisterApp() { func ExampleRegisterApp() {

4
go.mod
View file

@ -1,6 +1,6 @@
module github.com/mattn/go-mastodon module github.com/rmrfslashbin/go-mastodon
go 1.16 go 1.19
require ( require (
github.com/gorilla/websocket v1.5.0 github.com/gorilla/websocket v1.5.0