diff options
| author | Ethan Lee <ethanalee@google.com> | 2026-03-11 21:04:11 +0000 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2026-03-23 09:41:31 -0700 |
| commit | c43b677a694ff5791b1b696298c9e29ae6d221bb (patch) | |
| tree | f980f6f50b3251785b6b1925516147a4e9d52e32 /internal/api | |
| parent | 21d5c77a01c8874095d68e8c10a03c100fcd2070 (diff) | |
| download | go-x-pkgsite-c43b677a694ff5791b1b696298c9e29ae6d221bb.tar.xz | |
internal/api: implement module versions endpoint
- Implement module versions endpoint using ds.GetVersionsForPath
- Introduce paginate helper to generalize paginaton logic
- Update fakedatasource to correctly use V1Path to return all versions
Change-Id: Icc028bf8ca9c13978bb6eba84afe9736ccd6bcee
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/754862
Reviewed-by: Jonathan Amsterdam <jba@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Ethan Lee <ethanalee@google.com>
kokoro-CI: kokoro <noreply+kokoro@google.com>
Diffstat (limited to 'internal/api')
| -rw-r--r-- | internal/api/api.go | 66 | ||||
| -rw-r--r-- | internal/api/api_test.go | 74 |
2 files changed, 139 insertions, 1 deletions
diff --git a/internal/api/api.go b/internal/api/api.go index 2b0f3f4f..3b653fd8 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -9,6 +9,7 @@ import ( "encoding/json" "errors" "net/http" + "strconv" "strings" "golang.org/x/pkgsite/internal" @@ -210,7 +211,35 @@ func ServeModule(w http.ResponseWriter, r *http.Request, ds internal.DataSource) } } - // Future: handle licenses param. + return serveJSON(w, http.StatusOK, resp) +} + +// ServeModuleVersions handles requests for the v1 module versions endpoint. +func ServeModuleVersions(w http.ResponseWriter, r *http.Request, ds internal.DataSource) (err error) { + defer derrors.Wrap(&err, "ServeModuleVersions") + + path := strings.TrimPrefix(r.URL.Path, "/v1/versions/") + if path == "" { + return serveErrorJSON(w, http.StatusBadRequest, "missing path", nil) + } + + var params VersionsParams + if err := ParseParams(r.URL.Query(), ¶ms); err != nil { + return serveErrorJSON(w, http.StatusBadRequest, err.Error(), nil) + } + + infos, err := ds.GetVersionsForPath(r.Context(), path) + if err != nil { + if errors.Is(err, derrors.NotFound) { + return serveErrorJSON(w, http.StatusNotFound, err.Error(), nil) + } + return err + } + + resp, err := paginate(infos, params.ListParams, 100) + if err != nil { + return serveErrorJSON(w, http.StatusBadRequest, err.Error(), nil) + } return serveJSON(w, http.StatusOK, resp) } @@ -238,3 +267,38 @@ func serveErrorJSON(w http.ResponseWriter, status int, message string, candidate Candidates: candidates, }) } + +func paginate[T any](all []T, lp ListParams, defaultLimit int) (PaginatedResponse[T], error) { + limit := lp.Limit + if limit <= 0 { + limit = defaultLimit + } + + offset := 0 + if lp.Token != "" { + var err error + offset, err = strconv.Atoi(lp.Token) + if err != nil || offset < 0 { + return PaginatedResponse[T]{}, errors.New("invalid token") + } + } + + if offset > len(all) { + offset = len(all) + } + end := offset + limit + if end > len(all) { + end = len(all) + } + + var nextToken string + if end < len(all) { + nextToken = strconv.Itoa(end) + } + + return PaginatedResponse[T]{ + Items: all[offset:end], + Total: len(all), + NextPageToken: nextToken, + }, nil +} diff --git a/internal/api/api_test.go b/internal/api/api_test.go index f61c8581..9112c576 100644 --- a/internal/api/api_test.go +++ b/internal/api/api_test.go @@ -247,3 +247,77 @@ func TestServeModule(t *testing.T) { }) } } + +func TestServeModuleVersions(t *testing.T) { + ctx := context.Background() + ds := fakedatasource.New() + + ds.MustInsertModule(ctx, &internal.Module{ + ModuleInfo: internal.ModuleInfo{ModulePath: "example.com", Version: "v1.0.0"}, + Units: []*internal.Unit{{UnitMeta: internal.UnitMeta{Path: "example.com"}}}, + }) + ds.MustInsertModule(ctx, &internal.Module{ + ModuleInfo: internal.ModuleInfo{ModulePath: "example.com", Version: "v1.1.0"}, + Units: []*internal.Unit{{UnitMeta: internal.UnitMeta{Path: "example.com"}}}, + }) + ds.MustInsertModule(ctx, &internal.Module{ + ModuleInfo: internal.ModuleInfo{ModulePath: "example.com/v2", Version: "v2.0.0"}, + Units: []*internal.Unit{{UnitMeta: internal.UnitMeta{Path: "example.com/v2"}}}, + }) + + for _, test := range []struct { + name string + url string + wantStatus int + wantCount int + }{ + { + name: "all versions (cross-major)", + url: "/v1/versions/example.com", + wantStatus: http.StatusOK, + wantCount: 3, + }, + { + name: "with limit", + url: "/v1/versions/example.com?limit=1", + wantStatus: http.StatusOK, + wantCount: 1, + }, + { + name: "pagination", + url: "/v1/versions/example.com?limit=1&token=1", + wantStatus: http.StatusOK, + wantCount: 1, + }, + } { + t.Run(test.name, func(t *testing.T) { + r := httptest.NewRequest("GET", test.url, nil) + w := httptest.NewRecorder() + + err := ServeModuleVersions(w, r, ds) + if err != nil { + t.Fatalf("ServeModuleVersions returned error: %v", err) + } + + if w.Code != test.wantStatus { + t.Errorf("status = %d, want %d", w.Code, test.wantStatus) + } + + if test.wantStatus == http.StatusOK { + var got PaginatedResponse[internal.ModuleInfo] + if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil { + t.Fatalf("json.Unmarshal: %v", err) + } + if len(got.Items) != test.wantCount { + t.Errorf("count = %d, want %d", len(got.Items), test.wantCount) + } + if test.name == "with limit" && got.NextPageToken != "1" { + t.Errorf("nextPageToken = %q, want %q", got.NextPageToken, "1") + } + if test.name == "pagination" && got.NextPageToken != "2" { + t.Errorf("nextPageToken = %q, want %q", got.NextPageToken, "2") + } + } + }) + } +} |
