From 0a4b8a849a2c4e6964f32d599899888ef99e0a94 Mon Sep 17 00:00:00 2001 From: Virgil Date: Thu, 2 Apr 2026 02:32:03 +0000 Subject: [PATCH] feat(users): add quota package endpoints Co-Authored-By: Virgil --- docs/api-contract.md | 6 +++++ users.go | 10 ++++++++ users_test.go | 60 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) diff --git a/docs/api-contract.md b/docs/api-contract.md index 5f5c64f..1689e11 100644 --- a/docs/api-contract.md +++ b/docs/api-contract.md @@ -229,6 +229,12 @@ Coverage notes: rows list direct tests when a symbol is named in test names or r | method | UserService.CheckFollowing | `func (s *UserService) CheckFollowing(ctx context.Context, username, target string) (bool, error)` | CheckFollowing reports whether one user is following another user. | `TestUserService_CheckFollowing_Good`, `TestUserService_CheckFollowing_Bad_NotFound` | | method | UserService.GetCurrent | `func (s *UserService) GetCurrent(ctx context.Context) (*types.User, error)` | GetCurrent returns the authenticated user. | `TestUserService_Good_GetCurrent` | | method | UserService.GetQuota | `func (s *UserService) GetQuota(ctx context.Context) (*types.QuotaInfo, error)` | GetQuota returns the authenticated user's quota information. | `TestUserService_GetQuota_Good` | +| method | UserService.ListQuotaArtifacts | `func (s *UserService) ListQuotaArtifacts(ctx context.Context) ([]types.QuotaUsedArtifact, error)` | ListQuotaArtifacts returns all artifacts affecting the authenticated user's quota. | `TestUserService_ListQuotaArtifacts_Good` | +| method | UserService.IterQuotaArtifacts | `func (s *UserService) IterQuotaArtifacts(ctx context.Context) iter.Seq2[types.QuotaUsedArtifact, error]` | IterQuotaArtifacts returns an iterator over all artifacts affecting the authenticated user's quota. | `TestUserService_IterQuotaArtifacts_Good` | +| method | UserService.ListQuotaAttachments | `func (s *UserService) ListQuotaAttachments(ctx context.Context) ([]types.QuotaUsedAttachment, error)` | ListQuotaAttachments returns all attachments affecting the authenticated user's quota. | `TestUserService_ListQuotaAttachments_Good` | +| method | UserService.IterQuotaAttachments | `func (s *UserService) IterQuotaAttachments(ctx context.Context) iter.Seq2[types.QuotaUsedAttachment, error]` | IterQuotaAttachments returns an iterator over all attachments affecting the authenticated user's quota. | `TestUserService_IterQuotaAttachments_Good` | +| method | UserService.ListQuotaPackages | `func (s *UserService) ListQuotaPackages(ctx context.Context) ([]types.QuotaUsedPackage, error)` | ListQuotaPackages returns all packages affecting the authenticated user's quota. | `TestUserService_ListQuotaPackages_Good` | +| method | UserService.IterQuotaPackages | `func (s *UserService) IterQuotaPackages(ctx context.Context) iter.Seq2[types.QuotaUsedPackage, error]` | IterQuotaPackages returns an iterator over all packages affecting the authenticated user's quota. | `TestUserService_IterQuotaPackages_Good` | | method | UserService.ListStopwatches | `func (s *UserService) ListStopwatches(ctx context.Context) ([]types.StopWatch, error)` | ListStopwatches returns all existing stopwatches for the authenticated user. | `TestUserService_ListStopwatches_Good` | | method | UserService.IterStopwatches | `func (s *UserService) IterStopwatches(ctx context.Context) iter.Seq2[types.StopWatch, error]` | IterStopwatches returns an iterator over all existing stopwatches for the authenticated user. | `TestUserService_IterStopwatches_Good` | | method | UserService.IterFollowers | `func (s *UserService) IterFollowers(ctx context.Context, username string) iter.Seq2[types.User, error]` | IterFollowers returns an iterator over all followers of a user. | No direct tests. | diff --git a/users.go b/users.go index 51072c6..031e90a 100644 --- a/users.go +++ b/users.go @@ -82,6 +82,16 @@ func (s *UserService) IterQuotaAttachments(ctx context.Context) iter.Seq2[types. return ListIter[types.QuotaUsedAttachment](ctx, s.client, "/api/v1/user/quota/attachments", nil) } +// ListQuotaPackages returns all packages affecting the authenticated user's quota. +func (s *UserService) ListQuotaPackages(ctx context.Context) ([]types.QuotaUsedPackage, error) { + return ListAll[types.QuotaUsedPackage](ctx, s.client, "/api/v1/user/quota/packages", nil) +} + +// IterQuotaPackages returns an iterator over all packages affecting the authenticated user's quota. +func (s *UserService) IterQuotaPackages(ctx context.Context) iter.Seq2[types.QuotaUsedPackage, error] { + return ListIter[types.QuotaUsedPackage](ctx, s.client, "/api/v1/user/quota/packages", nil) +} + // ListEmails returns all email addresses for the authenticated user. func (s *UserService) ListEmails(ctx context.Context) ([]types.Email, error) { return ListAll[types.Email](ctx, s.client, "/api/v1/user/emails", nil) diff --git a/users_test.go b/users_test.go index 71413a7..1668a1c 100644 --- a/users_test.go +++ b/users_test.go @@ -283,6 +283,66 @@ func TestUserService_IterQuotaAttachments_Good(t *testing.T) { } } +func TestUserService_ListQuotaPackages_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/quota/packages" { + t.Errorf("wrong path: %s", r.URL.Path) + } + w.Header().Set("X-Total-Count", "2") + json.NewEncoder(w).Encode([]types.QuotaUsedPackage{ + {Name: "pkg-one", Type: "container", Version: "1.0.0", Size: 123, HTMLURL: "https://example.com/packages/1"}, + {Name: "pkg-two", Type: "npm", Version: "2.0.0", Size: 456, HTMLURL: "https://example.com/packages/2"}, + }) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + packages, err := f.Users.ListQuotaPackages(context.Background()) + if err != nil { + t.Fatal(err) + } + if len(packages) != 2 { + t.Fatalf("got %d packages, want 2", len(packages)) + } + if packages[0].Name != "pkg-one" || packages[0].Type != "container" || packages[0].Size != 123 { + t.Errorf("unexpected first package: %+v", packages[0]) + } +} + +func TestUserService_IterQuotaPackages_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/quota/packages" { + t.Errorf("wrong path: %s", r.URL.Path) + } + w.Header().Set("X-Total-Count", "1") + json.NewEncoder(w).Encode([]types.QuotaUsedPackage{ + {Name: "pkg-one", Type: "container", Version: "1.0.0", Size: 123, HTMLURL: "https://example.com/packages/1"}, + }) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + var got []types.QuotaUsedPackage + for pkg, err := range f.Users.IterQuotaPackages(context.Background()) { + if err != nil { + t.Fatal(err) + } + got = append(got, pkg) + } + if len(got) != 1 { + t.Fatalf("got %d packages, want 1", len(got)) + } + if got[0].Name != "pkg-one" || got[0].Type != "container" { + t.Errorf("unexpected package: %+v", got[0]) + } +} + func TestUserService_ListEmails_Good(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet {