Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add usage examples for withMiddleware and withCaptureHTTPRequest #400

Merged
merged 7 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions examples/middleware/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module github.com/stackitcloud/stackit-sdk-go/examples/middleware

go 1.18

require (
github.com/stackitcloud/stackit-sdk-go/core v0.12.0
github.com/stackitcloud/stackit-sdk-go/services/argus v0.10.0
)

require (
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/google/uuid v1.6.0 // indirect
)
9 changes: 9 additions & 0 deletions examples/middleware/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/stackitcloud/stackit-sdk-go/core v0.12.0 h1:auIzUUNRuydKOScvpICP4MifGgvOajiDQd+ncGmBL0U=
github.com/stackitcloud/stackit-sdk-go/core v0.12.0/go.mod h1:mDX1mSTsB3mP+tNBGcFNx6gH1mGBN4T+dVt+lcw7nlw=
github.com/stackitcloud/stackit-sdk-go/services/argus v0.10.0 h1:FAYOt6UBy/F2jPH2C/NnZnbjLZryJBjtM3afLVgGc4w=
github.com/stackitcloud/stackit-sdk-go/services/argus v0.10.0/go.mod h1:nVllQfYODhX1q3bgwVTLO7wHOp+8NMLiKbn3u/Dg5nU=
67 changes: 67 additions & 0 deletions examples/middleware/middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package main
DiogoFerrao marked this conversation as resolved.
Show resolved Hide resolved

import (
"context"
"fmt"
"net/http"
"os"

"github.com/stackitcloud/stackit-sdk-go/core/config"
"github.com/stackitcloud/stackit-sdk-go/services/argus"
)

// RequestCapturer is a middleware that prints the request and a status it receives.
func RequestCapturer(status string) config.Middleware {
return func(rt http.RoundTripper) http.RoundTripper {
return &roundTripperWithCapture{rt, status}
}
}

// roundTripperWithCapture is a custom round tripper that prints the request and an input it receives.
type roundTripperWithCapture struct {
transport http.RoundTripper
inputString string
}

func (rt roundTripperWithCapture) RoundTrip(req *http.Request) (*http.Response, error) {
returnStr := fmt.Sprintf("%s %s", req.Method, req.URL.String())

fmt.Println("Captured request:", returnStr)
fmt.Println("Input:", rt.inputString)

// Proceed with the original round trip
// This step is important to preserve the original behavior of the client
resp, err := rt.transport.RoundTrip(req)

fmt.Println("Captured response status:", resp.Status)

return resp, err
}

func main() {
// Specify the project ID
projectId := "PROJECT_ID"

// Create a new API client, that uses default authentication
// Use the `WithMiddleware` option to add the middleware to the client
// The middleware will be executed in the reverse order they are added
// In this case, the middleware with status "1" will be executed first
argusClient, err := argus.NewAPIClient(
config.WithRegion("eu01"),
config.WithMiddleware(RequestCapturer("Middleware 2")),
config.WithMiddleware(RequestCapturer("Middleware 1")),
)
if err != nil {
fmt.Fprintf(os.Stderr, "[Argus API] Creating API client: %v\n", err)
os.Exit(1)
}

// Get the argus instances for your project
getInstanceResp, err := argusClient.ListInstances(context.Background(), projectId).Execute()

if err != nil {
fmt.Fprintf(os.Stderr, "[Argus API] Error when calling `GetInstances`: %v\n", err)
} else {
fmt.Printf("[Argus API] Number of instances: %v\n", len(*getInstanceResp.Instances))
}
}
1 change: 1 addition & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use (
./examples/loadbalancer
./examples/logme
./examples/mariadb
./examples/middleware
./examples/mongodbflex
./examples/objectstorage
./examples/opensearch
Expand Down
Loading