Skip to content

Commit

Permalink
update go mod, improve examples
Browse files Browse the repository at this point in the history
  • Loading branch information
DiogoFerrao committed Apr 11, 2024
1 parent 85c3a51 commit c0a2d6e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
4 changes: 2 additions & 2 deletions examples/middleware/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/stackitcloud/stackit-sdk-go/examples/middleware
go 1.18

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

require (
Expand Down
18 changes: 11 additions & 7 deletions examples/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,25 @@ func RequestCapturer(status string) config.Middleware {
}
}

// roundTripperWithCapture is a custom round tripper that prints the request and a status it receives.
// roundTripperWithCapture is a custom round tripper that prints the request and an input it receives.
type roundTripperWithCapture struct {
transport http.RoundTripper
status string
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("Status:", rt.status)
fmt.Println("Input:", rt.inputString)

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

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

return resp, err
}

func main() {
Expand All @@ -44,8 +48,8 @@ func main() {
// In this case, the middleware with status "1" will be executed first
argusClient, err := argus.NewAPIClient(
config.WithRegion("eu01"),
config.WithMiddleware(RequestCapturer("2")),
config.WithMiddleware(RequestCapturer("1")),
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)
Expand Down

0 comments on commit c0a2d6e

Please sign in to comment.