forked from denverdino/aliyungo
/
project.go
197 lines (154 loc) · 4.8 KB
/
project.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package cms
import (
"encoding/json"
// "errors"
// "fmt"
// "io/ioutil"
// "net/http"
"net/url"
// "strings"
)
// 调用接口返回的实体
// 新增加的结构首字母一定要大写,否则json编码或者解析编码会出现问题
type ResultModel struct {
Code string `json:"code"`
Message string `json:"message"`
Success bool `json:"success"`
}
//project的实体类,主要用于发送请求,更新或者添加Project
type ProjectModel struct {
ProjectName string `json:"projectName"`
ProjectDesc string `json:"projectDesc"`
ProjectOwner string `json:"projectOwner"`
}
//project返回实体,继承于ProjectModel
type ProjectResultModel struct {
ProjectModel
Id int64 `json:"id"`
GmtModified int64 `json:"gmtModified"`
GmtCreate int64 `json:"gmtCreate"`
Status int `json:"status"`
Creator string `json:"creator"`
}
//取得多个project的结构
type ProjectListResultModel struct {
Total int `json:"total"`
Datapoints []ProjectResultModel `json:"datapoints"`
}
//返回的实体类型
type GetProjectResult struct {
Result ProjectResultModel `json:"result"`
Code string `json:"code"`
Success bool `json:"success"`
}
//列表批量取得Project的请求参数
type ListProjectRequestModel struct {
Page string `json:"page"`
PageSize string `json:"pageSize"`
}
/**
* 创建一个Project
* @params
* ProjectModel 创建模块的模型
* @return
* result 创建返回的模型
* error 错误,如果一切Ok,error为 nil
*/
func (c *Client) CreateProject(projectName string, projectDesc string, projectOwner string) (result ResultModel, err error) {
project := &ProjectModel{
projectName,
projectDesc,
projectOwner,
}
postObjContent, _ := json.Marshal(project)
postJson := string(postObjContent)
requestUrl := c.GetUrl("projects", "", "")
requestPath := GetRequestPath("projects", "", "")
responseResult, err := c.GetResponseJson("POST", requestUrl, requestPath, postJson)
if err != nil {
return result, err
}
err = json.Unmarshal([]byte(responseResult), &result)
return result, err
}
/**
* 删除一个Project
* @params
* projectName 创建模块的模型
* @return
* result 返回的模型
* error 错误,如果一切Ok,error为 nil
*/
func (c *Client) DeleteProject(projectName string) (result ResultModel, err error) {
requestUrl := c.GetUrl("projects", "", projectName)
requestPath := GetRequestPath("projects", "", projectName)
responseResult, err := c.GetResponseJson("DELETE", requestUrl, requestPath, "")
if err != nil {
return result, err
}
err = json.Unmarshal([]byte(responseResult), &result)
return result, err
}
/**
* 更新一个Project
* @params
* projectName 创建模块的模型
* project 修改的模型,这个只能修改projectDesc和ProjectOwer
* @return
* result 返回的模型
* error 错误,如果一切Ok,error为 nil
*/
func (c *Client) UpdateProject(projectName string, project *ProjectModel) (result ResultModel, err error) {
postObjContent, _ := json.Marshal(project)
postJson := string(postObjContent)
requestUrl := c.GetUrl("projects", "", projectName)
requestPath := GetRequestPath("projects", "", projectName)
responseResult, err := c.GetResponseJson("PUT", requestUrl, requestPath, postJson)
if err != nil {
return result, err
}
err = json.Unmarshal([]byte(responseResult), &result)
return result, err
}
/**
* 取得一个Project
* @params
* projectName projectName不能为空
* @return
* result 返回的模型
* error 错误,如果一切Ok,error为 nil
*/
func (c *Client) GetProject(projectName string) (result GetProjectResult, err error) {
requestUrl := c.GetUrl("projects", "", projectName)
requestPath := GetRequestPath("projects", "", projectName)
responseResult, err := c.GetResponseJson("GET", requestUrl, requestPath, "")
if err != nil {
return result, err
}
err = json.Unmarshal([]byte(responseResult), &result)
return result, err
}
/**
* 取得一个多个Project
* @params
* page: 页码
* pageSize: 每夜记录条数
* projectOwner 所属用户
* @return
* result 返回的模型
* error 错误,如果一切Ok,error为 nil
*/
func (c *Client) GetProjectList(page string, pageSize string, projectOwner string) (result ProjectListResultModel, err error) {
params := url.Values{}
params.Add("Page", page)
params.Add("PageSize", pageSize)
if projectOwner != "" {
params.Add("ProjectOwner", projectOwner)
}
paramsString := params.Encode()
requestUrl := c.GetUrl("projects", "", "")
requestPath := GetRequestPath("projects", "", "")
responseResult, _ := c.GetResponseJson("GET", requestUrl+"?"+paramsString+"&resource", requestPath+"?"+paramsString+"&resource", "")
err = json.Unmarshal([]byte(responseResult), &result)
return result, err
}