diff --git a/jsonrpc.go b/jsonrpc.go index 4e86fa8..5faf6fa 100644 --- a/jsonrpc.go +++ b/jsonrpc.go @@ -34,10 +34,10 @@ type JSONRPCResponse struct { } type JSONRPCRequest struct { - ID string `json:"id"` - JSONRPC string `json:"jsonrpc"` - Method string `json:"method"` - Params []json.RawMessage `json:"params"` + ID string `json:"id"` + JSONRPC string `json:"jsonrpc"` + Method string `json:"method"` + Params json.RawMessage `json:"params"` } type JSONRPC struct { @@ -62,7 +62,7 @@ func NewJSONRPC(url string, options ...func(rpc *JSONRPC)) *JSONRPC { } // Call returns raw response of method call -func (rpc *JSONRPC) Call(method string, params ...json.RawMessage) (json.RawMessage, error) { +func (rpc *JSONRPC) Call(method string, params json.RawMessage) (json.RawMessage, error) { request := JSONRPCRequest{ ID: "1", JSONRPC: "2.0", diff --git a/main.go b/main.go index 5e01ded..113662e 100644 --- a/main.go +++ b/main.go @@ -6,11 +6,12 @@ import ( "log" ) -func main() { - c := JSONRPCClient("https://api.steemit.com", func(x *JSONRPC) { x.Debug = true }) +const steemApiUrl = "https://api.steemit.com" - r, err := c.GetVirtualOpsInBlock(20000000) - //r, err := c.GetOpsInBlock(1) +func main() { + s := NewSteemAPI(steemApiUrl) + + r, err := s.GetOpsInBlock(20000000, true) if err != nil { log.Fatal(err) diff --git a/steemitapi.go b/steemitapi.go index a83e711..a3a84fe 100644 --- a/steemitapi.go +++ b/steemitapi.go @@ -1,5 +1,6 @@ package main +import log "github.com/sirupsen/logrus" import "encoding/json" import "github.com/joeshaw/iso8601" @@ -13,22 +14,49 @@ type SteemVirtualTransaction struct { Op []json.RawMessage `json:"op"` } -func (rpc *JSONRPC) GetVirtualOpsInBlock(blockNum int) ([]*SteemVirtualTransaction, error) { - raw, err1 := rpc.Call("condenser_api.get_ops_in_block", blockNum, true) - if err1 != nil { - return nil, err1 - } - - tmp := make([]SteemVirtualTransaction) - //var result []interface{} - err2 := json.Unmarshal(raw, &tmp) - if err2 != nil { - return result, nil - } - return nil, err2 +type SteemAPI struct { + url string + rpc *JSONRPC + Debug bool + log *log.Logger } -func (rpc *JSONRPC) GetOpsInBlock(blockNum int) (json.RawMessage, error) { - r, err := rpc.Call("condenser_api.get_ops_in_block", blockNum, false) - return r, err +func NewSteemAPI(url string, options ...func(s *SteemAPI)) *SteemAPI { + + rpc := NewJSONRPC(url, func(x *JSONRPC) { x.Debug = true }) + + self := &SteemAPI{ + rpc: rpc, + } + + self.log = log.New() + + for _, option := range options { + option(self) + } + + return self +} + +type GetOpsInBlockRequestParams struct { + BlockNum int + VirtualOps bool +} + +func (r *GetOpsInBlockRequestParams) MarshalJSON() ([]byte, error) { + arr := []interface{}{r.BlockNum, r.VirtualOps} + return json.Marshal(arr) +} + +func (self *SteemAPI) GetOpsInBlock(blockNum int, virtualOps bool) (json.RawMessage, error) { + p := &GetOpsInBlockRequestParams{BlockNum: blockNum, VirtualOps: virtualOps} + j, err := p.MarshalJSON() + if err != nil { + return nil, err + } + r, err := self.rpc.Call("condenser_api.get_ops_in_block", j) + if err != nil { + return nil, err + } + return r, nil }