Skip to content

Commit 20fb4b8

Browse files
authored
Add usage examples for withMiddleware and withCaptureHTTPRequest (#400)
* add example for middlewares * improve documentation of example * update go mod, improve examples * update go sum * move httpresponse example to runtime.go, added example for WithCaptureHTTPRequest
1 parent e509d0f commit 20fb4b8

File tree

7 files changed

+110
-9
lines changed

7 files changed

+110
-9
lines changed

examples/middleware/go.mod

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module github.com/stackitcloud/stackit-sdk-go/examples/middleware
2+
3+
go 1.18
4+
5+
require (
6+
github.com/stackitcloud/stackit-sdk-go/core v0.12.0
7+
github.com/stackitcloud/stackit-sdk-go/services/argus v0.10.0
8+
)
9+
10+
require (
11+
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
12+
github.com/google/uuid v1.6.0 // indirect
13+
)

examples/middleware/go.sum

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
2+
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
3+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
4+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
5+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
6+
github.com/stackitcloud/stackit-sdk-go/core v0.12.0 h1:auIzUUNRuydKOScvpICP4MifGgvOajiDQd+ncGmBL0U=
7+
github.com/stackitcloud/stackit-sdk-go/core v0.12.0/go.mod h1:mDX1mSTsB3mP+tNBGcFNx6gH1mGBN4T+dVt+lcw7nlw=
8+
github.com/stackitcloud/stackit-sdk-go/services/argus v0.10.0 h1:FAYOt6UBy/F2jPH2C/NnZnbjLZryJBjtM3afLVgGc4w=
9+
github.com/stackitcloud/stackit-sdk-go/services/argus v0.10.0/go.mod h1:nVllQfYODhX1q3bgwVTLO7wHOp+8NMLiKbn3u/Dg5nU=

examples/middleware/middleware.go

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"os"
8+
9+
"github.com/stackitcloud/stackit-sdk-go/core/config"
10+
"github.com/stackitcloud/stackit-sdk-go/services/argus"
11+
)
12+
13+
// RequestCapturer is a middleware that prints the request and a status it receives.
14+
func RequestCapturer(status string) config.Middleware {
15+
return func(rt http.RoundTripper) http.RoundTripper {
16+
return &roundTripperWithCapture{rt, status}
17+
}
18+
}
19+
20+
// roundTripperWithCapture is a custom round tripper that prints the request and an input it receives.
21+
type roundTripperWithCapture struct {
22+
transport http.RoundTripper
23+
inputString string
24+
}
25+
26+
func (rt roundTripperWithCapture) RoundTrip(req *http.Request) (*http.Response, error) {
27+
returnStr := fmt.Sprintf("%s %s", req.Method, req.URL.String())
28+
29+
fmt.Println("Captured request:", returnStr)
30+
fmt.Println("Input:", rt.inputString)
31+
32+
// Proceed with the original round trip
33+
// This step is important to preserve the original behavior of the client
34+
resp, err := rt.transport.RoundTrip(req)
35+
36+
fmt.Println("Captured response status:", resp.Status)
37+
38+
return resp, err
39+
}
40+
41+
func main() {
42+
// Specify the project ID
43+
projectId := "PROJECT_ID"
44+
45+
// Create a new API client, that uses default authentication
46+
// Use the `WithMiddleware` option to add the middleware to the client
47+
// The middleware will be executed in the reverse order they are added
48+
// In this case, the middleware with status "1" will be executed first
49+
argusClient, err := argus.NewAPIClient(
50+
config.WithRegion("eu01"),
51+
config.WithMiddleware(RequestCapturer("Middleware 2")),
52+
config.WithMiddleware(RequestCapturer("Middleware 1")),
53+
)
54+
if err != nil {
55+
fmt.Fprintf(os.Stderr, "[Argus API] Creating API client: %v\n", err)
56+
os.Exit(1)
57+
}
58+
59+
// Get the argus instances for your project
60+
getInstanceResp, err := argusClient.ListInstances(context.Background(), projectId).Execute()
61+
62+
if err != nil {
63+
fmt.Fprintf(os.Stderr, "[Argus API] Error when calling `GetInstances`: %v\n", err)
64+
} else {
65+
fmt.Printf("[Argus API] Number of instances: %v\n", len(*getInstanceResp.Instances))
66+
}
67+
}

examples/httpresponse/go.mod renamed to examples/runtime/go.mod

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
module github.com/stackitcloud/stackit-sdk-go/examples/httpresponse
1+
module github.com/stackitcloud/stackit-sdk-go/examples/runtime
22

33
go 1.18
44

55
require (
6-
github.com/stackitcloud/stackit-sdk-go/core v0.10.1
7-
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v0.12.0
6+
github.com/stackitcloud/stackit-sdk-go/core v0.12.0
7+
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v0.13.0
88
)
99

1010
require (

examples/httpresponse/go.sum renamed to examples/runtime/go.sum

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVI
33
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
44
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
55
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
6-
github.com/stackitcloud/stackit-sdk-go/core v0.10.1 h1:lzyualywD/2xIsYUHwlqCurG1OwlqCJVtJbOcPO6OzE=
7-
github.com/stackitcloud/stackit-sdk-go/core v0.10.1/go.mod h1:mDX1mSTsB3mP+tNBGcFNx6gH1mGBN4T+dVt+lcw7nlw=
8-
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v0.12.0 h1:W2WSYUyhKaHQ+BZfmyRw9PKv5q7ihGRyNhNgIlyM+Y8=
9-
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v0.12.0/go.mod h1:P0YyvgwIsVKJijdWGVJVOp/ac7PVX99Oj+dr4v1zECc=
6+
github.com/stackitcloud/stackit-sdk-go/core v0.12.0 h1:auIzUUNRuydKOScvpICP4MifGgvOajiDQd+ncGmBL0U=
7+
github.com/stackitcloud/stackit-sdk-go/core v0.12.0/go.mod h1:mDX1mSTsB3mP+tNBGcFNx6gH1mGBN4T+dVt+lcw7nlw=
8+
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v0.13.0 h1:PGLjBZxWM7NIrH1+W1+f+/4kZEgwv9DGnXcUzOqM0M8=
9+
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v0.13.0/go.mod h1:SdrqGLCkilL6wl1+jcxmLtks2IocgIg+bsyeyYUIzR4=

examples/httpresponse/httpresponse.go renamed to examples/runtime/runtime.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,16 @@ func main() {
3333
os.Exit(1)
3434
}
3535
fmt.Printf("Number of instances: %v\n\n", len(*getInstancesResp.Items))
36-
fmt.Printf("HTTP response: %+v\n", *httpResp)
36+
fmt.Printf("HTTP response: %+v\n\n", *httpResp)
37+
38+
// Get the MongoDB Flex instances for your project and capture the HTTP request using the context
39+
var httpReq *http.Request
40+
ctxWithHTTPReq := runtime.WithCaptureHTTPRequest(context.Background(), &httpReq)
41+
getInstancesResp, err = postgresflexClient.ListInstances(ctxWithHTTPReq, projectId).Execute()
42+
if err != nil {
43+
fmt.Fprintf(os.Stderr, "Error when calling `ListInstances`: %v\n", err)
44+
os.Exit(1)
45+
}
46+
fmt.Printf("Number of instances: %v\n\n", len(*getInstancesResp.Items))
47+
fmt.Printf("HTTP request: %+v\n\n", *httpReq)
3748
}

go.work

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ use (
99
./examples/configuration
1010
./examples/dns
1111
./examples/errorhandling
12-
./examples/httpresponse
1312
./examples/loadbalancer
1413
./examples/logme
1514
./examples/mariadb
15+
./examples/middleware
1616
./examples/mongodbflex
1717
./examples/objectstorage
1818
./examples/opensearch
@@ -21,6 +21,7 @@ use (
2121
./examples/rabbitmq
2222
./examples/redis
2323
./examples/resourcemanager
24+
./examples/runtime
2425
./examples/secretsmanager
2526
./examples/serviceaccount
2627
./examples/ske

0 commit comments

Comments
 (0)