You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

resty.go 4.3 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package blockchain
  2. import (
  3. "fmt"
  4. "strconv"
  5. "code.gitea.io/gitea/modules/setting"
  6. "github.com/go-resty/resty/v2"
  7. )
  8. var (
  9. restyClient *resty.Client
  10. )
  11. const (
  12. UrlCreateAccount = "createAccount"
  13. UrlGetBalance = "getBalance"
  14. UrlNewRepo = "newRepo"
  15. UrlContribute = "contribute"
  16. UrlSetIssue = "setIssue"
  17. Success = 0
  18. )
  19. type CreateAccountResult struct {
  20. Code int `json:"code"`
  21. Msg string `json:"message"`
  22. Payload map[string]interface{} `json:"data"`
  23. }
  24. type GetBalanceResult struct {
  25. Code int `json:"code"`
  26. Msg string `json:"message"`
  27. Data string `json:"data"`
  28. }
  29. type NewRepoResult struct {
  30. Code int `json:"code"`
  31. Msg string `json:"message"`
  32. //Data string `json:"data"`
  33. }
  34. type ContributeResult struct {
  35. Code int `json:"code"`
  36. Msg string `json:"message"`
  37. //Payload map[string]interface{} `json:"data"`
  38. }
  39. type SetIssueResult struct {
  40. Code int `json:"code"`
  41. Msg string `json:"message"`
  42. //Data string `json:"data"`
  43. }
  44. func getRestyClient() *resty.Client {
  45. if restyClient == nil {
  46. restyClient = resty.New()
  47. }
  48. return restyClient
  49. }
  50. func CreateBlockchainAccount() (*CreateAccountResult, error) {
  51. client := getRestyClient()
  52. var result CreateAccountResult
  53. res, err := client.R().
  54. SetHeader("Content-Type", "application/json").
  55. SetResult(&result).
  56. Get(setting.BlockChainHost + UrlCreateAccount)
  57. if err != nil {
  58. return nil, fmt.Errorf("resty create account: %s", err)
  59. }
  60. if result.Code != Success {
  61. return &result, fmt.Errorf("CreateAccount err: %s", res.String())
  62. }
  63. return &result, nil
  64. }
  65. func NewRepo(repoID, publicKey, repoName string) (*NewRepoResult, error) {
  66. client := getRestyClient()
  67. var result NewRepoResult
  68. res, err := client.R().
  69. SetHeader("Accept", "application/json").
  70. SetQueryParams(map[string]string{
  71. "repoId": repoID,
  72. "creator": publicKey,
  73. "repoName": repoName,
  74. }).
  75. SetResult(&result).
  76. Get(setting.BlockChainHost + UrlNewRepo)
  77. if err != nil {
  78. return nil, fmt.Errorf("resty newRepo: %v", err)
  79. }
  80. if result.Code != Success {
  81. return &result, fmt.Errorf("newRepo err: %s", res.String())
  82. }
  83. return &result, nil
  84. }
  85. func GetBalance(contractAddress, contributor string) (*GetBalanceResult, error) {
  86. client := getRestyClient()
  87. var result GetBalanceResult
  88. res, err := client.R().
  89. SetHeader("Accept", "application/json").
  90. SetQueryParams(map[string]string{
  91. "contractAddress": contractAddress,
  92. "contributor": contributor,
  93. }).
  94. SetResult(&result).
  95. Get(setting.BlockChainHost + UrlGetBalance)
  96. if err != nil {
  97. return nil, fmt.Errorf("resty getBalance: %v", err)
  98. }
  99. if result.Code != Success {
  100. return &result, fmt.Errorf("getBalance err: %s", res.String())
  101. }
  102. return &result, nil
  103. }
  104. func Contribute(contractAddress, contributor, commitId string, amount int64) (*ContributeResult, error) {
  105. client := getRestyClient()
  106. var result ContributeResult
  107. strAmount := strconv.FormatInt(amount, 10)
  108. res, err := client.R().
  109. SetHeader("Accept", "application/json").
  110. SetQueryParams(map[string]string{
  111. "contractAddress": contractAddress,
  112. "contributor": contributor,
  113. "commitId": commitId,
  114. "amount": strAmount,
  115. }).
  116. SetResult(&result).
  117. Get(setting.BlockChainHost + UrlContribute)
  118. if err != nil {
  119. return nil, fmt.Errorf("resty contribute: %v", err)
  120. }
  121. if result.Code != Success {
  122. return &result, fmt.Errorf("contribute err: %s", res.String())
  123. }
  124. return &result, nil
  125. }
  126. func SetIssue(contractAddress, contributor string, issueId int64, amount int64) (*SetIssueResult, error) {
  127. client := getRestyClient()
  128. var result SetIssueResult
  129. strAmount := strconv.FormatInt(amount, 10)
  130. strIssue := strconv.FormatInt(issueId, 10)
  131. res, err := client.R().
  132. SetHeader("Accept", "application/json").
  133. SetQueryParams(map[string]string{
  134. "contractAddress": contractAddress,
  135. "contributor": contributor,
  136. "issueId": strIssue,
  137. "amount": strAmount,
  138. }).
  139. SetResult(&result).
  140. Get(setting.BlockChainHost + UrlSetIssue)
  141. if err != nil {
  142. return nil, fmt.Errorf("resty SetIssue: %v", err)
  143. }
  144. if result.Code != Success {
  145. return &result, fmt.Errorf("SetIssue err: %s", res.String())
  146. }
  147. return &result, nil
  148. }