-
Notifications
You must be signed in to change notification settings - Fork 0
/
nuwa.go
108 lines (87 loc) · 2.33 KB
/
nuwa.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package nuwa
import (
`encoding/json`
`fmt`
`net/http`
`github.com/class100/core`
`github.com/go-resty/resty/v2`
`github.com/mcuadros/go-defaults`
`github.com/rs/xid`
log `github.com/sirupsen/logrus`
`github.com/storezhang/gox`
`github.com/storezhang/validatorx`
`github.com/class100/sdk-go`
)
const (
// 打包状态
// PackageStatusSuccess 打包成功
PackageStatusSuccess PackageStatus = "success"
// PackageStatusFailed 打包失败
PackageStatusFailed PackageStatus = "failed"
)
type (
// Client 女娲打包系统
Client struct {
class100.Client
// Endpoint 地址
Endpoint string `default:"https://nuwa.class100.com" json:"endpoint"`
}
// PackageStatus 打包结果
PackageStatus string
)
// New 创建一个新的女娲客户端
func New(endpoint string, accessKey string, secretKey string) *Client {
return &Client{
Endpoint: endpoint,
Client: class100.Client{
AccessKey: accessKey,
SecretKey: secretKey,
},
}
}
func (c Client) parseUrl(path string, version class100.ApiVersion) (url string) {
if class100.ApiVersionDefault == version {
url = fmt.Sprintf("%s/api/%s", c.Endpoint, path)
} else {
url = fmt.Sprintf("%s/api/%s/%s", c.Endpoint, version, path)
}
return
}
func (c *Client) Package(pkg *Package, environment core.Environment, version class100.ApiVersion) (rsp Response, err error) {
if core.EnvironmentSimulation == environment {
rsp = Response{
Id: xid.New().String(),
Key: xid.New().String(),
}
return
}
// 设置默认值
defaults.SetDefaults(c)
// 设置默认值
defaults.SetDefaults(pkg)
// 确保参数合法
if err = validatorx.Validate(pkg); nil != err {
return
}
// 发送请求
var nuwaRsp *resty.Response
if nuwaRsp, err = class100.NewResty().SetBody(Request{Package: pkg, Request: class100.Request{Environment: environment}}).
SetResult(&rsp).
Post(c.parseUrl("packages", version)); nil != err {
log.WithFields(log.Fields{
"nuwa": c,
"body": class100.RestyStringBody(nuwaRsp),
"error": err,
}).Error("提交打包请求出错")
return
}
if http.StatusAccepted != nuwaRsp.StatusCode() {
err = gox.NewCodeError(gox.ErrorCode(nuwaRsp.StatusCode()), "提交打包请求出错", nuwaRsp.String())
return
}
return
}
func (c Client) String() string {
jsonBytes, _ := json.MarshalIndent(c, "", " ")
return string(jsonBytes)
}