Debian Codegraph @

tq/api.go

raw symbols SCIP
1 package tq
2
3 import (
4 "time"
5
6 "github.com/git-lfs/git-lfs/v3/errors"
7 "github.com/git-lfs/git-lfs/v3/git"
8 "github.com/git-lfs/git-lfs/v3/lfsapi"
9 "github.com/git-lfs/git-lfs/v3/lfshttp"
10 "github.com/git-lfs/git-lfs/v3/tr"
11 "github.com/rubyist/tracerx"
12 )
13
14 type tqClient struct {
15 maxRetries int
16 *lfsapi.Client
17 }
18
19 type batchRef struct {
20 Name string `json:"name,omitempty"`
21 }
22
23 type batchRequest struct {
24 Operation string `json:"operation"`
25 Objects []*Transfer `json:"objects"`
26 TransferAdapterNames []string `json:"transfers,omitempty"`
27 Ref *batchRef `json:"ref"`
28 HashAlgorithm string `json:"hash_algo"`
29 }
30
31 type BatchResponse struct {
32 Objects []*Transfer `json:"objects"`
33 TransferAdapterName string `json:"transfer"`
34 HashAlgorithm string `json:"hash_algo"`
35 endpoint lfshttp.Endpoint
36 }
37
38 func Batch(m Manifest, dir Direction, remote string, remoteRef *git.Ref, objects []*Transfer) (*BatchResponse, error) {
39 if len(objects) == 0 {
40 return &BatchResponse{}, nil
41 }
42
43 cm := m.Upgrade()
44
45 return cm.batchClient().Batch(remote, &batchRequest{
46 Operation: dir.String(),
47 Objects: objects,
48 TransferAdapterNames: m.GetAdapterNames(dir),
49 Ref: &batchRef{Name: remoteRef.Refspec()},
50 HashAlgorithm: "sha256",
51 })
52 }
53
54 type BatchClient interface {
55 Batch(remote string, bReq *batchRequest) (*BatchResponse, error)
56 MaxRetries() int
57 SetMaxRetries(n int)
58 }
59
60 func (c *tqClient) MaxRetries() int {
61 return c.maxRetries
62 }
63
64 func (c *tqClient) SetMaxRetries(n int) {
65 c.maxRetries = n
66 }
67
68 func (c *tqClient) Batch(remote string, bReq *batchRequest) (*BatchResponse, error) {
69 bRes := &BatchResponse{}
70 if len(bReq.Objects) == 0 {
71 return bRes, nil
72 }
73
74 if len(bReq.TransferAdapterNames) == 1 && bReq.TransferAdapterNames[0] == "basic" {
75 bReq.TransferAdapterNames = nil
76 }
77
78 bRes.endpoint = c.Endpoints.Endpoint(bReq.Operation, remote)
79 requestedAt := time.Now()
80
81 req, err := c.NewRequest("POST", bRes.endpoint, "objects/batch", bReq)
82 if err != nil {
83 return nil, errors.Wrap(err, tr.Tr.Get("batch request"))
84 }
85
86 tracerx.Printf("api: batch %d files", len(bReq.Objects))
87
88 req = c.Client.LogRequest(req, "lfs.batch")
89 res, err := c.DoAPIRequestWithAuth(remote, lfshttp.WithRetries(req, c.MaxRetries()))
90 if err != nil {
91 tracerx.Printf("api error: %s", err)
92 return nil, errors.Wrap(err, tr.Tr.Get("batch response"))
93 }
94
95 if err := lfshttp.DecodeJSON(res, bRes); err != nil {
96 return bRes, errors.Wrap(err, tr.Tr.Get("batch response"))
97 }
98
99 if bRes.HashAlgorithm != "" && bRes.HashAlgorithm != "sha256" {
100 return bRes, errors.Wrap(errors.New(tr.Tr.Get("unsupported hash algorithm")), tr.Tr.Get("batch response"))
101 }
102
103 if res.StatusCode != 200 {
104 return nil, lfshttp.NewStatusCodeError(res)
105 }
106
107 for _, obj := range bRes.Objects {
108 for _, a := range obj.Actions {
109 a.createdAt = requestedAt
110 }
111 }
112
113 return bRes, nil
114 }