| 1 |
package cms |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
"net/url" |
| 10 |
|
| 11 |
) |
| 12 |
|
| 13 |
口返回的实体 |
| 14 |
要大写,否则json编码或者解析编码会出现问题 |
| 15 |
type ResultModel struct { |
| 16 |
Code string `json:"code"` |
| 17 |
Message string `json:"message"` |
| 18 |
Success bool `json:"success"` |
| 19 |
} |
| 20 |
|
| 21 |
发送请求,更新或者添加Project |
| 22 |
type ProjectModel struct { |
| 23 |
ProjectName string `json:"projectName"` |
| 24 |
ProjectDesc string `json:"projectDesc"` |
| 25 |
ProjectOwner string `json:"projectOwner"` |
| 26 |
} |
| 27 |
|
| 28 |
于ProjectModel |
| 29 |
type ProjectResultModel struct { |
| 30 |
ProjectModel |
| 31 |
Id int64 `json:"id"` |
| 32 |
GmtModified int64 `json:"gmtModified"` |
| 33 |
GmtCreate int64 `json:"gmtCreate"` |
| 34 |
Status int `json:"status"` |
| 35 |
Creator string `json:"creator"` |
| 36 |
} |
| 37 |
|
| 38 |
oject的结构 |
| 39 |
type ProjectListResultModel struct { |
| 40 |
Total int `json:"total"` |
| 41 |
Datapoints []ProjectResultModel `json:"datapoints"` |
| 42 |
} |
| 43 |
|
| 44 |
的实体类型 |
| 45 |
type GetProjectResult struct { |
| 46 |
Result ProjectResultModel `json:"result"` |
| 47 |
Code string `json:"code"` |
| 48 |
Success bool `json:"success"` |
| 49 |
} |
| 50 |
|
| 51 |
Project的请求参数 |
| 52 |
type ListProjectRequestModel struct { |
| 53 |
Page string `json:"page"` |
| 54 |
PageSize string `json:"pageSize"` |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
个Project |
| 59 |
|
| 60 |
模块的模型 |
| 61 |
|
| 62 |
返回的模型 |
| 63 |
切Ok,error为 nil |
| 64 |
|
| 65 |
func (c *Client) CreateProject(projectName string, projectDesc string, projectOwner string) (result ResultModel, err error) { |
| 66 |
|
| 67 |
project := &ProjectModel{ |
| 68 |
projectName, |
| 69 |
projectDesc, |
| 70 |
projectOwner, |
| 71 |
} |
| 72 |
|
| 73 |
postObjContent, _ := json.Marshal(project) |
| 74 |
postJson := string(postObjContent) |
| 75 |
|
| 76 |
requestUrl := c.GetUrl("projects", "", "") |
| 77 |
requestPath := GetRequestPath("projects", "", "") |
| 78 |
|
| 79 |
responseResult, err := c.GetResponseJson("POST", requestUrl, requestPath, postJson) |
| 80 |
|
| 81 |
if err != nil { |
| 82 |
return result, err |
| 83 |
} |
| 84 |
|
| 85 |
err = json.Unmarshal([]byte(responseResult), &result) |
| 86 |
|
| 87 |
return result, err |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
个Project |
| 92 |
|
| 93 |
模块的模型 |
| 94 |
|
| 95 |
回的模型 |
| 96 |
切Ok,error为 nil |
| 97 |
|
| 98 |
func (c *Client) DeleteProject(projectName string) (result ResultModel, err error) { |
| 99 |
|
| 100 |
requestUrl := c.GetUrl("projects", "", projectName) |
| 101 |
requestPath := GetRequestPath("projects", "", projectName) |
| 102 |
|
| 103 |
responseResult, err := c.GetResponseJson("DELETE", requestUrl, requestPath, "") |
| 104 |
|
| 105 |
if err != nil { |
| 106 |
return result, err |
| 107 |
} |
| 108 |
|
| 109 |
err = json.Unmarshal([]byte(responseResult), &result) |
| 110 |
|
| 111 |
return result, err |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
个Project |
| 117 |
|
| 118 |
模块的模型 |
| 119 |
改projectDesc和ProjectOwer |
| 120 |
|
| 121 |
回的模型 |
| 122 |
切Ok,error为 nil |
| 123 |
|
| 124 |
func (c *Client) UpdateProject(projectName string, project *ProjectModel) (result ResultModel, err error) { |
| 125 |
|
| 126 |
postObjContent, _ := json.Marshal(project) |
| 127 |
postJson := string(postObjContent) |
| 128 |
|
| 129 |
requestUrl := c.GetUrl("projects", "", projectName) |
| 130 |
requestPath := GetRequestPath("projects", "", projectName) |
| 131 |
|
| 132 |
responseResult, err := c.GetResponseJson("PUT", requestUrl, requestPath, postJson) |
| 133 |
|
| 134 |
if err != nil { |
| 135 |
return result, err |
| 136 |
} |
| 137 |
|
| 138 |
err = json.Unmarshal([]byte(responseResult), &result) |
| 139 |
|
| 140 |
return result, err |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
个Project |
| 145 |
|
| 146 |
能为空 |
| 147 |
|
| 148 |
回的模型 |
| 149 |
切Ok,error为 nil |
| 150 |
|
| 151 |
func (c *Client) GetProject(projectName string) (result GetProjectResult, err error) { |
| 152 |
|
| 153 |
requestUrl := c.GetUrl("projects", "", projectName) |
| 154 |
requestPath := GetRequestPath("projects", "", projectName) |
| 155 |
|
| 156 |
responseResult, err := c.GetResponseJson("GET", requestUrl, requestPath, "") |
| 157 |
|
| 158 |
if err != nil { |
| 159 |
return result, err |
| 160 |
} |
| 161 |
|
| 162 |
err = json.Unmarshal([]byte(responseResult), &result) |
| 163 |
|
| 164 |
return result, err |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
多个Project |
| 169 |
|
| 170 |
页码 |
| 171 |
记录条数 |
| 172 |
属用户 |
| 173 |
|
| 174 |
回的模型 |
| 175 |
切Ok,error为 nil |
| 176 |
|
| 177 |
func (c *Client) GetProjectList(page string, pageSize string, projectOwner string) (result ProjectListResultModel, err error) { |
| 178 |
|
| 179 |
params := url.Values{} |
| 180 |
params.Add("Page", page) |
| 181 |
params.Add("PageSize", pageSize) |
| 182 |
|
| 183 |
if projectOwner != "" { |
| 184 |
params.Add("ProjectOwner", projectOwner) |
| 185 |
} |
| 186 |
|
| 187 |
paramsString := params.Encode() |
| 188 |
|
| 189 |
requestUrl := c.GetUrl("projects", "", "") |
| 190 |
requestPath := GetRequestPath("projects", "", "") |
| 191 |
|
| 192 |
responseResult, _ := c.GetResponseJson("GET", requestUrl+"?"+paramsString+"&resource", requestPath+"?"+paramsString+"&resource", "") |
| 193 |
|
| 194 |
err = json.Unmarshal([]byte(responseResult), &result) |
| 195 |
|
| 196 |
return result, err |
| 197 |
} |