You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
2.6 KiB

  1. package jsonrpc
  2. import (
  3. "fmt"
  4. )
  5. func Example() {
  6. type Person struct {
  7. Name string `json:"name"`
  8. Age int `json:"age"`
  9. Country string `json:"country"`
  10. }
  11. // create client
  12. rpcClient := NewRPCClient("http://my-rpc-service")
  13. // execute rpc to service
  14. response, _ := rpcClient.Call("getPersonByID", 12345)
  15. // parse result into struct
  16. var person Person
  17. response.GetObject(&person)
  18. // change result and send back using rpc
  19. person.Age = 35
  20. rpcClient.Call("setPersonByID", 12345, person)
  21. }
  22. func ExampleRPCClient_Call() {
  23. rpcClient := NewRPCClient("http://my-rpc-service")
  24. // result processing omitted, see: RPCResponse methods
  25. rpcClient.Call("getTimestamp")
  26. rpcClient.Call("getPerson", 1234)
  27. rpcClient.Call("addNumbers", 5, 2, 3)
  28. rpcClient.Call("strangeFunction", 1, true, "alex", 3.4)
  29. type Person struct {
  30. Name string `json:"name"`
  31. Age int `json:"age"`
  32. Country string `json:"country"`
  33. }
  34. person := Person{
  35. Name: "alex",
  36. Age: 33,
  37. Country: "germany",
  38. }
  39. rpcClient.Call("setPersonByID", 123, person)
  40. }
  41. func ExampleRPCClient_CallNamed() {
  42. rpcClient := NewRPCClient("http://my-rpc-service")
  43. // result processing omitted, see: RPCResponse methods
  44. rpcClient.CallNamed("createPerson", map[string]interface{}{
  45. "name": "Bartholomew Allen",
  46. "nicknames": []string{"Barry", "Flash"},
  47. "male": true,
  48. "age": 28,
  49. "address": map[string]interface{}{"street": "Main Street", "city": "Central City"},
  50. })
  51. }
  52. func ExampleRPCResponse() {
  53. rpcClient := NewRPCClient("http://my-rpc-service")
  54. response, _ := rpcClient.Call("addNumbers", 1, 2, 3)
  55. sum, _ := response.GetInt()
  56. fmt.Println(sum)
  57. response, _ = rpcClient.Call("isValidEmail", "my@ema.il")
  58. valid, _ := response.GetBool()
  59. fmt.Println(valid)
  60. response, _ = rpcClient.Call("getJoke")
  61. joke, _ := response.GetString()
  62. fmt.Println(joke)
  63. response, _ = rpcClient.Call("getPi", 10)
  64. piRounded, _ := response.GetFloat64()
  65. fmt.Println(piRounded)
  66. var rndNumbers []int
  67. response, _ = rpcClient.Call("getRndIntNumbers", 10)
  68. response.GetObject(&rndNumbers)
  69. fmt.Println(rndNumbers[0])
  70. type Person struct {
  71. Name string `json:"name"`
  72. Age int `json:"age"`
  73. Country string `json:"country"`
  74. }
  75. var p Person
  76. response, _ = rpcClient.Call("getPersonByID", 1234)
  77. response.GetObject(&p)
  78. fmt.Println(p.Name)
  79. }
  80. func ExampleRPCClient_Batch() {
  81. rpcClient := NewRPCClient(httpServer.URL)
  82. req1 := rpcClient.NewRPCRequestObject("addNumbers", 1, 2, 3)
  83. req2 := rpcClient.NewRPCRequestObject("getTimestamp")
  84. responses, _ := rpcClient.Batch(req1, req2)
  85. response, _ := responses.GetResponseOf(req2)
  86. timestamp, _ := response.GetInt()
  87. fmt.Println(timestamp)
  88. }