Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
97 lines (81 loc) · 2.18 KB
/
Copy pathexample_test.go
File metadata and controls
97 lines (81 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package csbgo_test
import (
"context"
"fmt"
"log"
"github.com/gomodb/csbgo"
)
// ExampleClient configures a Client and invokes a CSB-published service.
func ExampleClient() {
client := csbgo.New(
csbgo.WithBaseURL("http://broker.example.com/CSB"), // the single CSB endpoint
csbgo.WithAK("your-access-key"),
csbgo.WithSK("your-secret-key"),
csbgo.WithDebug(true),
)
resp, err := client.Do(context.Background(),
csbgo.NewRequest(csbgo.MethodPost).
WithAPI("MyService").
WithVersion("v1").
WithQuery("name", "wiseking").
WithForm("p1", "dog"),
)
if err != nil {
// Non-2xx statuses surface here as *csbgo.StatusError.
log.Fatalf("call failed: %v", err)
}
fmt.Println(resp.ToString())
}
// ExampleClient_Do shows a JSON POST body through Client.Do.
func ExampleClient_Do() {
client := csbgo.New(
csbgo.WithBaseURL("http://broker.example.com/CSB"),
csbgo.WithAK("ak"),
csbgo.WithSK("sk"),
)
resp, err := client.Do(context.Background(),
csbgo.NewRequest(csbgo.MethodPost).
WithAPI("Echo").
WithVersion("v1").
WithJSON(map[string]any{"hello": "world"}),
)
if err != nil {
log.Fatalf("call failed: %v", err)
}
var out map[string]any
if err := resp.ToJSON(&out); err != nil {
log.Fatalf("decode failed: %v", err)
}
fmt.Printf("%v\n", out)
}
// ExampleClient_DoAs shows the generic, typed one-step call.
func ExampleClient_DoAs() {
client := csbgo.New(
csbgo.WithBaseURL("http://broker.example.com/CSB"),
csbgo.WithAK("ak"),
csbgo.WithSK("sk"),
)
type User struct {
Name string `json:"name"`
}
user, err := client.DoAs[User](context.Background(),
csbgo.NewRequest(csbgo.MethodGet).
WithAPI("UserService").
WithVersion("v1"))
if err != nil {
log.Fatalf("call failed: %v", err)
}
fmt.Println(user.Name)
}
// ExampleRequest_Clone shows deriving per-call requests from an endpoint
// template without mutating the original.
func ExampleRequest_Clone() {
base := csbgo.NewRequest(csbgo.MethodGet).
WithAPI("MyService").
WithVersion("v1")
user := base.Clone().WithPath("users/").WithPath("1001")
orders := base.Clone().WithPath("orders/").WithQueryInt("page", 2)
_ = user
_ = orders
// Pass user / orders to client.Do(ctx, ...).
}