-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
142 lines (115 loc) · 3.39 KB
/
main.go
File metadata and controls
142 lines (115 loc) · 3.39 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
135
136
137
138
139
140
141
142
// Package main demonstrates HTTP client usage.
package main
import (
"context"
"fmt"
"time"
"github.com/yigithankarabulut/wirekit"
"go.uber.org/zap"
)
// Post represents a blog post from JSONPlaceholder API.
type Post struct {
ID int `json:"id"`
UserID int `json:"userId"`
Title string `json:"title"`
Body string `json:"body"`
}
// BasicHTTPClient demonstrates basic HTTP client usage.
func BasicHTTPClient(ctx context.Context) {
fmt.Println("\n--- Basic HTTP Client ---")
// Create HTTP client with configuration
client := wirekit.HTTP().
WithBaseURL("https://jsonplaceholder.typicode.com").
WithTimeout(10*time.Second).
WithHeader("Accept", "application/json").
WithRetry(3, time.Second). // Retry 3 times with 1 second delay
Build()
// GET request
resp, err := client.Get(ctx, "/posts/1")
if err != nil {
wirekit.Error("GET request failed", zap.Error(err))
return
}
fmt.Println("Status:", resp.StatusCode)
fmt.Println("Success:", resp.IsSuccess())
// Parse JSON response
var post Post
if err := resp.JSON(&post); err != nil {
wirekit.Error("failed to parse JSON", zap.Error(err))
return
}
fmt.Printf("Post: %+v\n", post)
}
// GetJSONExample demonstrates GetJSON shorthand.
func GetJSONExample(ctx context.Context) {
fmt.Println("\n--- GET JSON (shorthand) ---")
client := wirekit.HTTP().
WithBaseURL("https://jsonplaceholder.typicode.com").
WithTimeout(10 * time.Second).
Build()
var posts []Post
if err := client.GetJSON(ctx, "/posts?_limit=3", &posts); err != nil {
wirekit.Error("GetJSON failed", zap.Error(err))
return
}
fmt.Printf("Got %d posts\n", len(posts))
}
// PostRequestExample demonstrates POST requests.
func PostRequestExample(ctx context.Context) {
fmt.Println("\n--- POST Request ---")
client := wirekit.HTTP().
WithBaseURL("https://jsonplaceholder.typicode.com").
WithTimeout(10 * time.Second).
Build()
newPost := map[string]any{
"title": "New Post",
"body": "This is the body",
"userId": 1,
}
resp, err := client.Post(ctx, "/posts", newPost)
if err != nil {
wirekit.Error("POST request failed", zap.Error(err))
return
}
fmt.Println("Created Status:", resp.StatusCode)
fmt.Println("Response:", resp.String())
}
// PostJSONExample demonstrates PostJSON shorthand.
func PostJSONExample(ctx context.Context) {
fmt.Println("\n--- POST JSON (shorthand) ---")
client := wirekit.HTTP().
WithBaseURL("https://jsonplaceholder.typicode.com").
WithTimeout(10 * time.Second).
Build()
newPost := map[string]any{
"title": "New Post",
"body": "This is the body",
"userId": 1,
}
var createdPost Post
if err := client.PostJSON(ctx, "/posts", newPost, &createdPost); err != nil {
wirekit.Error("PostJSON failed", zap.Error(err))
return
}
fmt.Printf("Created Post ID: %d\n", createdPost.ID)
}
// AuthenticatedClientExample demonstrates client with bearer token.
func AuthenticatedClientExample() {
fmt.Println("\n--- Client with Bearer Token ---")
authClient := wirekit.HTTP().
WithBaseURL("https://api.example.com").
WithBearerToken("your-jwt-token").
WithTimeout(30 * time.Second).
Build()
fmt.Printf("Auth client created: %T\n", authClient)
}
func main() {
wirekit.Log(wirekit.Zap).WithLevel("info").Init()
ctx := context.Background()
BasicHTTPClient(ctx)
GetJSONExample(ctx)
PostRequestExample(ctx)
PostJSONExample(ctx)
AuthenticatedClientExample()
wirekit.Info("HTTP client example completed")
}