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