123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386 |
- package ups_sdk
- import (
- "encoding/json"
- "errors"
- "fmt"
- "github.com/go-resty/resty/v2"
- "net/http"
- "net/url"
- "path"
- "strconv"
- )
- const (
- registerUserUrl = "/ups/api/v1/user/register"
- deleteUserUrl = "/ups/api/v1/user/delete"
- identifyUrl = "/ups/api/v1/user/identify"
- updateUserUserNameUrl = "/ups/api/v1/user/userName/update"
- updateUserPasswordUrl = "/ups/api/v1/user/password/update"
- mergeUserUrl = "/ups/api/v1/user/merge"
- getUsersUrl = "/ups/api/v1/user/query"
- getMessagesUrl = "/ups/api/v1/message/query"
- )
- type API struct {
- baseUrl string
- httpClient *resty.Client
- }
- // NewAPI 创建API
- func NewAPI(baseUrl string) (*API, error) {
- _, err := url.ParseRequestURI(baseUrl)
- if err != nil {
- return nil, err
- }
- return &API{
- baseUrl: baseUrl,
- httpClient: resty.New().SetTimeout(HttpClientTimeout),
- }, nil
- }
- // RegisterUser 注册用户
- func (api *API) RegisterUser(userName string, password string) error {
- if IsStringEmpty(userName) {
- return errors.New("没有传递用户名")
- }
- if IsStringEmpty(password) {
- return errors.New("没有传递密码")
- }
- return api.registerUser(userName, password)
- }
- // DeleteUser 删除用户
- func (api *API) DeleteUser(userName string) error {
- if IsStringEmpty(userName) {
- return errors.New("没有传递用户名")
- }
- return api.deleteUser(userName)
- }
- // Identify 认证用户
- func (api *API) Identify(userName string, password string) error {
- if IsStringEmpty(userName) {
- return errors.New("没有传递用户名")
- }
- if IsStringEmpty(password) {
- return errors.New("没有传递密码")
- }
- return api.identify(userName, password)
- }
- // UpdateUserUserName 修改用户用户名
- func (api *API) UpdateUserUserName(oldUserName string, newUserName string) error {
- if IsStringEmpty(oldUserName) {
- return errors.New("没有传递旧用户名")
- }
- if IsStringEmpty(newUserName) {
- return errors.New("没有传递新用户名")
- }
- return api.updateUserUserName(oldUserName, newUserName)
- }
- // UpdateUserPassword 修改用户密码
- func (api *API) UpdateUserPassword(userName string, newPassword string) error {
- if IsStringEmpty(userName) {
- return errors.New("没有传递用户名")
- }
- if IsStringEmpty(newPassword) {
- return errors.New("没有传递新密码")
- }
- return api.updateUserPassword(userName, newPassword)
- }
- // MergeUser 合并用户,实际作用校验from和to用户存在性,保留to用户,删除from用户
- func (api *API) MergeUser(fromUserName string, toUserName string) error {
- if IsStringEmpty(fromUserName) {
- return errors.New("没有传递被合并的用户名")
- }
- if IsStringEmpty(toUserName) {
- return errors.New("没有传递合并到的用户名")
- }
- return api.mergeUser(fromUserName, toUserName)
- }
- // GetUsers 查询用户
- func (api *API) GetUsers(userName string, pageNo int, pageSize int) (*QueryUserResult, error) {
- return api.getUsers(userName, pageNo, pageSize)
- }
- // GetMessages 查询消息
- func (api *API) GetMessages(topic string, startSendTime string, endSendTime string, pageNo int, pageSize int) (*QueryMessageResult, error) {
- return api.getMessages(topic, startSendTime, endSendTime, pageNo, pageSize)
- }
- func (api *API) registerUser(userName string, password string) error {
- requestUrl := path.Join(api.baseUrl, registerUserUrl)
- resp, err := api.httpClient.R().
- SetHeader("Content-Type", "application/json").
- SetBody(&RegisterUserRequest{
- UserName: userName,
- Password: password,
- }).
- Post(requestUrl)
- if err != nil {
- return err
- }
- if resp.IsError() {
- return fmt.Errorf("请求失败: URL %s, Method %s, Status Code %d, Status %s\n",
- requestUrl, http.MethodPost, resp.StatusCode(), resp.Status())
- }
- response := new(RegisterUserResponse)
- err = json.Unmarshal(resp.Body(), response)
- if err != nil {
- return err
- }
- if !response.Success {
- return fmt.Errorf("请求失败: URL %s, Method: %s, Error %s\n",
- requestUrl, http.MethodPost, response.Msg)
- }
- return nil
- }
- func (api *API) deleteUser(userName string) error {
- requestUrl := path.Join(api.baseUrl, deleteUserUrl)
- resp, err := api.httpClient.R().
- SetHeader("Content-Type", "application/json").
- SetQueryParam("userName", userName).
- Delete(requestUrl)
- if err != nil {
- return err
- }
- if resp.IsError() {
- return fmt.Errorf("请求失败: URL %s, Method: %s, Status Code %d, Status %s\n",
- requestUrl, http.MethodDelete, resp.StatusCode(), resp.Status())
- }
- response := new(DeleteUserResponse)
- err = json.Unmarshal(resp.Body(), response)
- if err != nil {
- return err
- }
- if !response.Success {
- return fmt.Errorf("请求失败: URL %s, Method: %s, Error %s\n",
- requestUrl, http.MethodDelete, response.Msg)
- }
- return nil
- }
- func (api *API) identify(userName string, password string) error {
- requestUrl := path.Join(api.baseUrl, identifyUrl)
- resp, err := api.httpClient.R().
- SetHeader("Content-Type", "application/json").
- SetBody(&IdentifyRequest{
- UserName: userName,
- Password: password,
- }).
- Post(requestUrl)
- if err != nil {
- return err
- }
- if resp.IsError() {
- return fmt.Errorf("请求失败: URL %s, Method %s, Status Code %d, Status %s\n",
- requestUrl, http.MethodPost, resp.StatusCode(), resp.Status())
- }
- response := new(IdentifyResponse)
- err = json.Unmarshal(resp.Body(), response)
- if err != nil {
- return err
- }
- if !response.Success {
- return fmt.Errorf("请求失败: URL %s, Method: %s, Error %s\n",
- requestUrl, http.MethodPost, response.Msg)
- }
- return nil
- }
- func (api *API) updateUserUserName(oldUserName string, newUserName string) error {
- requestUrl := path.Join(api.baseUrl, updateUserUserNameUrl)
- resp, err := api.httpClient.R().
- SetHeader("Content-Type", "application/json").
- SetBody(&UpdateUserUserNameRequest{
- OldUserName: oldUserName,
- NewUserName: newUserName,
- }).
- Put(requestUrl)
- if err != nil {
- return err
- }
- if resp.IsError() {
- return fmt.Errorf("请求失败: URL %s, Method %s, Status Code %d, Status %s\n",
- requestUrl, http.MethodPut, resp.StatusCode(), resp.Status())
- }
- response := new(UpdateUserUserNameResponse)
- err = json.Unmarshal(resp.Body(), response)
- if err != nil {
- return err
- }
- if !response.Success {
- return fmt.Errorf("请求失败: URL %s, Method: %s, Error %s\n",
- requestUrl, http.MethodPut, response.Msg)
- }
- return nil
- }
- func (api *API) updateUserPassword(userName string, newPassword string) error {
- requestUrl := path.Join(api.baseUrl, updateUserPasswordUrl)
- resp, err := api.httpClient.R().
- SetHeader("Content-Type", "application/json").
- SetBody(&UpdateUserPasswordRequest{
- UserName: userName,
- NewPassword: newPassword,
- }).
- Put(requestUrl)
- if err != nil {
- return err
- }
- if resp.IsError() {
- return fmt.Errorf("请求失败: URL %s, Method %s, Status Code %d, Status %s\n",
- requestUrl, http.MethodPut, resp.StatusCode(), resp.Status())
- }
- response := new(UpdateUserPasswordResponse)
- err = json.Unmarshal(resp.Body(), response)
- if err != nil {
- return err
- }
- if !response.Success {
- return fmt.Errorf("请求失败: URL %s, Method: %s, Error %s\n",
- requestUrl, http.MethodPut, response.Msg)
- }
- return nil
- }
- func (api *API) mergeUser(fromUserName string, toUserName string) error {
- requestUrl := path.Join(api.baseUrl, mergeUserUrl)
- resp, err := api.httpClient.R().
- SetHeader("Content-Type", "application/json").
- SetBody(&MergeUserRequest{
- FromUserName: fromUserName,
- ToUserName: toUserName,
- }).
- Post(requestUrl)
- if err != nil {
- return err
- }
- if resp.IsError() {
- return fmt.Errorf("请求失败: URL %s, Method %s, Status Code %d, Status %s\n",
- requestUrl, http.MethodPost, resp.StatusCode(), resp.Status())
- }
- response := new(MergeUserResponse)
- err = json.Unmarshal(resp.Body(), response)
- if err != nil {
- return err
- }
- if !response.Success {
- return fmt.Errorf("请求失败: URL %s, Method: %s, Error %s\n",
- requestUrl, http.MethodPost, response.Msg)
- }
- return nil
- }
- func (api *API) getUsers(userName string, pageNo int, pageSize int) (*QueryUserResult, error) {
- requestUrl := path.Join(api.baseUrl, getUsersUrl)
- resp, err := api.httpClient.R().
- SetHeader("Content-Type", "application/json").
- SetQueryParam("userName", userName).
- SetQueryParam("pageNo", strconv.Itoa(pageNo)).
- SetQueryParam("pageSize", strconv.Itoa(pageSize)).
- Get(requestUrl)
- if err != nil {
- return nil, err
- }
- if resp.IsError() {
- return nil, fmt.Errorf("请求失败: URL %s, Method %s, Status Code %d, Status %s\n",
- requestUrl, http.MethodGet, resp.StatusCode(), resp.Status())
- }
- response := new(GetUsersResponse)
- err = json.Unmarshal(resp.Body(), response)
- if err != nil {
- return nil, err
- }
- if !response.Success {
- return nil, fmt.Errorf("请求失败: URL %s, Method: %s, Error %s\n",
- requestUrl, http.MethodGet, response.Msg)
- }
- return &response.QueryUserResult, nil
- }
- func (api *API) getMessages(topic string, startSendTime string, endSendTime string, pageNo int, pageSize int) (*QueryMessageResult, error) {
- requestUrl := path.Join(api.baseUrl, getMessagesUrl)
- resp, err := api.httpClient.R().
- SetHeader("Content-Type", "application/json").
- SetQueryParam("topic", topic).
- SetQueryParam("startSendTime", startSendTime).
- SetQueryParam("endSendTime", endSendTime).
- SetQueryParam("pageNo", strconv.Itoa(pageNo)).
- SetQueryParam("pageSize", strconv.Itoa(pageSize)).
- Get(requestUrl)
- if err != nil {
- return nil, err
- }
- if resp.IsError() {
- return nil, fmt.Errorf("请求失败: URL %s, Method %s, Status Code %d, Status %s\n",
- requestUrl, http.MethodGet, resp.StatusCode(), resp.Status())
- }
- response := new(GetMessagesResponse)
- err = json.Unmarshal(resp.Body(), response)
- if err != nil {
- return nil, err
- }
- if !response.Success {
- return nil, fmt.Errorf("请求失败: URL %s, Method: %s, Error %s\n",
- requestUrl, http.MethodGet, response.Msg)
- }
- return &response.QueryMessageResult, nil
- }
|