api.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. package ups_sdk
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/go-resty/resty/v2"
  7. "net/http"
  8. "net/url"
  9. "path"
  10. "strconv"
  11. )
  12. const (
  13. registerUserUrl = "/ups/api/v1/user/register"
  14. deleteUserUrl = "/ups/api/v1/user/delete"
  15. identifyUrl = "/ups/api/v1/user/identify"
  16. updateUserUserNameUrl = "/ups/api/v1/user/userName/update"
  17. updateUserPasswordUrl = "/ups/api/v1/user/password/update"
  18. mergeUserUrl = "/ups/api/v1/user/merge"
  19. getUsersUrl = "/ups/api/v1/user/query"
  20. getMessagesUrl = "/ups/api/v1/message/query"
  21. )
  22. type API struct {
  23. baseUrl string
  24. httpClient *resty.Client
  25. }
  26. // NewAPI 创建API
  27. func NewAPI(baseUrl string) (*API, error) {
  28. _, err := url.ParseRequestURI(baseUrl)
  29. if err != nil {
  30. return nil, err
  31. }
  32. return &API{
  33. baseUrl: baseUrl,
  34. httpClient: resty.New().SetTimeout(HttpClientTimeout),
  35. }, nil
  36. }
  37. // RegisterUser 注册用户
  38. func (api *API) RegisterUser(userName string, password string) error {
  39. if IsStringEmpty(userName) {
  40. return errors.New("没有传递用户名")
  41. }
  42. if IsStringEmpty(password) {
  43. return errors.New("没有传递密码")
  44. }
  45. return api.registerUser(userName, password)
  46. }
  47. // DeleteUser 删除用户
  48. func (api *API) DeleteUser(userName string) error {
  49. if IsStringEmpty(userName) {
  50. return errors.New("没有传递用户名")
  51. }
  52. return api.deleteUser(userName)
  53. }
  54. // Identify 认证用户
  55. func (api *API) Identify(userName string, password string) error {
  56. if IsStringEmpty(userName) {
  57. return errors.New("没有传递用户名")
  58. }
  59. if IsStringEmpty(password) {
  60. return errors.New("没有传递密码")
  61. }
  62. return api.identify(userName, password)
  63. }
  64. // UpdateUserUserName 修改用户用户名
  65. func (api *API) UpdateUserUserName(oldUserName string, newUserName string) error {
  66. if IsStringEmpty(oldUserName) {
  67. return errors.New("没有传递旧用户名")
  68. }
  69. if IsStringEmpty(newUserName) {
  70. return errors.New("没有传递新用户名")
  71. }
  72. return api.updateUserUserName(oldUserName, newUserName)
  73. }
  74. // UpdateUserPassword 修改用户密码
  75. func (api *API) UpdateUserPassword(userName string, newPassword string) error {
  76. if IsStringEmpty(userName) {
  77. return errors.New("没有传递用户名")
  78. }
  79. if IsStringEmpty(newPassword) {
  80. return errors.New("没有传递新密码")
  81. }
  82. return api.updateUserPassword(userName, newPassword)
  83. }
  84. // MergeUser 合并用户,实际作用校验from和to用户存在性,保留to用户,删除from用户
  85. func (api *API) MergeUser(fromUserName string, toUserName string) error {
  86. if IsStringEmpty(fromUserName) {
  87. return errors.New("没有传递被合并的用户名")
  88. }
  89. if IsStringEmpty(toUserName) {
  90. return errors.New("没有传递合并到的用户名")
  91. }
  92. return api.mergeUser(fromUserName, toUserName)
  93. }
  94. // GetUsers 查询用户
  95. func (api *API) GetUsers(userName string, pageNo int, pageSize int) (*QueryUserResult, error) {
  96. return api.getUsers(userName, pageNo, pageSize)
  97. }
  98. // GetMessages 查询消息
  99. func (api *API) GetMessages(topic string, startSendTime string, endSendTime string, pageNo int, pageSize int) (*QueryMessageResult, error) {
  100. return api.getMessages(topic, startSendTime, endSendTime, pageNo, pageSize)
  101. }
  102. func (api *API) registerUser(userName string, password string) error {
  103. requestUrl := path.Join(api.baseUrl, registerUserUrl)
  104. resp, err := api.httpClient.R().
  105. SetHeader("Content-Type", "application/json").
  106. SetBody(&RegisterUserRequest{
  107. UserName: userName,
  108. Password: password,
  109. }).
  110. Post(requestUrl)
  111. if err != nil {
  112. return err
  113. }
  114. if resp.IsError() {
  115. return fmt.Errorf("请求失败: URL %s, Method %s, Status Code %d, Status %s\n",
  116. requestUrl, http.MethodPost, resp.StatusCode(), resp.Status())
  117. }
  118. response := new(RegisterUserResponse)
  119. err = json.Unmarshal(resp.Body(), response)
  120. if err != nil {
  121. return err
  122. }
  123. if !response.Success {
  124. return fmt.Errorf("请求失败: URL %s, Method: %s, Error %s\n",
  125. requestUrl, http.MethodPost, response.Msg)
  126. }
  127. return nil
  128. }
  129. func (api *API) deleteUser(userName string) error {
  130. requestUrl := path.Join(api.baseUrl, deleteUserUrl)
  131. resp, err := api.httpClient.R().
  132. SetHeader("Content-Type", "application/json").
  133. SetQueryParam("userName", userName).
  134. Delete(requestUrl)
  135. if err != nil {
  136. return err
  137. }
  138. if resp.IsError() {
  139. return fmt.Errorf("请求失败: URL %s, Method: %s, Status Code %d, Status %s\n",
  140. requestUrl, http.MethodDelete, resp.StatusCode(), resp.Status())
  141. }
  142. response := new(DeleteUserResponse)
  143. err = json.Unmarshal(resp.Body(), response)
  144. if err != nil {
  145. return err
  146. }
  147. if !response.Success {
  148. return fmt.Errorf("请求失败: URL %s, Method: %s, Error %s\n",
  149. requestUrl, http.MethodDelete, response.Msg)
  150. }
  151. return nil
  152. }
  153. func (api *API) identify(userName string, password string) error {
  154. requestUrl := path.Join(api.baseUrl, identifyUrl)
  155. resp, err := api.httpClient.R().
  156. SetHeader("Content-Type", "application/json").
  157. SetBody(&IdentifyRequest{
  158. UserName: userName,
  159. Password: password,
  160. }).
  161. Post(requestUrl)
  162. if err != nil {
  163. return err
  164. }
  165. if resp.IsError() {
  166. return fmt.Errorf("请求失败: URL %s, Method %s, Status Code %d, Status %s\n",
  167. requestUrl, http.MethodPost, resp.StatusCode(), resp.Status())
  168. }
  169. response := new(IdentifyResponse)
  170. err = json.Unmarshal(resp.Body(), response)
  171. if err != nil {
  172. return err
  173. }
  174. if !response.Success {
  175. return fmt.Errorf("请求失败: URL %s, Method: %s, Error %s\n",
  176. requestUrl, http.MethodPost, response.Msg)
  177. }
  178. return nil
  179. }
  180. func (api *API) updateUserUserName(oldUserName string, newUserName string) error {
  181. requestUrl := path.Join(api.baseUrl, updateUserUserNameUrl)
  182. resp, err := api.httpClient.R().
  183. SetHeader("Content-Type", "application/json").
  184. SetBody(&UpdateUserUserNameRequest{
  185. OldUserName: oldUserName,
  186. NewUserName: newUserName,
  187. }).
  188. Put(requestUrl)
  189. if err != nil {
  190. return err
  191. }
  192. if resp.IsError() {
  193. return fmt.Errorf("请求失败: URL %s, Method %s, Status Code %d, Status %s\n",
  194. requestUrl, http.MethodPut, resp.StatusCode(), resp.Status())
  195. }
  196. response := new(UpdateUserUserNameResponse)
  197. err = json.Unmarshal(resp.Body(), response)
  198. if err != nil {
  199. return err
  200. }
  201. if !response.Success {
  202. return fmt.Errorf("请求失败: URL %s, Method: %s, Error %s\n",
  203. requestUrl, http.MethodPut, response.Msg)
  204. }
  205. return nil
  206. }
  207. func (api *API) updateUserPassword(userName string, newPassword string) error {
  208. requestUrl := path.Join(api.baseUrl, updateUserPasswordUrl)
  209. resp, err := api.httpClient.R().
  210. SetHeader("Content-Type", "application/json").
  211. SetBody(&UpdateUserPasswordRequest{
  212. UserName: userName,
  213. NewPassword: newPassword,
  214. }).
  215. Put(requestUrl)
  216. if err != nil {
  217. return err
  218. }
  219. if resp.IsError() {
  220. return fmt.Errorf("请求失败: URL %s, Method %s, Status Code %d, Status %s\n",
  221. requestUrl, http.MethodPut, resp.StatusCode(), resp.Status())
  222. }
  223. response := new(UpdateUserPasswordResponse)
  224. err = json.Unmarshal(resp.Body(), response)
  225. if err != nil {
  226. return err
  227. }
  228. if !response.Success {
  229. return fmt.Errorf("请求失败: URL %s, Method: %s, Error %s\n",
  230. requestUrl, http.MethodPut, response.Msg)
  231. }
  232. return nil
  233. }
  234. func (api *API) mergeUser(fromUserName string, toUserName string) error {
  235. requestUrl := path.Join(api.baseUrl, mergeUserUrl)
  236. resp, err := api.httpClient.R().
  237. SetHeader("Content-Type", "application/json").
  238. SetBody(&MergeUserRequest{
  239. FromUserName: fromUserName,
  240. ToUserName: toUserName,
  241. }).
  242. Post(requestUrl)
  243. if err != nil {
  244. return err
  245. }
  246. if resp.IsError() {
  247. return fmt.Errorf("请求失败: URL %s, Method %s, Status Code %d, Status %s\n",
  248. requestUrl, http.MethodPost, resp.StatusCode(), resp.Status())
  249. }
  250. response := new(MergeUserResponse)
  251. err = json.Unmarshal(resp.Body(), response)
  252. if err != nil {
  253. return err
  254. }
  255. if !response.Success {
  256. return fmt.Errorf("请求失败: URL %s, Method: %s, Error %s\n",
  257. requestUrl, http.MethodPost, response.Msg)
  258. }
  259. return nil
  260. }
  261. func (api *API) getUsers(userName string, pageNo int, pageSize int) (*QueryUserResult, error) {
  262. requestUrl := path.Join(api.baseUrl, getUsersUrl)
  263. resp, err := api.httpClient.R().
  264. SetHeader("Content-Type", "application/json").
  265. SetQueryParam("userName", userName).
  266. SetQueryParam("pageNo", strconv.Itoa(pageNo)).
  267. SetQueryParam("pageSize", strconv.Itoa(pageSize)).
  268. Get(requestUrl)
  269. if err != nil {
  270. return nil, err
  271. }
  272. if resp.IsError() {
  273. return nil, fmt.Errorf("请求失败: URL %s, Method %s, Status Code %d, Status %s\n",
  274. requestUrl, http.MethodGet, resp.StatusCode(), resp.Status())
  275. }
  276. response := new(GetUsersResponse)
  277. err = json.Unmarshal(resp.Body(), response)
  278. if err != nil {
  279. return nil, err
  280. }
  281. if !response.Success {
  282. return nil, fmt.Errorf("请求失败: URL %s, Method: %s, Error %s\n",
  283. requestUrl, http.MethodGet, response.Msg)
  284. }
  285. return &response.QueryUserResult, nil
  286. }
  287. func (api *API) getMessages(topic string, startSendTime string, endSendTime string, pageNo int, pageSize int) (*QueryMessageResult, error) {
  288. requestUrl := path.Join(api.baseUrl, getMessagesUrl)
  289. resp, err := api.httpClient.R().
  290. SetHeader("Content-Type", "application/json").
  291. SetQueryParam("topic", topic).
  292. SetQueryParam("startSendTime", startSendTime).
  293. SetQueryParam("endSendTime", endSendTime).
  294. SetQueryParam("pageNo", strconv.Itoa(pageNo)).
  295. SetQueryParam("pageSize", strconv.Itoa(pageSize)).
  296. Get(requestUrl)
  297. if err != nil {
  298. return nil, err
  299. }
  300. if resp.IsError() {
  301. return nil, fmt.Errorf("请求失败: URL %s, Method %s, Status Code %d, Status %s\n",
  302. requestUrl, http.MethodGet, resp.StatusCode(), resp.Status())
  303. }
  304. response := new(GetMessagesResponse)
  305. err = json.Unmarshal(resp.Body(), response)
  306. if err != nil {
  307. return nil, err
  308. }
  309. if !response.Success {
  310. return nil, fmt.Errorf("请求失败: URL %s, Method: %s, Error %s\n",
  311. requestUrl, http.MethodGet, response.Msg)
  312. }
  313. return &response.QueryMessageResult, nil
  314. }