From e685d0316a0d862aecbc61d8bee7993e481831f6 Mon Sep 17 00:00:00 2001 From: Jeffrey Paul Date: Thu, 18 Oct 2018 01:39:41 -0700 Subject: [PATCH] prepping for refactor --- main.go | 12 ++++---- steemapi.go | 63 ++++++++++++++++++++++++++++++++++++++++ steemitapi.go | 62 --------------------------------------- steemtypes.go | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 149 insertions(+), 68 deletions(-) create mode 100644 steemapi.go delete mode 100644 steemitapi.go create mode 100644 steemtypes.go diff --git a/main.go b/main.go index 113662e..0320b61 100644 --- a/main.go +++ b/main.go @@ -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) } diff --git a/steemapi.go b/steemapi.go new file mode 100644 index 0000000..67fa992 --- /dev/null +++ b/steemapi.go @@ -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 +} diff --git a/steemitapi.go b/steemitapi.go deleted file mode 100644 index a3a84fe..0000000 --- a/steemitapi.go +++ /dev/null @@ -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 -} diff --git a/steemtypes.go b/steemtypes.go new file mode 100644 index 0000000..78d5dea --- /dev/null +++ b/steemtypes.go @@ -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) +}