useAPI.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package api_management
  2. import (
  3. "adm-management/errors"
  4. v1 "adm-management/pb/v1"
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "git.getensh.com/common/gopkgsv2/database"
  9. "git.getensh.com/common/gopkgsv2/logger"
  10. "go.uber.org/zap"
  11. "google.golang.org/grpc/status"
  12. "gorm.io/gorm"
  13. )
  14. type KVList struct {
  15. Key string `json:"key"`
  16. Value string `json:"value"`
  17. Desc string `json:"desc"`
  18. }
  19. func UseAPI(ctx context.Context, req *v1.UseAPIRequest) (reply *v1.UseAPIReply, err error) {
  20. defer func() {
  21. if r := recover(); r != nil {
  22. err = fmt.Errorf("%+v", r)
  23. e := &status.Status{}
  24. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  25. logger.Error("err",
  26. zap.String("system_err", err.Error()),
  27. zap.Stack("stacktrace"))
  28. }
  29. }
  30. }()
  31. reply = &v1.UseAPIReply{}
  32. if req.ApiId != 0 {
  33. db := database.DB()
  34. var api string
  35. err = db.Raw("SELECT request FROM t_adm_api_management where id = ?", req.ApiId).Find(&api).Error
  36. if err != nil {
  37. if err == gorm.ErrRecordNotFound {
  38. return reply, nil
  39. }
  40. return reply, errors.SystemError
  41. }
  42. var ReqMap = make(map[string]string)
  43. if api[2:6] == "list" {
  44. s := api[9 : len(api)-2]
  45. _ = json.Unmarshal([]byte(s), &ReqMap)
  46. for a, b := range ReqMap {
  47. reply.Request = append(reply.Request, &v1.CheckAPI{
  48. Key: a,
  49. Value: "",
  50. Desc: b,
  51. })
  52. }
  53. return reply, nil
  54. }
  55. _ = json.Unmarshal([]byte(api), &ReqMap)
  56. for k, v := range ReqMap {
  57. reply.Request = append(reply.Request, &v1.CheckAPI{
  58. Key: k,
  59. Value: "",
  60. Desc: v,
  61. })
  62. }
  63. return reply, nil
  64. }
  65. return reply, nil
  66. }