git-lfs/tq/api.go
Preben Ingvaldsen 880bbf81af lfsapi: add Access param to DoWithAuth()
Add a new Access parameter to DoWithAuth() to allow callers to
specify the URL Access Mode. This removes the assumption that
DoWithAuth() will always be used for an LFS API request. This
commit also adds DoAPIRequestWithAuth(), which provides the
functionality of DoWithAuth() while explicitly using the LFS API
endpoint for the access mode, allowing parity with the previous
DoWithAuth() functionality.
2018-09-28 14:16:55 -07:00

91 lines
2.2 KiB
Go

package tq
import (
"time"
"github.com/git-lfs/git-lfs/errors"
"github.com/git-lfs/git-lfs/git"
"github.com/git-lfs/git-lfs/lfsapi"
"github.com/git-lfs/git-lfs/lfshttp"
"github.com/rubyist/tracerx"
)
type tqClient struct {
MaxRetries int
*lfsapi.Client
}
type batchRef struct {
Name string `json:"name,omitempty"`
}
type batchRequest struct {
Operation string `json:"operation"`
Objects []*Transfer `json:"objects"`
TransferAdapterNames []string `json:"transfers,omitempty"`
Ref *batchRef `json:"ref"`
}
type BatchResponse struct {
Objects []*Transfer `json:"objects"`
TransferAdapterName string `json:"transfer"`
endpoint lfshttp.Endpoint
}
func Batch(m *Manifest, dir Direction, remote string, remoteRef *git.Ref, objects []*Transfer) (*BatchResponse, error) {
if len(objects) == 0 {
return &BatchResponse{}, nil
}
return m.batchClient().Batch(remote, &batchRequest{
Operation: dir.String(),
Objects: objects,
TransferAdapterNames: m.GetAdapterNames(dir),
Ref: &batchRef{Name: remoteRef.Refspec()},
})
}
func (c *tqClient) Batch(remote string, bReq *batchRequest) (*BatchResponse, error) {
bRes := &BatchResponse{}
if len(bReq.Objects) == 0 {
return bRes, nil
}
if len(bReq.TransferAdapterNames) == 1 && bReq.TransferAdapterNames[0] == "basic" {
bReq.TransferAdapterNames = nil
}
bRes.endpoint = c.Endpoints.Endpoint(bReq.Operation, remote)
requestedAt := time.Now()
req, err := c.NewRequest("POST", bRes.endpoint, "objects/batch", bReq)
if err != nil {
return nil, errors.Wrap(err, "batch request")
}
tracerx.Printf("api: batch %d files", len(bReq.Objects))
req = c.Client.LogRequest(req, "lfs.batch")
res, err := c.DoAPIRequestWithAuth(remote, lfshttp.WithRetries(req, c.MaxRetries))
if err != nil {
tracerx.Printf("api error: %s", err)
return nil, errors.Wrap(err, "batch response")
}
if err := lfshttp.DecodeJSON(res, bRes); err != nil {
return bRes, errors.Wrap(err, "batch response")
}
if res.StatusCode != 200 {
return nil, lfshttp.NewStatusCodeError(res)
}
for _, obj := range bRes.Objects {
for _, a := range obj.Actions {
a.createdAt = requestedAt
}
}
return bRes, nil
}