From 40ffe2651b53cc864a4659be4d7e24ffea928124 Mon Sep 17 00:00:00 2001 From: oysheng Date: Fri, 20 Apr 2018 16:47:05 +0800 Subject: [PATCH] modify request for json --- api/block_retrieve.go | 22 ++++++++++++++-------- cmd/bytomcli/commands/block.go | 20 ++++++++++++++++---- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/api/block_retrieve.go b/api/block_retrieve.go index 33c4a1f0..e033b854 100644 --- a/api/block_retrieve.go +++ b/api/block_retrieve.go @@ -17,9 +17,11 @@ func (a *API) getBestBlockHash() Response { } // return block header by hash -func (a *API) getBlockHeaderByHash(strHash string) Response { +func (a *API) getBlockHeaderByHash(req struct { + BlockHash string `json:"block_hash"` +}) Response { hash := bc.Hash{} - if err := hash.UnmarshalText([]byte(strHash)); err != nil { + if err := hash.UnmarshalText([]byte(req.BlockHash)); err != nil { log.WithField("error", err).Error("Error occurs when transforming string hash to hash struct") return NewErrorResponse(err) } @@ -130,9 +132,11 @@ func (a *API) getBlock(ins GetBlockReq) Response { } // return block transactions count by hash -func (a *API) getBlockTransactionsCountByHash(strHash string) Response { +func (a *API) getBlockTransactionsCountByHash(req struct { + BlockHash string `json:"block_hash"` +}) Response { hash := bc.Hash{} - if err := hash.UnmarshalText([]byte(strHash)); err != nil { + if err := hash.UnmarshalText([]byte(req.BlockHash)); err != nil { log.WithField("error", err).Error("Error occurs when transforming string hash to hash struct") return NewErrorResponse(err) } @@ -143,19 +147,21 @@ func (a *API) getBlockTransactionsCountByHash(strHash string) Response { return NewErrorResponse(err) } - count := map[string]int{"count": len(legacyBlock.Transactions)} + count := map[string]int{"tx_count": len(legacyBlock.Transactions)} return NewSuccessResponse(count) } // return block transactions count by height -func (a *API) getBlockTransactionsCountByHeight(height uint64) Response { - legacyBlock, err := a.chain.GetBlockByHeight(height) +func (a *API) getBlockTransactionsCountByHeight(req struct { + Height uint64 `json:"block_height"` +}) Response { + legacyBlock, err := a.chain.GetBlockByHeight(req.Height) if err != nil { log.WithField("error", err).Error("Fail to get block by hash") return NewErrorResponse(err) } - count := map[string]int{"count": len(legacyBlock.Transactions)} + count := map[string]int{"tx_count": len(legacyBlock.Transactions)} return NewSuccessResponse(count) } diff --git a/cmd/bytomcli/commands/block.go b/cmd/bytomcli/commands/block.go index 3fa04925..1ae68290 100644 --- a/cmd/bytomcli/commands/block.go +++ b/cmd/bytomcli/commands/block.go @@ -98,7 +98,11 @@ var getBlockHeaderByHashCmd = &cobra.Command{ Short: "Get the header of a block matching the given hash", Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { - data, exitCode := util.ClientCall("/get-block-header-by-hash", args[0]) + var req = struct { + BlockHash string `json:"block_hash"` + }{BlockHash: args[0]} + + data, exitCode := util.ClientCall("/get-block-header-by-hash", &req) if exitCode != util.Success { os.Exit(exitCode) } @@ -111,7 +115,11 @@ var getBlockTransactionsCountByHashCmd = &cobra.Command{ Short: "Get the transactions count of a block matching the given hash", Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { - data, exitCode := util.ClientCall("/get-block-transactions-count-by-hash", args[0]) + var req = struct { + BlockHash string `json:"block_hash"` + }{BlockHash: args[0]} + + data, exitCode := util.ClientCall("/get-block-transactions-count-by-hash", &req) if exitCode != util.Success { os.Exit(exitCode) } @@ -124,13 +132,17 @@ var getBlockTransactionsCountByHeightCmd = &cobra.Command{ Short: "Get the transactions count of a block matching the given height", Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { - ui64, err := strconv.ParseUint(args[0], 10, 64) + height, err := strconv.ParseUint(args[0], 10, 64) if err != nil { jww.ERROR.Printf("Invalid height value") os.Exit(util.ErrLocalExe) } - data, exitCode := util.ClientCall("/get-block-transactions-count-by-height", ui64) + var req = struct { + Height uint64 `json:"block_height"` + }{Height: height} + + data, exitCode := util.ClientCall("/get-block-transactions-count-by-height", &req) if exitCode != util.Success { os.Exit(exitCode) } -- 2.11.0