OSDN Git Service

Merge pull request #41 from Bytom/dev
[bytom/vapor.git] / vendor / github.com / btcsuite / btcd / btcjson / example_test.go
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/btcsuite/btcd/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.Bool(false), nil)
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(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",false],"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",false],"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("Verbose:", *gbCmd.Verbose)
88         fmt.Println("VerboseTx:", *gbCmd.VerboseTx)
89
90         // Output:
91         // Hash: 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
92         // Verbose: false
93         // VerboseTx: false
94 }
95
96 // This example demonstrates how to marshal a JSON-RPC response.
97 func ExampleMarshalResponse() {
98         // Marshal a new JSON-RPC response.  For example, this is a response
99         // to a getblockheight request.
100         marshalledBytes, err := btcjson.MarshalResponse(1, 350001, nil)
101         if err != nil {
102                 fmt.Println(err)
103                 return
104         }
105
106         // Display the marshalled response.  Ordinarily this would be sent
107         // across the wire to the RPC client, but for this example, just display
108         // it.
109         fmt.Printf("%s\n", marshalledBytes)
110
111         // Output:
112         // {"result":350001,"error":null,"id":1}
113 }
114
115 // This example demonstrates how to unmarshal a JSON-RPC response and then
116 // unmarshal the result field in the response to a concrete type.
117 func Example_unmarshalResponse() {
118         // Ordinarily this would be read from the wire, but for this example,
119         // it is hard coded here for clarity.  This is an example response to a
120         // getblockheight request.
121         data := []byte(`{"result":350001,"error":null,"id":1}`)
122
123         // Unmarshal the raw bytes from the wire into a JSON-RPC response.
124         var response btcjson.Response
125         if err := json.Unmarshal(data, &response); err != nil {
126                 fmt.Println("Malformed JSON-RPC response:", err)
127                 return
128         }
129
130         // Check the response for an error from the server.  For example, the
131         // server might return an error if an invalid/unknown block hash is
132         // requested.
133         if response.Error != nil {
134                 fmt.Println(response.Error)
135                 return
136         }
137
138         // Unmarshal the result into the expected type for the response.
139         var blockHeight int32
140         if err := json.Unmarshal(response.Result, &blockHeight); err != nil {
141                 fmt.Printf("Unexpected result type: %T\n", response.Result)
142                 return
143         }
144         fmt.Println("Block height:", blockHeight)
145
146         // Output:
147         // Block height: 350001
148 }