|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- package blockchain
-
- import (
- "fmt"
-
- "code.gitea.io/gitea/modules/setting"
- "github.com/go-resty/resty/v2"
- )
-
- var (
- restyClient *resty.Client
- )
-
- const (
- UrlCreateAccount = "createAccount"
- UrlGetBalance = "getBalance"
- UrlNewRepo = "newRepo"
- UrlContribute = "contribute"
-
- ActionCommit = "commit"
-
- Success = 0
- )
-
- type CreateAccountResult struct {
- Code int `json:"code"`
- Msg string `json:"message"`
- Payload map[string]interface{} `json:"data"`
- }
-
- type GetBalanceResult struct {
- Code int `json:"code"`
- Msg string `json:"message"`
- Payload map[string]interface{} `json:"data"`
- }
-
- type NewRepoResult struct {
- Code int `json:"code"`
- Msg string `json:"message"`
- //Data string `json:"data"`
- }
-
- type ContributeResult struct {
- Code int `json:"code"`
- Msg string `json:"message"`
- Payload map[string]interface{} `json:"data"`
- }
-
- func getRestyClient() *resty.Client {
- if restyClient == nil {
- restyClient = resty.New()
- }
- return restyClient
- }
-
- func CreateBlockchainAccount() (*CreateAccountResult, error) {
- client := getRestyClient()
- var result CreateAccountResult
-
- res, err := client.R().
- SetHeader("Content-Type", "application/json").
- SetResult(&result).
- Get(setting.BlockChainHost + UrlCreateAccount)
-
- if err != nil {
- return nil, fmt.Errorf("resty create account: %s", err)
- }
-
- if result.Code != Success {
- return &result, fmt.Errorf("CreateAccount err: %s", res.String())
- }
-
- return &result, nil
- }
-
- func NewRepo(repoID, publicKey, repoName string) (*NewRepoResult, error) {
- client := getRestyClient()
- var result NewRepoResult
-
- res, err := client.R().
- SetHeader("Accept", "application/json").
- SetQueryParams(map[string]string{
- "repoId" : repoID,
- "creator" : publicKey,
- "repoName" : repoName,
- }).
- SetResult(&result).
- Get(setting.BlockChainHost + UrlNewRepo)
-
- if err != nil {
- return nil, fmt.Errorf("resty newRepo: %v", err)
- }
-
- if result.Code != Success {
- return &result, fmt.Errorf("newRepo err: %s", res.String())
- }
-
- return &result, nil
- }
-
- func GetBalance(contractAddress, contributor string) (*GetBalanceResult, error) {
- client := getRestyClient()
- var result GetBalanceResult
-
- res, err := client.R().
- SetHeader("Accept", "application/json").
- SetQueryParams(map[string]string{
- "contractAddress" : contractAddress,
- "contributor" : contributor,
- }).
- SetResult(&result).
- Get(setting.BlockChainHost + UrlGetBalance)
-
- if err != nil {
- return nil, fmt.Errorf("resty getBalance: %v", err)
- }
-
- if result.Code != Success {
- return &result, fmt.Errorf("getBalance err: %s", res.String())
- }
-
- return &result, nil
- }
-
- func Contribute(contractAddress, contributor, action, commitId string, codeLine int) (*ContributeResult, error) {
- client := getRestyClient()
- var result ContributeResult
-
- res, err := client.R().
- SetHeader("Accept", "application/json").
- SetQueryParams(map[string]string{
- "contractAddress" : contractAddress,
- "contributor" : contributor,
- "action" : action,
- "commitId": commitId,
- "amount": string(codeLine),
- }).
- SetResult(&result).
- Get(setting.BlockChainHost + UrlContribute)
-
- if err != nil {
- return nil, fmt.Errorf("resty contribute: %v", err)
- }
-
- if result.Code != Success {
- return &result, fmt.Errorf("contribute err: %s", res.String())
- }
-
- return &result, nil
- }
|