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.2 kB

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