github.com/x-oss-byte/git-lfs@v2.5.2+incompatible/tq/api.go (about) 1 package tq 2 3 import ( 4 "time" 5 6 "github.com/git-lfs/git-lfs/errors" 7 "github.com/git-lfs/git-lfs/git" 8 "github.com/git-lfs/git-lfs/lfsapi" 9 "github.com/rubyist/tracerx" 10 ) 11 12 type tqClient struct { 13 MaxRetries int 14 *lfsapi.Client 15 } 16 17 type batchRef struct { 18 Name string `json:"name,omitempty"` 19 } 20 21 type batchRequest struct { 22 Operation string `json:"operation"` 23 Objects []*Transfer `json:"objects"` 24 TransferAdapterNames []string `json:"transfers,omitempty"` 25 Ref *batchRef `json:"ref"` 26 } 27 28 type BatchResponse struct { 29 Objects []*Transfer `json:"objects"` 30 TransferAdapterName string `json:"transfer"` 31 endpoint lfsapi.Endpoint 32 } 33 34 func Batch(m *Manifest, dir Direction, remote string, remoteRef *git.Ref, objects []*Transfer) (*BatchResponse, error) { 35 if len(objects) == 0 { 36 return &BatchResponse{}, nil 37 } 38 39 return m.batchClient().Batch(remote, &batchRequest{ 40 Operation: dir.String(), 41 Objects: objects, 42 TransferAdapterNames: m.GetAdapterNames(dir), 43 Ref: &batchRef{Name: remoteRef.Refspec()}, 44 }) 45 } 46 47 func (c *tqClient) Batch(remote string, bReq *batchRequest) (*BatchResponse, error) { 48 bRes := &BatchResponse{} 49 if len(bReq.Objects) == 0 { 50 return bRes, nil 51 } 52 53 if len(bReq.TransferAdapterNames) == 1 && bReq.TransferAdapterNames[0] == "basic" { 54 bReq.TransferAdapterNames = nil 55 } 56 57 bRes.endpoint = c.Endpoints.Endpoint(bReq.Operation, remote) 58 requestedAt := time.Now() 59 60 req, err := c.NewRequest("POST", bRes.endpoint, "objects/batch", bReq) 61 if err != nil { 62 return nil, errors.Wrap(err, "batch request") 63 } 64 65 tracerx.Printf("api: batch %d files", len(bReq.Objects)) 66 67 req = c.LogRequest(req, "lfs.batch") 68 res, err := c.DoWithAuth(remote, lfsapi.WithRetries(req, c.MaxRetries)) 69 if err != nil { 70 tracerx.Printf("api error: %s", err) 71 return nil, errors.Wrap(err, "batch response") 72 } 73 74 if err := lfsapi.DecodeJSON(res, bRes); err != nil { 75 return bRes, errors.Wrap(err, "batch response") 76 } 77 78 if res.StatusCode != 200 { 79 return nil, lfsapi.NewStatusCodeError(res) 80 } 81 82 for _, obj := range bRes.Objects { 83 for _, a := range obj.Actions { 84 a.createdAt = requestedAt 85 } 86 } 87 88 return bRes, nil 89 }