github.com/palcoin-project/palcd@v1.0.0/btcjson/example_test.go (about)

     1  // Copyright (c) 2014 The btcsuite developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package btcjson_test
     6  
     7  import (
     8  	"encoding/json"
     9  	"fmt"
    10  
    11  	"github.com/palcoin-project/palcd/btcjson"
    12  )
    13  
    14  // This example demonstrates how to create and marshal a command into a JSON-RPC
    15  // request.
    16  func ExampleMarshalCmd() {
    17  	// Create a new getblock command.  Notice the nil parameter indicates
    18  	// to use the default parameter for that fields.  This is a common
    19  	// pattern used in all of the New<Foo>Cmd functions in this package for
    20  	// optional fields.  Also, notice the call to btcjson.Bool which is a
    21  	// convenience function for creating a pointer out of a primitive for
    22  	// optional parameters.
    23  	blockHash := "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"
    24  	gbCmd := btcjson.NewGetBlockCmd(blockHash, btcjson.Int(0))
    25  
    26  	// Marshal the command to the format suitable for sending to the RPC
    27  	// server.  Typically the client would increment the id here which is
    28  	// request so the response can be identified.
    29  	id := 1
    30  	marshalledBytes, err := btcjson.MarshalCmd(btcjson.RpcVersion1, id, gbCmd)
    31  	if err != nil {
    32  		fmt.Println(err)
    33  		return
    34  	}
    35  
    36  	// Display the marshalled command.  Ordinarily this would be sent across
    37  	// the wire to the RPC server, but for this example, just display it.
    38  	fmt.Printf("%s\n", marshalledBytes)
    39  
    40  	// Output:
    41  	// {"jsonrpc":"1.0","method":"getblock","params":["000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",0],"id":1}
    42  }
    43  
    44  // This example demonstrates how to unmarshal a JSON-RPC request and then
    45  // unmarshal the concrete request into a concrete command.
    46  func ExampleUnmarshalCmd() {
    47  	// Ordinarily this would be read from the wire, but for this example,
    48  	// it is hard coded here for clarity.
    49  	data := []byte(`{"jsonrpc":"1.0","method":"getblock","params":["000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",0],"id":1}`)
    50  
    51  	// Unmarshal the raw bytes from the wire into a JSON-RPC request.
    52  	var request btcjson.Request
    53  	if err := json.Unmarshal(data, &request); err != nil {
    54  		fmt.Println(err)
    55  		return
    56  	}
    57  
    58  	// Typically there isn't any need to examine the request fields directly
    59  	// like this as the caller already knows what response to expect based
    60  	// on the command it sent.  However, this is done here to demonstrate
    61  	// why the unmarshal process is two steps.
    62  	if request.ID == nil {
    63  		fmt.Println("Unexpected notification")
    64  		return
    65  	}
    66  	if request.Method != "getblock" {
    67  		fmt.Println("Unexpected method")
    68  		return
    69  	}
    70  
    71  	// Unmarshal the request into a concrete command.
    72  	cmd, err := btcjson.UnmarshalCmd(&request)
    73  	if err != nil {
    74  		fmt.Println(err)
    75  		return
    76  	}
    77  
    78  	// Type assert the command to the appropriate type.
    79  	gbCmd, ok := cmd.(*btcjson.GetBlockCmd)
    80  	if !ok {
    81  		fmt.Printf("Incorrect command type: %T\n", cmd)
    82  		return
    83  	}
    84  
    85  	// Display the fields in the concrete command.
    86  	fmt.Println("Hash:", gbCmd.Hash)
    87  	fmt.Println("Verbosity:", *gbCmd.Verbosity)
    88  
    89  	// Output:
    90  	// Hash: 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
    91  	// Verbosity: 0
    92  }
    93  
    94  // This example demonstrates how to marshal a JSON-RPC response.
    95  func ExampleMarshalResponse() {
    96  	// Marshal a new JSON-RPC response.  For example, this is a response
    97  	// to a getblockheight request.
    98  	marshalledBytes, err := btcjson.MarshalResponse(btcjson.RpcVersion1, 1, 350001, nil)
    99  	if err != nil {
   100  		fmt.Println(err)
   101  		return
   102  	}
   103  
   104  	// Display the marshalled response.  Ordinarily this would be sent
   105  	// across the wire to the RPC client, but for this example, just display
   106  	// it.
   107  	fmt.Printf("%s\n", marshalledBytes)
   108  
   109  	// Output:
   110  	// {"jsonrpc":"1.0","result":350001,"error":null,"id":1}
   111  }
   112  
   113  // This example demonstrates how to unmarshal a JSON-RPC response and then
   114  // unmarshal the result field in the response to a concrete type.
   115  func Example_unmarshalResponse() {
   116  	// Ordinarily this would be read from the wire, but for this example,
   117  	// it is hard coded here for clarity.  This is an example response to a
   118  	// getblockheight request.
   119  	data := []byte(`{"jsonrpc":"1.0","result":350001,"error":null,"id":1}`)
   120  
   121  	// Unmarshal the raw bytes from the wire into a JSON-RPC response.
   122  	var response btcjson.Response
   123  	if err := json.Unmarshal(data, &response); err != nil {
   124  		fmt.Println("Malformed JSON-RPC response:", err)
   125  		return
   126  	}
   127  
   128  	// Check the response for an error from the server.  For example, the
   129  	// server might return an error if an invalid/unknown block hash is
   130  	// requested.
   131  	if response.Error != nil {
   132  		fmt.Println(response.Error)
   133  		return
   134  	}
   135  
   136  	// Unmarshal the result into the expected type for the response.
   137  	var blockHeight int32
   138  	if err := json.Unmarshal(response.Result, &blockHeight); err != nil {
   139  		fmt.Printf("Unexpected result type: %T\n", response.Result)
   140  		return
   141  	}
   142  	fmt.Println("Block height:", blockHeight)
   143  
   144  	// Output:
   145  	// Block height: 350001
   146  }