Merge pull request #10864 from ywk253100/200227_response

Set the "Link" and "location" header
This commit is contained in:
Wenkai Yin(尹文开) 2020-02-28 18:25:02 +08:00 committed by GitHub
commit bd0a8e9f8f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 110 additions and 10 deletions

View File

@ -560,6 +560,9 @@ responses:
X-Request-Id:
description: The ID of the corresponding request for the response
type: string
Location:
description: The location of the resource
type: string
'202':
description: Accepted
headers:

View File

@ -110,8 +110,10 @@ func (a *artifactAPI) ListArtifacts(ctx context.Context, params operation.ListAr
assembler.NewVulAssembler(boolValue(params.WithScanOverview)).WithArtifacts(artifacts...).Assemble(ctx)
// TODO add link header
return operation.NewListArtifactsOK().WithXTotalCount(total).WithLink("").WithPayload(artifacts)
return operation.NewListArtifactsOK().
WithXTotalCount(total).
WithLink(a.Links(ctx, params.HTTPRequest.URL, total, query.PageNumber, query.PageSize).String()).
WithPayload(artifacts)
}
func (a *artifactAPI) GetArtifact(ctx context.Context, params operation.GetArtifactParams) middleware.Responder {
@ -166,18 +168,17 @@ func (a *artifactAPI) CopyArtifact(ctx context.Context, params operation.CopyArt
}
dstRepo := fmt.Sprintf("%s/%s", params.ProjectName, params.RepositoryName)
_, id, err := a.repoCtl.Ensure(ctx, dstRepo)
_, _, err = a.repoCtl.Ensure(ctx, dstRepo)
if err != nil {
return a.SendError(ctx, err)
}
id, err = a.artCtl.Copy(ctx, srcRepo, ref, dstRepo)
_, err = a.artCtl.Copy(ctx, srcRepo, ref, dstRepo)
if err != nil {
return a.SendError(ctx, err)
}
// TODO set location header
_ = id
return operation.NewCopyArtifactCreated()
location := strings.TrimSuffix(params.HTTPRequest.URL.Path, "/") + "/" + ref
return operation.NewCopyArtifactCreated().WithLocation(location)
}
func (a *artifactAPI) ScanArtifact(ctx context.Context, params operation.ScanArtifactParams) middleware.Responder {
@ -243,7 +244,7 @@ func (a *artifactAPI) CreateTag(ctx context.Context, params operation.CreateTagP
if _, err = a.tagCtl.Create(ctx, tag); err != nil {
return a.SendError(ctx, err)
}
// TODO set location header?
// TODO as we provide no API for get the single tag, ignore setting the location header here
return operation.NewCreateTagCreated()
}

View File

@ -19,7 +19,10 @@ package handler
import (
"context"
"errors"
"github.com/goharbor/harbor/src/internal"
ierror "github.com/goharbor/harbor/src/internal/error"
"net/url"
"strconv"
"github.com/go-openapi/runtime/middleware"
"github.com/goharbor/harbor/src/common/rbac"
@ -95,3 +98,32 @@ func (b *BaseAPI) RequireProjectAccess(ctx context.Context, projectIDOrName inte
}
return ierror.ForbiddenError(nil)
}
// Links return Links based on the provided pagination information
func (b *BaseAPI) Links(ctx context.Context, u *url.URL, total, pageNumber, pageSize int64) internal.Links {
url := *u
var links internal.Links
// prev
if pageNumber > 1 && (pageNumber-1)*pageSize < total {
q := url.Query()
q.Set("page", strconv.FormatInt(pageNumber-1, 10))
url.RawQuery = q.Encode()
link := &internal.Link{
URL: url.String(),
Rel: "prev",
}
links = append(links, link)
}
// next
if pageSize*pageNumber < total {
q := url.Query()
q.Set("page", strconv.FormatInt(pageNumber+1, 10))
url.RawQuery = q.Encode()
link := &internal.Link{
URL: url.String(),
Rel: "next",
}
links = append(links, link)
}
return links
}

View File

@ -0,0 +1,62 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package handler
import (
"github.com/stretchr/testify/suite"
"net/url"
"testing"
)
type baseHandlerTestSuite struct {
suite.Suite
base *BaseAPI
}
func (b *baseHandlerTestSuite) SetupSuite() {
b.base = &BaseAPI{}
}
func (b *baseHandlerTestSuite) TestLinks() {
// request first page, response contains only "next" link
url, err := url.Parse("http://localhost/api/artifacts?page=1&page_size=1")
b.Require().Nil(err)
links := b.base.Links(nil, url, 3, 1, 1)
b.Require().Len(links, 1)
b.Equal("next", links[0].Rel)
b.Equal("http://localhost/api/artifacts?page=2&page_size=1", links[0].URL)
// request last page, response contains only "prev" link
url, err = url.Parse("http://localhost/api/artifacts?page=3&page_size=1")
b.Require().Nil(err)
links = b.base.Links(nil, url, 3, 3, 1)
b.Require().Len(links, 1)
b.Equal("prev", links[0].Rel)
b.Equal("http://localhost/api/artifacts?page=2&page_size=1", links[0].URL)
// request the second page, response contains both "prev" and "next" links
url, err = url.Parse("http://localhost/api/artifacts?page=2&page_size=1")
b.Require().Nil(err)
links = b.base.Links(nil, url, 3, 2, 1)
b.Require().Len(links, 2)
b.Equal("prev", links[0].Rel)
b.Equal("http://localhost/api/artifacts?page=1&page_size=1", links[0].URL)
b.Equal("next", links[1].Rel)
b.Equal("http://localhost/api/artifacts?page=3&page_size=1", links[1].URL)
}
func TestBaseHandler(t *testing.T) {
suite.Run(t, &baseHandlerTestSuite{})
}

View File

@ -81,8 +81,10 @@ func (r *repositoryAPI) ListRepositories(ctx context.Context, params operation.L
for _, repository := range repositories {
repos = append(repos, r.assembleRepository(ctx, repository))
}
// TODO add link header
return operation.NewListRepositoriesOK().WithXTotalCount(total).WithLink("").WithPayload(repos)
return operation.NewListRepositoriesOK().
WithXTotalCount(total).
WithLink(r.Links(ctx, params.HTTPRequest.URL, total, query.PageNumber, query.PageSize).String()).
WithPayload(repos)
}
func (r *repositoryAPI) GetRepository(ctx context.Context, params operation.GetRepositoryParams) middleware.Responder {