- package api
- import(
- "io/ioutil"
- "net/http"
- "bytes"
- jsoniter "github.com/json-iterator/go"
- "fmt"
- "errors"
- "strings"
- )
- var http_url string = "http://10.10.1.20:8083"
- type Error struct {
- Code int `json:"code"`
- Message string `json:"message"`
- }
- type ApiError struct {
- Error_code int `json:"error_code"`
- Result bool `json:"result"`
- }
- //type ApiTicker
- func TestGetResParse(ret []byte,res interface{}) error{
- var resApi ApiResComm
- resApi.SetRes(res)
- str := string(ret[1:len(ret)-1]) //需要正则
- strUn := strings.Replace("{"res":" + str + "}","\","",-1)
- byteUn := []byte(strUn)
- fmt.Printf("byteUn: %s\n",byteUn)
-
- if err := jsoniter.Unmarshal(byteUn,&resApi); err != nil{
- fmt.Printf("parse error: %s\n",err.Error())
- return err
- }
- return nil
- }
- //================================================
- type HttpCmd struct {
- //CmdPost(string) (string,error)
- }
- func (c *HttpCmd)CmdPost(jsonCmd string)([]byte,error) {
- body := jsonCmd
- res, err := http.Post(http_url, "application/json;charset=utf-8", bytes.NewBuffer([]byte(body)))
- if err != nil {
- fmt.Println("http post error ", err.Error())
- return nil,err
- }
- defer res.Body.Close()
- content, err := ioutil.ReadAll(res.Body)
- if err != nil {
- fmt.Println("post, read body error ", err.Error())
- return nil,err
- }
- return content,nil
- }
- func (c *HttpCmd) CmdGet(getUrl string)([]byte,error) {
- resp, err := http.Get(getUrl)
- if err != nil {
- fmt.Println("http Get error ", err.Error())
- return nil,err
- }
-
- defer resp.Body.Close()
- content, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- fmt.Println("get, read body error ", err.Error())
- return nil,err
- }
-
- return content,nil
- }
- func (c *HttpCmd) CmdParse (res []byte,result interface{}) (ResultComm,error){
- var params ResultComm
- params.SetResult(result)
- if err := jsoniter.Unmarshal(res,¶ms); err != nil{
- return params, err
- }
- if params.GetError() != nil{
- errMsg := fmt.Sprintf("%v", params.GetError())
- return params, errors.New(errMsg)
- }
- return params, nil
- }
- func (c *HttpCmd) ApiResParse(ret []byte,res interface{})(int,error){
- var resApi ApiResComm
- var apiError ApiError
- resApi.SetRes(res)
- str := string(ret[1:len(ret)-1]) //需要正则
- strUn := strings.Replace("{"res":" + str + "}","\","",-1)
- strEt := strings.Replace(str,"\","",-1)
- if err := jsoniter.Unmarshal([]byte(strEt),&apiError); err != nil{
- fmt.Printf("api error parse error: %s\n",err.Error())
- return 0,err
- }
- byteUn := []byte(strUn)
- //fmt.Printf("byteUn: %s\n",byteUn)
- if apiError.Error_code != 0 {
- return apiError.Error_code,nil
- }
-
- if err := jsoniter.Unmarshal(byteUn,&resApi); err != nil{
- fmt.Printf("api result parse error: %s\n",err.Error())
- return 0,err
- }
-
- return 0,nil
- }
- //==================================================
- type CmdResult interface {
- GetError()*Error
- GetId()int
- GetResult()interface{}
- SetResult(interface{})
- }
- type ApiRes interface {
- GetRes()interface{}
- SetRes(interface{})
- }
- //////////////////////////////////////////////////////////////////////////////
- type CmdAddAsset struct {
- HttpCmd
- ResultComm
- }
- type AddAssetResul struct {
- }
- ///////////////////////////////////////////////////////////////////////////////////
- type ResultComm struct {
- Err *Error `json:"error"`
- Result interface{} `json:"result"`
- Id int `json:"id"`
- }
- func (r *ResultComm)GetError()*Error{
- return r.Err
- }
- func (r *ResultComm)GetId()int {
- return r.Id
- }
- func (r *ResultComm)GetResult()interface{}{
- return r.Result
- }
- func (r *ResultComm)SetResult(result interface{}){
- r.Result = result
- }
- ///////////////////////////////////////////////////////////////////////////////////
- type ApiResComm struct {
- Res interface{} `json:"res"`
- }
- func (r *ApiResComm)GetRes()interface{}{
- return r.Res
- }
- func (r *ApiResComm)SetRes(res interface{}){
- r.Res = res
- }
- ///////////////////////////////////////////////////////////////////////////////////
- type CmdAddMarket struct {
- HttpCmd
- ResultComm
- }
- //=======================================================
- type CmdMarketSummary struct {
- HttpCmd
- ResultComm
- }
- type MarketSummaryResult struct {
- Name string `json:"name"`
- Ask_count int `json:"ask_count"`
- Ask_amount string `json:"ask_amount"`
- Bid_count int `json:"bid_count"`
- bid_amount string `json:"bid_amount"`
- }
- ////////////////////////////////////////////////////////////////////////////////////////
- type CommCommand struct {
- HttpCmd
- ResultComm
- }
- type ApiCommand struct {
- HttpCmd
- ApiResComm
- }