prepping for refactor

This commit is contained in:
Jeffrey Paul 2018-10-18 01:39:41 -07:00
parent f0b0786ef8
commit e685d0316a
Signed by: sneak
GPG Key ID: 052443F4DF2A55C2
4 changed files with 149 additions and 68 deletions

12
main.go
View File

@ -1,22 +1,22 @@
package main
import (
"fmt"
"github.com/davecgh/go-spew/spew"
"log"
log "github.com/sirupsen/logrus"
//"fmt"
)
const steemApiUrl = "https://api.steemit.com"
const steemAPIURL = "https://api.steemit.com"
func main() {
s := NewSteemAPI(steemApiUrl)
s := NewSteemAPI(steemAPIURL)
r, err := s.GetOpsInBlock(20000000, true)
r, err := s.GetOpsInBlock(20000000)
if err != nil {
log.Fatal(err)
}
//fmt.Println(r)
spew.Dump(r)
fmt.Println(r)
}

63
steemapi.go Normal file
View File

@ -0,0 +1,63 @@
package main
import (
log "github.com/sirupsen/logrus"
//"fmt"
"encoding/json"
)
//import "github.com/davecgh/go-spew/spew"
type SteemAPI struct {
url string
rpc *JSONRPC
Debug bool
log *log.Logger
}
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
}
func (self *SteemAPI) GetOpsInBlock(blockNum int) (GetOpsInBlockResponse, error) {
// first fetch virtual ops
vOpsParams := &GetOpsInBlockRequestParams{BlockNum: blockNum, VirtualOps: true}
vop, err := vOpsParams.MarshalJSON()
vOpsResponse, err := self.rpc.Call("condenser_api.get_ops_in_block", vop)
if err != nil {
return nil, err
}
var result []OperationObject
err = json.Unmarshal(vOpsResponse, &result)
if err != nil {
return nil, err
}
// result is now populated with vops, now get real ops
realOpsParams := &GetOpsInBlockRequestParams{BlockNum: blockNum, VirtualOps: false}
rop, err := realOpsParams.MarshalJSON()
realOpsResponse, err := self.rpc.Call("condenser_api.get_ops_in_block", rop)
var secondResult []OperationObject
err = json.Unmarshal(realOpsResponse, &secondResult)
if err != nil {
return nil, err
}
result = append(result, secondResult...)
return result, nil
}

View File

@ -1,62 +0,0 @@
package main
import log "github.com/sirupsen/logrus"
import "encoding/json"
import "github.com/joeshaw/iso8601"
type SteemVirtualTransaction struct {
TxID string `json:"trx_id"`
BlockNum uint64 `json:"block"`
TxInBlock uint64 `json:"trx_in_block"`
OpInTx int `json:"op_in_trx"`
IsVirtual int `json:"virtual_op"`
Timestamp iso8601.Time `json:"timestamp"`
Op []json.RawMessage `json:"op"`
}
type SteemAPI struct {
url string
rpc *JSONRPC
Debug bool
log *log.Logger
}
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
}

80
steemtypes.go Normal file
View File

@ -0,0 +1,80 @@
package main
import "encoding/json"
import "github.com/joeshaw/iso8601"
// TODO(sneak)
//func (r *SteemOpInBlock) UnmarshalJSON() ([]byte, error) {
//}
type OperationObject struct {
TransactionID string `json:"trx_id"`
BlockNumber uint64 `json:"block"`
TransactionInBlock uint64 `json:"trx_in_block"`
OpInTx int `json:"op_in_trx"`
VirtualOperation int `json:"virtual_op"`
Timestamp iso8601.Time `json:"timestamp"`
Operation []json.RawMessage `json:"op"`
}
/*
type operationTuple struct {
Type OpType
Data Operation
}
func (op *operationTuple) MarshalJSON() ([]byte, error) {
return json.Marshal([]interface{}{
op.Type,
op.Data,
})
}
func (op *operationTuple) UnmarshalJSON(data []byte) error {
// The operation object is [opType, opBody].
raw := make([]*json.RawMessage, 2)
if err := json.Unmarshal(data, &raw); err != nil {
return errors.Wrapf(err, "failed to unmarshal operation object: %v", string(data))
}
if len(raw) != 2 {
return errors.Errorf("invalid operation object: %v", string(data))
}
// Unmarshal the type.
var opType OpType
if err := json.Unmarshal(*raw[0], &opType); err != nil {
return errors.Wrapf(err, "failed to unmarshal Operation.Type: %v", string(*raw[0]))
}
// Unmarshal the data.
var opData Operation
template, ok := dataObjects[opType]
if ok {
opData = reflect.New(
reflect.Indirect(reflect.ValueOf(template)).Type(),
).Interface().(Operation)
if err := json.Unmarshal(*raw[1], opData); err != nil {
return errors.Wrapf(err, "failed to unmarshal Operation.Data: %v", string(*raw[1]))
}
} else {
opData = &UnknownOperation{opType, raw[1]}
}
// Update fields.
op.Type = opType
op.Data = opData
return nil
}
*/
type GetOpsInBlockRequestParams struct {
BlockNum int
VirtualOps bool
}
type GetOpsInBlockResponse []OperationObject
func (r *GetOpsInBlockRequestParams) MarshalJSON() ([]byte, error) {
arr := []interface{}{r.BlockNum, r.VirtualOps}
return json.Marshal(arr)
}