api.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. type SaveUserTransactionFunc func() error
  38. type SaveUserRollbackFunc func() error
  39. type SaveUserCommitFunc func() error
  40. type RegisterUserFlowParams struct {
  41. UserName string
  42. Password string
  43. SaveUserTransactionFunc SaveUserTransactionFunc
  44. SaveUserRollbackFunc SaveUserRollbackFunc
  45. SaveUserCommitFunc SaveUserCommitFunc
  46. }
  47. func (params *RegisterUserFlowParams) Check() error {
  48. if IsStringEmpty(params.UserName) {
  49. return errors.New("没有传递用户名")
  50. }
  51. if IsStringEmpty(params.Password) {
  52. return errors.New("没有传递密码")
  53. }
  54. if params.SaveUserTransactionFunc == nil {
  55. return errors.New("没有传递保存用户名事务函数")
  56. }
  57. if params.SaveUserRollbackFunc == nil {
  58. return errors.New("没有传递保存用户名事务回滚函数")
  59. }
  60. if params.SaveUserCommitFunc == nil {
  61. return errors.New("没有传递保存用户名事务回滚函数")
  62. }
  63. return nil
  64. }
  65. // RegisterUserFlow 注册用户流程
  66. func (api *API) RegisterUserFlow(params *RegisterUserFlowParams) error {
  67. return nil
  68. }
  69. // RegisterUser 注册用户
  70. func (api *API) RegisterUser(userName string, password string) error {
  71. if IsStringEmpty(userName) {
  72. return errors.New("没有传递用户名")
  73. }
  74. if IsStringEmpty(password) {
  75. return errors.New("没有传递密码")
  76. }
  77. return api.registerUser(userName, password)
  78. }
  79. // DeleteUser 删除用户
  80. func (api *API) DeleteUser(userName string) error {
  81. if IsStringEmpty(userName) {
  82. return errors.New("没有传递用户名")
  83. }
  84. return api.deleteUser(userName)
  85. }
  86. // Identify 认证用户
  87. func (api *API) Identify(userName string, password string) error {
  88. if IsStringEmpty(userName) {
  89. return errors.New("没有传递用户名")
  90. }
  91. if IsStringEmpty(password) {
  92. return errors.New("没有传递密码")
  93. }
  94. return api.identify(userName, password)
  95. }
  96. // UpdateUserUserName 修改用户用户名
  97. func (api *API) UpdateUserUserName(oldUserName string, newUserName string) error {
  98. if IsStringEmpty(oldUserName) {
  99. return errors.New("没有传递旧用户名")
  100. }
  101. if IsStringEmpty(newUserName) {
  102. return errors.New("没有传递新用户名")
  103. }
  104. return api.updateUserUserName(oldUserName, newUserName)
  105. }
  106. // UpdateUserPassword 修改用户密码
  107. func (api *API) UpdateUserPassword(userName string, newPassword string) error {
  108. if IsStringEmpty(userName) {
  109. return errors.New("没有传递用户名")
  110. }
  111. if IsStringEmpty(newPassword) {
  112. return errors.New("没有传递新密码")
  113. }
  114. return api.updateUserPassword(userName, newPassword)
  115. }
  116. // MergeUser 合并用户,实际作用校验from和to用户存在性,保留to用户,删除from用户
  117. func (api *API) MergeUser(fromUserName string, toUserName string) error {
  118. if IsStringEmpty(fromUserName) {
  119. return errors.New("没有传递被合并的用户名")
  120. }
  121. if IsStringEmpty(toUserName) {
  122. return errors.New("没有传递合并到的用户名")
  123. }
  124. return api.mergeUser(fromUserName, toUserName)
  125. }
  126. // GetUsers 查询用户
  127. func (api *API) GetUsers(userName string, pageNo int, pageSize int) (*QueryUserResult, error) {
  128. return api.getUsers(userName, pageNo, pageSize)
  129. }
  130. // GetMessages 查询消息
  131. func (api *API) GetMessages(topic string, startSendTime string, endSendTime string, pageNo int, pageSize int) (*QueryMessageResult, error) {
  132. return api.getMessages(topic, startSendTime, endSendTime, pageNo, pageSize)
  133. }
  134. func (api *API) registerUser(userName string, password string) error {
  135. requestUrl := path.Join(api.baseUrl, registerUserUrl)
  136. resp, err := api.httpClient.R().
  137. SetHeader("Content-Type", "application/json").
  138. SetBody(&RegisterUserRequest{
  139. UserName: userName,
  140. Password: password,
  141. }).
  142. Post(requestUrl)
  143. if err != nil {
  144. return err
  145. }
  146. if resp.IsError() {
  147. return fmt.Errorf("请求失败: URL %s, Method %s, Status Code %d, Status %s\n",
  148. requestUrl, http.MethodPost, resp.StatusCode(), resp.Status())
  149. }
  150. response := new(RegisterUserResponse)
  151. err = json.Unmarshal(resp.Body(), response)
  152. if err != nil {
  153. return err
  154. }
  155. if !response.Success {
  156. return fmt.Errorf("请求失败: URL %s, Method: %s, Error %s\n",
  157. requestUrl, http.MethodPost, response.Msg)
  158. }
  159. return nil
  160. }
  161. func (api *API) deleteUser(userName string) error {
  162. requestUrl := path.Join(api.baseUrl, deleteUserUrl)
  163. resp, err := api.httpClient.R().
  164. SetHeader("Content-Type", "application/json").
  165. SetQueryParam("userName", userName).
  166. Delete(requestUrl)
  167. if err != nil {
  168. return err
  169. }
  170. if resp.IsError() {
  171. return fmt.Errorf("请求失败: URL %s, Method: %s, Status Code %d, Status %s\n",
  172. requestUrl, http.MethodDelete, resp.StatusCode(), resp.Status())
  173. }
  174. response := new(DeleteUserResponse)
  175. err = json.Unmarshal(resp.Body(), response)
  176. if err != nil {
  177. return err
  178. }
  179. if !response.Success {
  180. return fmt.Errorf("请求失败: URL %s, Method: %s, Error %s\n",
  181. requestUrl, http.MethodDelete, response.Msg)
  182. }
  183. return nil
  184. }
  185. func (api *API) identify(userName string, password string) error {
  186. requestUrl := path.Join(api.baseUrl, identifyUrl)
  187. resp, err := api.httpClient.R().
  188. SetHeader("Content-Type", "application/json").
  189. SetBody(&IdentifyRequest{
  190. UserName: userName,
  191. Password: password,
  192. }).
  193. Post(requestUrl)
  194. if err != nil {
  195. return err
  196. }
  197. if resp.IsError() {
  198. return fmt.Errorf("请求失败: URL %s, Method %s, Status Code %d, Status %s\n",
  199. requestUrl, http.MethodPost, resp.StatusCode(), resp.Status())
  200. }
  201. response := new(IdentifyResponse)
  202. err = json.Unmarshal(resp.Body(), response)
  203. if err != nil {
  204. return err
  205. }
  206. if !response.Success {
  207. return fmt.Errorf("请求失败: URL %s, Method: %s, Error %s\n",
  208. requestUrl, http.MethodPost, response.Msg)
  209. }
  210. return nil
  211. }
  212. func (api *API) updateUserUserName(oldUserName string, newUserName string) error {
  213. requestUrl := path.Join(api.baseUrl, updateUserUserNameUrl)
  214. resp, err := api.httpClient.R().
  215. SetHeader("Content-Type", "application/json").
  216. SetBody(&UpdateUserUserNameRequest{
  217. OldUserName: oldUserName,
  218. NewUserName: newUserName,
  219. }).
  220. Put(requestUrl)
  221. if err != nil {
  222. return err
  223. }
  224. if resp.IsError() {
  225. return fmt.Errorf("请求失败: URL %s, Method %s, Status Code %d, Status %s\n",
  226. requestUrl, http.MethodPut, resp.StatusCode(), resp.Status())
  227. }
  228. response := new(UpdateUserUserNameResponse)
  229. err = json.Unmarshal(resp.Body(), response)
  230. if err != nil {
  231. return err
  232. }
  233. if !response.Success {
  234. return fmt.Errorf("请求失败: URL %s, Method: %s, Error %s\n",
  235. requestUrl, http.MethodPut, response.Msg)
  236. }
  237. return nil
  238. }
  239. func (api *API) updateUserPassword(userName string, newPassword string) error {
  240. requestUrl := path.Join(api.baseUrl, updateUserPasswordUrl)
  241. resp, err := api.httpClient.R().
  242. SetHeader("Content-Type", "application/json").
  243. SetBody(&UpdateUserPasswordRequest{
  244. UserName: userName,
  245. NewPassword: newPassword,
  246. }).
  247. Put(requestUrl)
  248. if err != nil {
  249. return err
  250. }
  251. if resp.IsError() {
  252. return fmt.Errorf("请求失败: URL %s, Method %s, Status Code %d, Status %s\n",
  253. requestUrl, http.MethodPut, resp.StatusCode(), resp.Status())
  254. }
  255. response := new(UpdateUserPasswordResponse)
  256. err = json.Unmarshal(resp.Body(), response)
  257. if err != nil {
  258. return err
  259. }
  260. if !response.Success {
  261. return fmt.Errorf("请求失败: URL %s, Method: %s, Error %s\n",
  262. requestUrl, http.MethodPut, response.Msg)
  263. }
  264. return nil
  265. }
  266. func (api *API) mergeUser(fromUserName string, toUserName string) error {
  267. requestUrl := path.Join(api.baseUrl, mergeUserUrl)
  268. resp, err := api.httpClient.R().
  269. SetHeader("Content-Type", "application/json").
  270. SetBody(&MergeUserRequest{
  271. FromUserName: fromUserName,
  272. ToUserName: toUserName,
  273. }).
  274. Post(requestUrl)
  275. if err != nil {
  276. return err
  277. }
  278. if resp.IsError() {
  279. return fmt.Errorf("请求失败: URL %s, Method %s, Status Code %d, Status %s\n",
  280. requestUrl, http.MethodPost, resp.StatusCode(), resp.Status())
  281. }
  282. response := new(MergeUserResponse)
  283. err = json.Unmarshal(resp.Body(), response)
  284. if err != nil {
  285. return err
  286. }
  287. if !response.Success {
  288. return fmt.Errorf("请求失败: URL %s, Method: %s, Error %s\n",
  289. requestUrl, http.MethodPost, response.Msg)
  290. }
  291. return nil
  292. }
  293. func (api *API) getUsers(userName string, pageNo int, pageSize int) (*QueryUserResult, error) {
  294. requestUrl := path.Join(api.baseUrl, getUsersUrl)
  295. resp, err := api.httpClient.R().
  296. SetHeader("Content-Type", "application/json").
  297. SetQueryParam("userName", userName).
  298. SetQueryParam("pageNo", strconv.Itoa(pageNo)).
  299. SetQueryParam("pageSize", strconv.Itoa(pageSize)).
  300. Get(requestUrl)
  301. if err != nil {
  302. return nil, err
  303. }
  304. if resp.IsError() {
  305. return nil, fmt.Errorf("请求失败: URL %s, Method %s, Status Code %d, Status %s\n",
  306. requestUrl, http.MethodGet, resp.StatusCode(), resp.Status())
  307. }
  308. response := new(GetUsersResponse)
  309. err = json.Unmarshal(resp.Body(), response)
  310. if err != nil {
  311. return nil, err
  312. }
  313. if !response.Success {
  314. return nil, fmt.Errorf("请求失败: URL %s, Method: %s, Error %s\n",
  315. requestUrl, http.MethodGet, response.Msg)
  316. }
  317. return &response.QueryUserResult, nil
  318. }
  319. func (api *API) getMessages(topic string, startSendTime string, endSendTime string, pageNo int, pageSize int) (*QueryMessageResult, error) {
  320. requestUrl := path.Join(api.baseUrl, getMessagesUrl)
  321. resp, err := api.httpClient.R().
  322. SetHeader("Content-Type", "application/json").
  323. SetQueryParam("topic", topic).
  324. SetQueryParam("startSendTime", startSendTime).
  325. SetQueryParam("endSendTime", endSendTime).
  326. SetQueryParam("pageNo", strconv.Itoa(pageNo)).
  327. SetQueryParam("pageSize", strconv.Itoa(pageSize)).
  328. Get(requestUrl)
  329. if err != nil {
  330. return nil, err
  331. }
  332. if resp.IsError() {
  333. return nil, fmt.Errorf("请求失败: URL %s, Method %s, Status Code %d, Status %s\n",
  334. requestUrl, http.MethodGet, resp.StatusCode(), resp.Status())
  335. }
  336. response := new(GetMessagesResponse)
  337. err = json.Unmarshal(resp.Body(), response)
  338. if err != nil {
  339. return nil, err
  340. }
  341. if !response.Success {
  342. return nil, fmt.Errorf("请求失败: URL %s, Method: %s, Error %s\n",
  343. requestUrl, http.MethodGet, response.Msg)
  344. }
  345. return &response.QueryMessageResult, nil
  346. }