-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
134 lines (122 loc) · 3.14 KB
/
example_test.go
File metadata and controls
134 lines (122 loc) · 3.14 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package ghkit_test
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
ghkit "github.com/pcanilho/go-github-kit"
"github.com/pcanilho/go-github-kit/etag"
"github.com/pcanilho/go-github-kit/ghtest"
"github.com/pcanilho/go-github-kit/pages"
"github.com/pcanilho/go-github-kit/throttle"
)
// ExampleHTTPClient is the library-agnostic entry point: a configured
// *http.Client you can hand to any client library that takes one.
func ExampleHTTPClient() {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "ok")
}))
defer srv.Close()
hc, err := ghkit.HTTPClient(
ghkit.WithToken("fake-token"),
ghkit.WithETagCache(),
)
if err != nil {
fmt.Println("construct:", err)
return
}
resp, err := hc.Get(srv.URL)
if err != nil {
fmt.Println("get:", err)
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Print(string(body))
// Output: ok
}
// Example_etagOnly uses only the etag sub-package inside a hand-built
// transport chain.
func Example_etagOnly() {
rt, err := etag.NewTransport(nil,
etag.WithCache(etag.NewLRUCache(1024)),
etag.WithKeyScope("tenant-42"),
)
if err != nil {
fmt.Println("construct:", err)
return
}
hc := &http.Client{Transport: rt}
resp, err := hc.Get("https://api.github.com/meta")
if err != nil {
fmt.Println("get:", err)
return
}
if err := resp.Body.Close(); err != nil {
fmt.Println("close:", err)
}
}
// Example_paginated walks a Link-paginated endpoint with the pages
// sub-package. The fixture serves three pages of two items each; the
// iterator yields one element at a time without the caller writing the
// Link-walking loop.
func Example_paginated() {
var srv *httptest.Server
srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
page := 1
if p := r.URL.Query().Get("page"); p == "2" {
page = 2
} else if p == "3" {
page = 3
}
base := srv.URL + r.URL.Path
if link := ghtest.LinkHeader(base, page, 2, 3); link != "" {
w.Header().Set("Link", link)
}
w.Header().Set("Content-Type", "application/json")
switch page {
case 1:
fmt.Fprint(w, `[{"id":1},{"id":2}]`)
case 2:
fmt.Fprint(w, `[{"id":3},{"id":4}]`)
case 3:
fmt.Fprint(w, `[{"id":5},{"id":6}]`)
}
}))
defer srv.Close()
type item struct {
ID int `json:"id"`
}
var ids []int
for it, err := range pages.As[item](context.Background(), srv.Client(), "GET", srv.URL+"/items", nil) {
if err != nil {
fmt.Println("err:", err)
return
}
ids = append(ids, it.ID)
}
fmt.Println(ids)
// Output: [1 2 3 4 5 6]
}
// Example_throttle wraps any http.RoundTripper in a token-bucket cap.
func Example_throttle() {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "ok")
}))
defer srv.Close()
rt, err := throttle.NewTransport(http.DefaultTransport, 10.0, throttle.WithBurst(1))
if err != nil {
fmt.Println("construct:", err)
return
}
hc := &http.Client{Transport: rt}
resp, err := hc.Get(srv.URL)
if err != nil {
fmt.Println("get:", err)
return
}
defer resp.Body.Close()
fmt.Println(resp.StatusCode)
// Output: 200
}