-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample_test.go
51 lines (41 loc) · 1.17 KB
/
example_test.go
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
package httputil_test
import (
"fmt"
"github.com/sudo-suhas/xgo/httputil"
)
func ExampleURLBuilderSource() {
b, err := httputil.NewURLBuilderSource("https://api.example.com/v1")
check(err)
u := b.NewURLBuilder().
Path("/users").
URL()
fmt.Println("URL:", u.String())
// Output:
// URL: https://api.example.com/v1/users
}
func ExampleURLBuilder() {
b, err := httputil.NewURLBuilderSource("https://api.example.com/v1/")
check(err)
u := b.NewURLBuilder().
Path("/users/{userID}/posts/{postID}/comments").
PathParam("userID", "foo").
PathParamInt("postID", 42).
QueryParam("author_id", "foo", "bar").
QueryParamInt("limit", 20).
QueryParamBool("recent", true).
URL()
fmt.Println("URL:", u.String())
u = b.NewURLBuilder().
Path("posts/{title}").
PathParam("title", `Letters & "Special" Characters`).
URL()
fmt.Println("URL (encoded path param):", u.String())
// Output:
// URL: https://api.example.com/v1/users/foo/posts/42/comments?author_id=foo&author_id=bar&limit=20&recent=true
// URL (encoded path param): https://api.example.com/v1/posts/Letters%2520&%2520%2522Special%2522%2520Characters
}
func check(err error) {
if err != nil {
panic(err)
}
}