basic fetch working

This commit is contained in:
Jeffrey Paul 2018-10-18 00:09:30 -07:00
parent 74f68b0e00
commit f0b0786ef8
Signed by: sneak
GPG Key ID: 052443F4DF2A55C2
3 changed files with 54 additions and 25 deletions

View File

@ -34,10 +34,10 @@ type JSONRPCResponse struct {
} }
type JSONRPCRequest struct { type JSONRPCRequest struct {
ID string `json:"id"` ID string `json:"id"`
JSONRPC string `json:"jsonrpc"` JSONRPC string `json:"jsonrpc"`
Method string `json:"method"` Method string `json:"method"`
Params []json.RawMessage `json:"params"` Params json.RawMessage `json:"params"`
} }
type JSONRPC struct { type JSONRPC struct {
@ -62,7 +62,7 @@ func NewJSONRPC(url string, options ...func(rpc *JSONRPC)) *JSONRPC {
} }
// Call returns raw response of method call // 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{ request := JSONRPCRequest{
ID: "1", ID: "1",
JSONRPC: "2.0", JSONRPC: "2.0",

View File

@ -6,11 +6,12 @@ import (
"log" "log"
) )
func main() { const steemApiUrl = "https://api.steemit.com"
c := JSONRPCClient("https://api.steemit.com", func(x *JSONRPC) { x.Debug = true })
r, err := c.GetVirtualOpsInBlock(20000000) func main() {
//r, err := c.GetOpsInBlock(1) s := NewSteemAPI(steemApiUrl)
r, err := s.GetOpsInBlock(20000000, true)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)

View File

@ -1,5 +1,6 @@
package main package main
import log "github.com/sirupsen/logrus"
import "encoding/json" import "encoding/json"
import "github.com/joeshaw/iso8601" import "github.com/joeshaw/iso8601"
@ -13,22 +14,49 @@ type SteemVirtualTransaction struct {
Op []json.RawMessage `json:"op"` Op []json.RawMessage `json:"op"`
} }
func (rpc *JSONRPC) GetVirtualOpsInBlock(blockNum int) ([]*SteemVirtualTransaction, error) { type SteemAPI struct {
raw, err1 := rpc.Call("condenser_api.get_ops_in_block", blockNum, true) url string
if err1 != nil { rpc *JSONRPC
return nil, err1 Debug bool
} log *log.Logger
tmp := make([]SteemVirtualTransaction)
//var result []interface{}
err2 := json.Unmarshal(raw, &tmp)
if err2 != nil {
return result, nil
}
return nil, err2
} }
func (rpc *JSONRPC) GetOpsInBlock(blockNum int) (json.RawMessage, error) { func NewSteemAPI(url string, options ...func(s *SteemAPI)) *SteemAPI {
r, err := rpc.Call("condenser_api.get_ops_in_block", blockNum, false)
return r, err 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
} }