blockchain_api.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package api
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io"
  6. "net/http"
  7. )
  8. type BlockchainApi struct {
  9. baseUrl string
  10. }
  11. func NewBlockchainApi() *BlockchainApi {
  12. return &BlockchainApi{
  13. baseUrl: "http://web.410eth.com:8888",
  14. }
  15. }
  16. func HttpPost(url string, jsonData []byte) (string, error) {
  17. resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
  18. if err != nil {
  19. return "", err
  20. }
  21. defer resp.Body.Close()
  22. body, err := io.ReadAll(resp.Body)
  23. if err != nil {
  24. return "", err
  25. }
  26. return string(body), nil
  27. }
  28. func (a *BlockchainApi) HelloWorld(request HelloWorldRequest) (string, error) {
  29. url := a.baseUrl
  30. jsonData, _ := json.Marshal(request)
  31. return HttpPost(url, jsonData)
  32. }
  33. func (a *BlockchainApi) V2LpCountByChainId(request V2LpCountByChainIdRequest) (V2LpCountByChainIdResult, error) {
  34. url := a.baseUrl + "/v2-lp/countByChainId"
  35. jsonData, _ := json.Marshal(request)
  36. rstStr, err := HttpPost(url, jsonData)
  37. if err != nil {
  38. return V2LpCountByChainIdResult{}, err
  39. }
  40. result := V2LpCountByChainIdResult{
  41. Origin: rstStr,
  42. }
  43. json.Unmarshal([]byte(rstStr), &result)
  44. return result, err
  45. }