diff --git a/users.go b/users.go index f616ff5..9cb36f3 100644 --- a/users.go +++ b/users.go @@ -68,6 +68,16 @@ func (s *UserService) IterStopwatches(ctx context.Context) iter.Seq2[types.StopW return ListIter[types.StopWatch](ctx, s.client, "/api/v1/user/stopwatches", nil) } +// ListMySubscriptions returns all repositories watched by the authenticated user. +func (s *UserService) ListMySubscriptions(ctx context.Context) ([]types.Repository, error) { + return ListAll[types.Repository](ctx, s.client, "/api/v1/user/subscriptions", nil) +} + +// IterMySubscriptions returns an iterator over all repositories watched by the authenticated user. +func (s *UserService) IterMySubscriptions(ctx context.Context) iter.Seq2[types.Repository, error] { + return ListIter[types.Repository](ctx, s.client, "/api/v1/user/subscriptions", nil) +} + // ListFollowers returns all followers of a user. func (s *UserService) ListFollowers(ctx context.Context, username string) ([]types.User, error) { path := ResolvePath("/api/v1/users/{username}/followers", pathParams("username", username)) @@ -80,6 +90,18 @@ func (s *UserService) IterFollowers(ctx context.Context, username string) iter.S return ListIter[types.User](ctx, s.client, path, nil) } +// ListSubscriptions returns all repositories watched by a user. +func (s *UserService) ListSubscriptions(ctx context.Context, username string) ([]types.Repository, error) { + path := ResolvePath("/api/v1/users/{username}/subscriptions", pathParams("username", username)) + return ListAll[types.Repository](ctx, s.client, path, nil) +} + +// IterSubscriptions returns an iterator over all repositories watched by a user. +func (s *UserService) IterSubscriptions(ctx context.Context, username string) iter.Seq2[types.Repository, error] { + path := ResolvePath("/api/v1/users/{username}/subscriptions", pathParams("username", username)) + return ListIter[types.Repository](ctx, s.client, path, nil) +} + // ListFollowing returns all users that a user is following. func (s *UserService) ListFollowing(ctx context.Context, username string) ([]types.User, error) { path := ResolvePath("/api/v1/users/{username}/following", pathParams("username", username)) diff --git a/users_test.go b/users_test.go index 18eed62..29dc4de 100644 --- a/users_test.go +++ b/users_test.go @@ -112,6 +112,65 @@ func TestUserService_ListStopwatches_Good(t *testing.T) { } } +func TestUserService_ListMySubscriptions_Good(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + t.Errorf("expected GET, got %s", r.Method) + } + if r.URL.Path != "/api/v1/user/subscriptions" { + t.Errorf("wrong path: %s", r.URL.Path) + } + w.Header().Set("X-Total-Count", "1") + json.NewEncoder(w).Encode([]types.Repository{ + {Name: "go-forge", FullName: "core/go-forge"}, + }) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + repos, err := f.Users.ListMySubscriptions(context.Background()) + if err != nil { + t.Fatal(err) + } + if len(repos) != 1 { + t.Fatalf("got %d repositories, want 1", len(repos)) + } + if repos[0].FullName != "core/go-forge" { + t.Errorf("got full name=%q, want %q", repos[0].FullName, "core/go-forge") + } +} + +func TestUserService_IterMySubscriptions_Good(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + t.Errorf("expected GET, got %s", r.Method) + } + if r.URL.Path != "/api/v1/user/subscriptions" { + t.Errorf("wrong path: %s", r.URL.Path) + } + w.Header().Set("X-Total-Count", "1") + json.NewEncoder(w).Encode([]types.Repository{ + {Name: "go-forge", FullName: "core/go-forge"}, + }) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + count := 0 + for repo, err := range f.Users.IterMySubscriptions(context.Background()) { + if err != nil { + t.Fatal(err) + } + count++ + if repo.FullName != "core/go-forge" { + t.Errorf("got full name=%q, want %q", repo.FullName, "core/go-forge") + } + } + if count != 1 { + t.Fatalf("got %d repositories, want 1", count) + } +} + func TestUserService_IterStopwatches_Good(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { @@ -232,3 +291,62 @@ func TestUserService_ListFollowers_Good(t *testing.T) { t.Errorf("got username=%q, want %q", followers[0].UserName, "bob") } } + +func TestUserService_ListSubscriptions_Good(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + t.Errorf("expected GET, got %s", r.Method) + } + if r.URL.Path != "/api/v1/users/alice/subscriptions" { + t.Errorf("wrong path: %s", r.URL.Path) + } + w.Header().Set("X-Total-Count", "1") + json.NewEncoder(w).Encode([]types.Repository{ + {Name: "go-forge", FullName: "core/go-forge"}, + }) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + repos, err := f.Users.ListSubscriptions(context.Background(), "alice") + if err != nil { + t.Fatal(err) + } + if len(repos) != 1 { + t.Fatalf("got %d repositories, want 1", len(repos)) + } + if repos[0].Name != "go-forge" { + t.Errorf("got name=%q, want %q", repos[0].Name, "go-forge") + } +} + +func TestUserService_IterSubscriptions_Good(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + t.Errorf("expected GET, got %s", r.Method) + } + if r.URL.Path != "/api/v1/users/alice/subscriptions" { + t.Errorf("wrong path: %s", r.URL.Path) + } + w.Header().Set("X-Total-Count", "1") + json.NewEncoder(w).Encode([]types.Repository{ + {Name: "go-forge", FullName: "core/go-forge"}, + }) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + count := 0 + for repo, err := range f.Users.IterSubscriptions(context.Background(), "alice") { + if err != nil { + t.Fatal(err) + } + count++ + if repo.Name != "go-forge" { + t.Errorf("got name=%q, want %q", repo.Name, "go-forge") + } + } + if count != 1 { + t.Fatalf("got %d repositories, want 1", count) + } +}