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

@ -37,7 +37,7 @@ type JSONRPCRequest struct {
ID string `json:"id"`
JSONRPC string `json:"jsonrpc"`
Method string `json:"method"`
Params []json.RawMessage `json:"params"`
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",

View File

@ -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)

View File

@ -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
}