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 3.3 kB

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