bugly.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "strconv"
  7. "go-common/app/admin/ep/marthe/model"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. )
  11. const (
  12. _buglyOkCode = 200
  13. _buglyCookieExpiredCode = 100006
  14. _issueDetailCode = 100000
  15. _issueDetailList = "/v2/lastCrashInfo/appId/%s/platformId/%s/issues/%s/crashDataType/null?offsetTop=56&fsn=6d0260aa-331f-48b9-8557-c2aaf6e0be90"
  16. _issueList = "/v2/issueList?sortOrder=desc&sortField=uploadTime&rows=50&fsn=45cdb5aa-eb6f-4bda-9bba-ba0b264bfc93&appId=%s&platformId=%s&version=%s&start=%s&rows=%s&exceptionTypeList=%s"
  17. _issueVersionList = "/v2/getSelector/appId/%s/platformId/%s?types=version&fsn=8b8782b5-053d-4f58-bc17-d5c43d7f5ece"
  18. _issueExceptionList = "/v2/issueInfo/appId/%s/platformId/%s/issueId/%s/exceptionTypeList/Crash,Native,ExtensionCrash?fsn=114a8d02-586d-4fe4-8c23-79003fbe6882"
  19. )
  20. // BuglyVersion Bugly Version .
  21. func (d *Dao) BuglyVersion(c context.Context, buglyCookie *model.BuglyCookie, projectID, platformID string) (ret []*model.BugVersion, err error) {
  22. var (
  23. req *http.Request
  24. res *model.BugVersionResponse
  25. hostStr string
  26. )
  27. hostStr = d.c.Bugly.Host + fmt.Sprintf(_issueVersionList, projectID, platformID)
  28. if req, err = d.newRequest("GET", hostStr, nil); err != nil {
  29. return
  30. }
  31. req.Header.Set("Cookie", buglyCookie.Cookie)
  32. req.Header.Set("x-token", buglyCookie.Token)
  33. req.Header.Set("content-type", "application/json;charset=utf-8")
  34. req.Header.Set("x-csrf-token", "undefined")
  35. if err = d.httpClient.Do(c, req, &res); err != nil {
  36. log.Error("d.BugVersion url(%s) err(%v)", "BugVersion", err)
  37. return
  38. }
  39. if res.Status != _buglyOkCode {
  40. if res.Code == _buglyCookieExpiredCode {
  41. err = ecode.MartheCookieExpired
  42. log.Error("maybe need to update cookie and token")
  43. } else {
  44. err = ecode.MartheBuglyErr
  45. }
  46. log.Error("Status url(%s) res(%v) err(%v)", "BugVersion", res, err)
  47. return
  48. }
  49. ret = res.Ret.BugVersionList
  50. return
  51. }
  52. // BuglyIssueAndRetry Bugly Issue And Retry.
  53. func (d *Dao) BuglyIssueAndRetry(c context.Context, buglyCookie *model.BuglyCookie, bugIssueRequest *model.BugIssueRequest) (ret *model.BugRet, err error) {
  54. for i := 0; i < d.c.Bugly.UrlRetryCount; i++ {
  55. if ret, err = d.BuglyIssue(c, buglyCookie, bugIssueRequest); err == nil {
  56. break
  57. }
  58. }
  59. return
  60. }
  61. // BuglyIssue Get Issue.
  62. func (d *Dao) BuglyIssue(c context.Context, buglyCookie *model.BuglyCookie, bugIssueRequest *model.BugIssueRequest) (ret *model.BugRet, err error) {
  63. var (
  64. req *http.Request
  65. res *model.BugIssueResponse
  66. hostStr string
  67. )
  68. hostStr = d.c.Bugly.Host + fmt.Sprintf(_issueList, bugIssueRequest.ProjectID, bugIssueRequest.PlatformID, bugIssueRequest.Version, strconv.Itoa(bugIssueRequest.StartNum), strconv.Itoa(bugIssueRequest.Rows), bugIssueRequest.ExceptionType)
  69. if req, err = d.newRequest("GET", hostStr, nil); err != nil {
  70. return
  71. }
  72. req.Header.Set("Cookie", buglyCookie.Cookie)
  73. req.Header.Set("x-token", buglyCookie.Token)
  74. req.Header.Set("content-type", "application/json;charset=utf-8")
  75. req.Header.Set("x-csrf-token", "undefined")
  76. if err = d.httpClient.Do(c, req, &res); err != nil {
  77. log.Error("d.BuglyIssue url(%s) err(%v)", "BuglyIssue", err)
  78. return
  79. }
  80. if res.Status != _buglyOkCode {
  81. if res.Code == _buglyCookieExpiredCode {
  82. err = ecode.MartheCookieExpired
  83. log.Error("maybe need to update cookie and token")
  84. } else {
  85. err = ecode.MartheBuglyErr
  86. }
  87. log.Error("Status url(%s) res(%v) err(%v)", "BuglyIssue", res, err)
  88. return
  89. }
  90. ret = res.Ret
  91. return
  92. }
  93. // BuglyIssueDetailAndRetry Bugly Issue Detail And Retry.
  94. func (d *Dao) BuglyIssueDetailAndRetry(c context.Context, buglyCookie *model.BuglyCookie, projectID, platformID, issueNo string) (bugIssueDetail *model.BugIssueDetail, err error) {
  95. for i := 0; i < d.c.Bugly.UrlRetryCount; i++ {
  96. if bugIssueDetail, err = d.BuglyIssueDetail(c, buglyCookie, projectID, platformID, issueNo); err == nil {
  97. break
  98. }
  99. }
  100. return
  101. }
  102. // BuglyIssueDetail Get Issue Detail.
  103. func (d *Dao) BuglyIssueDetail(c context.Context, buglyCookie *model.BuglyCookie, projectID, platformID, issueNo string) (bugIssueDetail *model.BugIssueDetail, err error) {
  104. var (
  105. req *http.Request
  106. res *model.BugIssueDetailResponse
  107. hostStr string
  108. )
  109. hostStr = d.c.Bugly.Host + fmt.Sprintf(_issueDetailList, projectID, platformID, issueNo)
  110. if req, err = d.newRequest("GET", hostStr, nil); err != nil {
  111. return
  112. }
  113. req.Header.Set("Cookie", buglyCookie.Cookie)
  114. req.Header.Set("x-token", buglyCookie.Token)
  115. req.Header.Set("content-type", "application/json;charset=utf-8")
  116. req.Header.Set("x-csrf-token", "undefined")
  117. if err = d.httpClient.Do(c, req, &res); err != nil {
  118. log.Error("d.BuglyIssue url(%s) err(%v)", "BuglyIssue", err)
  119. return
  120. }
  121. if res.Code != _issueDetailCode {
  122. err = ecode.MartheBuglyErr
  123. log.Error("Status url(%s) res(%v) err(%v)", "BuglyIssue", res, err)
  124. return
  125. }
  126. bugIssueDetail = res.Data
  127. return
  128. }
  129. // BuglyIssueExceptionList Bugly Issue Exception List.
  130. func (d *Dao) BuglyIssueExceptionList(c context.Context, buglyCookie *model.BuglyCookie, projectID, platformID, issueNo string) (bugIssueException *model.IssueException, err error) {
  131. var (
  132. req *http.Request
  133. res *model.BugIssueExceptionListResponse
  134. hostStr string
  135. )
  136. hostStr = d.c.Bugly.Host + fmt.Sprintf(_issueExceptionList, projectID, platformID, issueNo)
  137. if req, err = d.newRequest("GET", hostStr, nil); err != nil {
  138. return
  139. }
  140. req.Header.Set("Cookie", buglyCookie.Cookie)
  141. req.Header.Set("x-token", buglyCookie.Token)
  142. req.Header.Set("content-type", "application/json;charset=utf-8")
  143. req.Header.Set("x-csrf-token", "undefined")
  144. if err = d.httpClient.Do(c, req, &res); err != nil {
  145. log.Error("d.BuglyIssueExceptionList url(%s) err(%v)", "BuglyIssueExceptionList", err)
  146. return
  147. }
  148. if res.Status != _buglyOkCode {
  149. err = ecode.MartheBuglyErr
  150. log.Error("Status url(%s) res(%v) err(%v)", "BuglyIssueExceptionList", res, err)
  151. return
  152. }
  153. if res.Ret != nil && len(res.Ret.IssueException) != 0 && res.Ret.IssueException[0].IssueID == issueNo {
  154. bugIssueException = res.Ret.IssueException[0]
  155. }
  156. return
  157. }