From d64d139799cca647a291da9a8bfe785075d79341 Mon Sep 17 00:00:00 2001 From: Joshua Brigati Date: Wed, 26 Jun 2019 14:14:15 -0500 Subject: [PATCH 1/3] added byHeight file to call GetBlockByHeightRaw --- byHeight.go | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 byHeight.go diff --git a/byHeight.go b/byHeight.go new file mode 100644 index 0000000..2fe3656 --- /dev/null +++ b/byHeight.go @@ -0,0 +1,74 @@ +// Copyright 2016 Factom Foundation +// Use of this source code is governed by the MIT +// license that can be found in the LICENSE file. + +package factom + +import ( + "encoding/json" + "fmt" +) + + + +type JStruct struct { + data []byte +} + +func (e *JStruct) MarshalJSON() ([]byte, error) { + return e.data, nil +} + +func (e *JStruct) UnmarshalJSON(b []byte) error { + e.data = b + return nil +} + +type BlockByHeightRawResponse struct { + //TODO: implement all of the blocks as proper structures + + DBlock *JStruct `json:"dblock,omitempty"` + ABlock *JStruct `json:"ablock,omitempty"` + FBlock *JStruct `json:"fblock,omitempty"` + ECBlock *JStruct `json:"ecblock,omitempty"` + + RawData string `json:"rawdata,omitempty"` +} + +func (f *BlockByHeightRawResponse) String() string { + var s string + if f.DBlock != nil { + j, _ := f.DBlock.MarshalJSON() + s += fmt.Sprintln("DBlock:", string(j)) + } else if f.ABlock != nil { + j, _ := f.ABlock.MarshalJSON() + s += fmt.Sprintln("ABlock:", string(j)) + } else if f.FBlock != nil { + j, _ := f.FBlock.MarshalJSON() + s += fmt.Sprintln("FBlock:", string(j)) + } else if f.ECBlock != nil { + j, _ := f.ECBlock.MarshalJSON() + s += fmt.Sprintln("ECBlock:", string(j)) + } + + return s +} + +func GetBlockByHeightRaw(blockType string, height int64) (*BlockByHeightRawResponse, error) { + params := heightRequest{Height: height} + req := NewJSON2Request(fmt.Sprintf("%vblock-by-height", blockType), APICounter(), params) + resp, err := factomdRequest(req) + if err != nil { + return nil, err + } + if resp.Error != nil { + return nil, resp.Error + } + + block := new(BlockByHeightRawResponse) + if err := json.Unmarshal(resp.JSONResult(), block); err != nil { + return nil, err + } + + return block, nil +} From 9a365d3fcce135355897d4f759faca4f68ef44c4 Mon Sep 17 00:00:00 2001 From: Joshua Brigati Date: Fri, 28 Jun 2019 11:42:22 -0500 Subject: [PATCH 2/3] added Deprecation comments --- byHeight.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/byHeight.go b/byHeight.go index 2fe3656..f658e0f 100644 --- a/byHeight.go +++ b/byHeight.go @@ -1,3 +1,4 @@ +// BlockByHeightRawResponse returns the raw data from the api call. // Copyright 2016 Factom Foundation // Use of this source code is governed by the MIT // license that can be found in the LICENSE file. @@ -54,6 +55,7 @@ func (f *BlockByHeightRawResponse) String() string { return s } +// Deprecated: use ablock, dblock, eblock, ecblock and fblock instead. func GetBlockByHeightRaw(blockType string, height int64) (*BlockByHeightRawResponse, error) { params := heightRequest{Height: height} req := NewJSON2Request(fmt.Sprintf("%vblock-by-height", blockType), APICounter(), params) From 06dcca87be7f4da540493b113ec4c94e75f4c814 Mon Sep 17 00:00:00 2001 From: Joshua Brigati Date: Fri, 28 Jun 2019 12:59:00 -0500 Subject: [PATCH 3/3] fixed deprecation notices --- byHeight.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/byHeight.go b/byHeight.go index f658e0f..626d12c 100644 --- a/byHeight.go +++ b/byHeight.go @@ -1,4 +1,3 @@ -// BlockByHeightRawResponse returns the raw data from the api call. // Copyright 2016 Factom Foundation // Use of this source code is governed by the MIT // license that can be found in the LICENSE file. @@ -55,6 +54,7 @@ func (f *BlockByHeightRawResponse) String() string { return s } +// GetBlockByHeightRaw fetches the specified block type by height // Deprecated: use ablock, dblock, eblock, ecblock and fblock instead. func GetBlockByHeightRaw(blockType string, height int64) (*BlockByHeightRawResponse, error) { params := heightRequest{Height: height}