github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/client/api/README.md (about) 1 # Micro API Client [](https://godoc.org/github.com/tickoalcantara12/micro/v3/client) 2 3 This is the Go client to access services on the Micro Platform 4 5 ## Usage 6 7 ```go 8 package main 9 10 import ( 11 "fmt" 12 "os" 13 14 "github.com/tickoalcantara12/micro/v3/client/api" 15 ) 16 17 type Request struct { 18 Name string `json:"name"` 19 } 20 21 type Response struct { 22 Msg string `json:"msg"` 23 } 24 25 var ( 26 token = os.Getenv("TOKEN") 27 ) 28 29 func main() { 30 c := api.NewClient(nil) 31 32 // set your api token 33 c.SetToken(token) 34 35 req := &Request{ 36 Name: "John", 37 } 38 39 var rsp Response 40 41 if err := c.Call("helloworld", "call", req, &rsp); err != nil { 42 fmt.Println(err) 43 return 44 } 45 46 fmt.Println(rsp) 47 } 48 ``` 49 50 ## Streaming 51 52 The client supports streaming 53 54 ```go 55 package main 56 57 import ( 58 "fmt" 59 60 "github.com/tickoalcantara12/micro/v3/client/api" 61 ) 62 63 type Request struct { 64 Count string `json:"count"` 65 } 66 67 type Response struct { 68 Count string `json:"count"` 69 } 70 71 var ( 72 token, _ = os.Getenv("TOKEN") 73 ) 74 75 func main() { 76 c := api.NewClient(nil) 77 78 // set your api token 79 c.SetToken(token) 80 81 stream, err := c.Stream("streams", "subscribe", Request{Count: "10"}) 82 if err != nil { 83 fmt.Println(err) 84 return 85 } 86 87 for { 88 var rsp Response 89 if err := stream.Recv(&rsp); err != nil { 90 fmt.Println(err) 91 return 92 } 93 fmt.Println("got", rsp.Count) 94 } 95 } 96 ``` 97