|
1 | 1 | # HTTP Client |
2 | 2 |  |
3 | 3 |
|
4 | | -This plugin is a http client for micro. |
| 4 | +This plugin is an HTTP client for [Micro](https://pkg.go.dev/go.unistack.org/micro/v4). |
| 5 | +It implements the [micro.Client](https://pkg.go.dev/go.unistack.org/micro/v4/client#Client) interface. |
5 | 6 |
|
6 | 7 | ## Overview |
7 | 8 |
|
8 | | -The http client wraps `net/http` to provide a robust micro client with service discovery, load balancing and streaming. |
9 | | -It complies with the [micro.Client](https://godoc.org/go.unistack.org/micro-client-http/v3#Client) interface. |
| 9 | +The HTTP client wraps `net/http` to provide a robust client with service discovery, load balancing and |
| 10 | +implements HTTP rules defined in the [google/api/http.proto](https://github.com/googleapis/googleapis/blob/master/google/api/http.proto) specification. |
10 | 11 |
|
11 | | -## Usage |
| 12 | +## Limitations |
| 13 | + |
| 14 | +* Streaming is not yet implemented. |
| 15 | +* Only protobuf-generated messages are supported. |
12 | 16 |
|
13 | | -### Use directly |
| 17 | +## Usage |
14 | 18 |
|
15 | 19 | ```go |
16 | | -import "go.unistack.org/micro-client-http/v3" |
| 20 | +import ( |
| 21 | + "go.unistack.org/micro/v4" |
| 22 | + http "go.unistack.org/micro-client-http/v4" |
| 23 | +) |
17 | 24 |
|
18 | 25 | service := micro.NewService( |
19 | 26 | micro.Name("my.service"), |
20 | 27 | micro.Client(http.NewClient()), |
21 | 28 | ) |
22 | 29 | ``` |
23 | 30 |
|
24 | | -### Call Service |
| 31 | +### Simple call |
25 | 32 |
|
26 | | -Assuming you have a http service "my.service" with path "/foo/bar" |
27 | 33 | ```go |
28 | | -// new client |
29 | | -client := http.NewClient() |
| 34 | +import ( |
| 35 | + "go.unistack.org/micro/v4/client" |
| 36 | + http "go.unistack.org/micro-client-http/v4" |
| 37 | + jsoncodec "go.unistack.org/micro-codec-json/v4" |
| 38 | +) |
30 | 39 |
|
31 | | -// create request/response |
32 | | -request := client.NewRequest("my.service", "/foo/bar", protoRequest{}) |
33 | | -response := new(protoResponse) |
| 40 | +c := http.NewClient( |
| 41 | + client.Codec("application/json", jsoncodec.NewCodec()), |
| 42 | +) |
| 43 | + |
| 44 | +req := c.NewRequest( |
| 45 | + "user-service", |
| 46 | + "/user/{user_id}/order/{order_id}", |
| 47 | + &protoReq{UserId: "123", OrderId: 456}, |
| 48 | +) |
| 49 | +rsp := new(protoRsp) |
34 | 50 |
|
35 | | -// call service |
36 | | -err := client.Call(context.TODO(), request, response) |
| 51 | +err := c.Call( |
| 52 | + ctx, |
| 53 | + req, |
| 54 | + rsp, |
| 55 | + client.WithAddress("example.com"), |
| 56 | +) |
37 | 57 | ``` |
38 | 58 |
|
39 | | -or you can call any rest api or site and unmarshal to response struct |
40 | | -```go |
41 | | -// new client |
42 | | -client := client.NewClientCallOptions(http.NewClient(), http.Address("https://api.github.com")) |
| 59 | +### Call with specific options |
43 | 60 |
|
44 | | -req := client.NewRequest("github", "/users/vtolstov", nil) |
45 | | -rsp := make(map[string]interface{}) |
| 61 | +```go |
| 62 | +import ( |
| 63 | + "go.unistack.org/micro/v4/client" |
| 64 | + http "go.unistack.org/micro-client-http/v4" |
| 65 | +) |
46 | 66 |
|
47 | | -err := c.Call(context.TODO(), req, &rsp, mhttp.Method(http.MethodGet)) |
| 67 | +err := c.Call( |
| 68 | + ctx, |
| 69 | + req, |
| 70 | + rsp, |
| 71 | + client.WithAddress("example.com"), |
| 72 | + http.Method("POST"), |
| 73 | + http.Path("/user/{user_id}/order/{order_id}"), |
| 74 | + http.Body("*"), // <- use all fields from the proto request as HTTP request body or specify a single field name to use only that field (see Google API HTTP spec: google/api/http.proto) |
| 75 | +) |
48 | 76 | ``` |
49 | 77 |
|
50 | | -Look at http_test.go for detailed use. |
| 78 | +### Call with request headers |
51 | 79 |
|
52 | | -### Encoding |
| 80 | +```go |
| 81 | +import ( |
| 82 | + "go.unistack.org/micro/v4/metadata" |
| 83 | + http "go.unistack.org/micro-client-http/v4" |
| 84 | +) |
| 85 | + |
| 86 | +ctx := metadata.NewOutgoingContext(ctx, metadata.Pairs( |
| 87 | + "Authorization", "Bearer token", |
| 88 | + "My-Header", "My-Header-Value", |
| 89 | +)) |
| 90 | + |
| 91 | +err := c.Call( |
| 92 | + ctx, |
| 93 | + req, |
| 94 | + rsp, |
| 95 | + http.Header("Authorization", "true", "My-Header", "false"), // <- call option that declares required/optional headers |
| 96 | +) |
| 97 | +``` |
| 98 | + |
| 99 | +### Call with response headers |
53 | 100 |
|
54 | | -Default protobuf with content-type application/proto |
55 | 101 | ```go |
56 | | -client.NewRequest("service", "/path", protoRequest{}) |
| 102 | +import ( |
| 103 | + "go.unistack.org/micro/v4/metadata" |
| 104 | + http "go.unistack.org/micro-client-http/v4" |
| 105 | +) |
| 106 | + |
| 107 | +respMetadata := metadata.Metadata{} |
| 108 | + |
| 109 | +err := c.Call( |
| 110 | + ctx, |
| 111 | + req, |
| 112 | + rsp, |
| 113 | + client.WithResponseMetadata(&respMetadata), // <- metadata with response headers |
| 114 | +) |
57 | 115 | ``` |
58 | 116 |
|
59 | | -Json with content-type application/json |
| 117 | +### Call with cookies |
| 118 | + |
60 | 119 | ```go |
61 | | -client.NewJsonRequest("service", "/path", jsonRequest{}) |
| 120 | +import ( |
| 121 | + "go.unistack.org/micro/v4/metadata" |
| 122 | + http "go.unistack.org/micro-client-http/v4" |
| 123 | +) |
| 124 | + |
| 125 | +ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs( |
| 126 | + "Cookie", "session_id=abc123; theme=dark", |
| 127 | +)) |
| 128 | + |
| 129 | +err := c.Call( |
| 130 | + ctx, |
| 131 | + req, |
| 132 | + rsp, |
| 133 | + http.Cookie("session_id", "true", "theme", "false"), // <- call option that declares required/optional cookies |
| 134 | +) |
62 | 135 | ``` |
63 | 136 |
|
| 137 | +### Call with error mapping |
| 138 | + |
| 139 | +```go |
| 140 | +import ( |
| 141 | + http "go.unistack.org/micro-client-http/v4" |
| 142 | + jsoncodec "go.unistack.org/micro-codec-json/v4" |
| 143 | +) |
| 144 | + |
| 145 | +err := c.Call( |
| 146 | + ctx, |
| 147 | + req, |
| 148 | + rsp, |
| 149 | + http.ErrorMap(map[string]any{ |
| 150 | + "default": &protoDefaultError{}, // <- default case |
| 151 | + "403": &protoSpecialError{}, // <- key is the HTTP status code that is mapped to this error |
| 152 | + }), |
| 153 | +) |
| 154 | +``` |
0 commit comments