generated from goexl/template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pb.go
48 lines (43 loc) · 1.03 KB
/
pb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package apisix
import (
"context"
"encoding/base64"
"fmt"
"os"
"strings"
"github.com/goexl/exc"
"github.com/goexl/gox/field"
)
func (c *Client) UploadDescriptor(
ctx context.Context,
filename string, id string, description string,
) (rsp *Response, err error) {
req := c.http.R()
if content, re := os.ReadFile(filename); nil != re {
err = re
} else {
pr := new(descriptorReq)
pr.Content = base64.StdEncoding.EncodeToString(content)
pr.Description = description
req.SetBody(pr)
}
if nil != err {
return
}
rsp = new(Response)
url := fmt.Sprintf("%s/apisix/admin/protos/%s", strings.TrimSuffix(c.endpoint, "/"), id)
req.SetHeader("X-API-KEY", c.apiKey)
if hr, he := req.SetContext(ctx).SetResult(rsp).Put(url); nil != he {
err = he
} else if hr.IsError() {
err = exc.NewFields(
"Apisix返回错误",
field.New("url", hr.Request.URL),
field.New("code", hr.StatusCode()),
field.New("body", hr.Body()),
)
} else {
c.logger.Debug("上传Protobuf文件到Apisix成功", field.New("rsp", rsp))
}
return
}