Skip to content

Commit 0d97198

Browse files
authored
docs: add WithApplication and action waiting to code example (#808)
The code example in our README.md only contained a minimal example showing how to set up the client and make a simple GET request. This introduces two additional "features" to the example: 1. We sometimes get support requests from users who forget to wait for the returned actions and expect the server (or other resource) to be immediately ready. 2. We regularly get support requests from users where the rate limit is exhausted by tools that do not identify themselves. This makes it hard to help users, as we are missing information. By showing the best-practices for both of these cases in the example, I hope that more people adopt them and have a better experience overall. Additionally I added the example to the go package docs for `hcloud`, so it also shows up on pkgs.go.dev for the package.
1 parent 8a968c5 commit 0d97198

File tree

2 files changed

+85
-15
lines changed

2 files changed

+85
-15
lines changed

README.md

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,47 @@ go get github.com/hetznercloud/hcloud-go/v2/hcloud
2828
package main
2929

3030
import (
31-
"context"
32-
"fmt"
33-
"log"
31+
"context"
32+
"fmt"
33+
"log"
3434

35-
"github.com/hetznercloud/hcloud-go/v2/hcloud"
35+
"github.com/hetznercloud/hcloud-go/v2/hcloud"
36+
"github.com/hetznercloud/hcloud-go/v2/hcloud/exp/actionutil"
3637
)
3738

3839
func main() {
39-
client := hcloud.NewClient(hcloud.WithToken("token"))
40-
41-
server, _, err := client.Server.GetByID(context.Background(), 1)
42-
if err != nil {
43-
log.Fatalf("error retrieving server: %s\n", err)
44-
}
45-
if server != nil {
46-
fmt.Printf("server 1 is called %q\n", server.Name)
47-
} else {
48-
fmt.Println("server 1 not found")
49-
}
40+
ctx := context.Background()
41+
42+
client := hcloud.NewClient(
43+
hcloud.WithToken("token"),
44+
hcloud.WithApplication("my-tool", "v1.0.0"),
45+
)
46+
47+
result, _, err := client.Server.Create(ctx, hcloud.ServerCreateOpts{
48+
Name: "Foo",
49+
Image: &hcloud.Image{Name: "ubuntu-24.0"},
50+
ServerType: &hcloud.ServerType{Name: "cpx22"},
51+
Location: &hcloud.Location{Name: "hel1"},
52+
})
53+
if err != nil {
54+
log.Fatalf("error creating server: %s\n", err)
55+
}
56+
57+
// Always await any returned actions, to make sure the async process is completed before you use the result:
58+
err = client.Action.WaitFor(ctx, actionutil.AppendNext(result.Action, result.NextActions)...)
59+
if err != nil {
60+
log.Fatalf("error creating server: %s\n", err)
61+
}
62+
63+
server, _, err := client.Server.GetByID(ctx, result.Server.ID)
64+
if err != nil {
65+
log.Fatalf("error retrieving server: %s\n", err)
66+
}
67+
if server != nil {
68+
fmt.Printf("server is called %q\n", server.Name) // prints 'server is called "Foo"'
69+
} else {
70+
fmt.Println("server not found")
71+
}
5072
}
5173
```
5274

hcloud/hcloud.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,54 @@ Make sure to follow our API changelog available at https://docs.hetzner.cloud/ch
77
(or the RRS feed available at https://docs.hetzner.cloud/changelog/feed.rss) to be
88
notified about additions, deprecations and removals.
99
10+
# Example
11+
12+
package main
13+
14+
import (
15+
"context"
16+
"fmt"
17+
"log"
18+
19+
"github.com/hetznercloud/hcloud-go/v2/hcloud"
20+
"github.com/hetznercloud/hcloud-go/v2/hcloud/exp/actionutil"
21+
)
22+
23+
func main() {
24+
ctx := context.Background()
25+
26+
client := hcloud.NewClient(
27+
hcloud.WithToken("token"),
28+
hcloud.WithApplication("my-tool", "v1.0.0"),
29+
)
30+
31+
result, _, err := client.Server.Create(ctx, hcloud.ServerCreateOpts{
32+
Name: "Foo",
33+
Image: &hcloud.Image{Name: "ubuntu-24.0"},
34+
ServerType: &hcloud.ServerType{Name: "cpx22"},
35+
Location: &hcloud.Location{Name: "hel1"},
36+
})
37+
if err != nil {
38+
log.Fatalf("error creating server: %s\n", err)
39+
}
40+
41+
// Always await any returned actions, to make sure the async process is completed before you use the result:
42+
err = client.Action.WaitFor(ctx, actionutil.AppendNext(result.Action, result.NextActions)...)
43+
if err != nil {
44+
log.Fatalf("error creating server: %s\n", err)
45+
}
46+
47+
server, _, err := client.Server.GetByID(ctx, result.Server.ID)
48+
if err != nil {
49+
log.Fatalf("error retrieving server: %s\n", err)
50+
}
51+
if server != nil {
52+
fmt.Printf("server is called %q\n", server.Name) // prints 'server is called "Foo"'
53+
} else {
54+
fmt.Println("server not found")
55+
}
56+
}
57+
1058
# Retry mechanism
1159
1260
The [Client.Do] method will retry failed requests that match certain criteria. The

0 commit comments

Comments
 (0)