Skip to content

Commit

Permalink
feat: make hooks configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
pmalek committed Aug 7, 2024
1 parent 286b446 commit db15240
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 43 deletions.
49 changes: 49 additions & 0 deletions internal/hooks/httpdump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package hooks

import (
"fmt"
"net/http"
"net/http/httputil"
)

type HTTPDumpRequestHook struct {
Enabled bool
}

var _ beforeRequestHook = (*HTTPDumpRequestHook)(nil)

func (i *HTTPDumpRequestHook) BeforeRequest(hookCtx BeforeRequestContext, req *http.Request) (*http.Request, error) {
if !i.Enabled {
return req, nil
}

b, err := httputil.DumpRequestOut(req, true)
if err != nil {
fmt.Printf("Error dumping request: %v\n", err)
} else {
fmt.Printf("request:\n%s\n\n", b)
}

return req, nil
}

type HTTPDumpResponseHook struct {
Enabled bool
}

var _ afterSuccessHook = (*HTTPDumpResponseHook)(nil)

func (i *HTTPDumpResponseHook) AfterSuccess(hookCtx AfterSuccessContext, res *http.Response) (*http.Response, error) {
if !i.Enabled {
return res, nil
}

b, err := httputil.DumpResponse(res, true)
if err != nil {
fmt.Printf("Error dumping respone: %v\n", err)
} else {
fmt.Printf("response:\n%s\n\n", b)
}

return res, nil
}
23 changes: 23 additions & 0 deletions internal/hooks/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package hooks

import "os"

/*
* This file is only ever generated once on the first generation and then is free to be modified.
* Any hooks you wish to add should be registered in the InitHooks function. Feel free to define them
* in this file or in separate files in the hooks package.
*/

func initHooks(h *Hooks) {
h.registerBeforeRequestHook(&UserAgentPreRequestHook{})

h.registerBeforeRequestHook(&APIURLRequestHook{})

h.registerBeforeRequestHook(&HTTPDumpRequestHook{
Enabled: os.Getenv("KONNECT_SDK_HTTP_DUMP_REQUEST") == "true",
})

h.registerAfterSuccessHook(&HTTPDumpResponseHook{
Enabled: os.Getenv("KONNECT_SDK_HTTP_DUMP_RESPONSE") == "true",
})
}
43 changes: 0 additions & 43 deletions internal/hooks/registration.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
package hooks

import (
"fmt"
"net/http"
"net/http/httputil"
"strings"
"sync"
)

/*
* This file is only ever generated once on the first generation and then is free to be modified.
* Any hooks you wish to add should be registered in the InitHooks function. Feel free to define them
* in this file or in separate files in the hooks package.
*/

func initHooks(h *Hooks) {
h.registerBeforeRequestHook(&UserAgentPreRequestHook{})
h.registerAfterSuccessHook(&UserAgentPreRequestHook{})
}

var (
l sync.RWMutex
userAgent string
Expand All @@ -42,36 +28,7 @@ type UserAgentPreRequestHook struct{}
var _ beforeRequestHook = (*UserAgentPreRequestHook)(nil)

func (i *UserAgentPreRequestHook) BeforeRequest(hookCtx BeforeRequestContext, req *http.Request) (*http.Request, error) {
b, err := httputil.DumpRequestOut(req, true)
if err != nil {
fmt.Printf("Error dumping request: %v\n", err)
} else {
fmt.Printf("request:\n%s\n\n", b)
}

req.Header.Set("User-Agent", GetUserAgent())

switch hookCtx.OperationID {
case "get-organizations-me":
// NOTE(pmalek): This is because we merge OpenAPI specs and /organizations/me
// is only served by the global API.
// @mheap mentioned that we can add operation specific URLs to do away with this.
if strings.HasSuffix(req.URL.Host, "api.konghq.tech") {
req.URL.Host = "global.api.konghq.tech"
} else {
req.URL.Host = "global.api.konghq.com"
}
}
return req, nil
}

func (i *UserAgentPreRequestHook) AfterSuccess(hookCtx AfterSuccessContext, res *http.Response) (*http.Response, error) {
b, err := httputil.DumpResponse(res, true)
if err != nil {
fmt.Printf("Error dumping respone: %v\n", err)
} else {
fmt.Printf("response:\n%s\n\n", b)
}

return res, nil
}
25 changes: 25 additions & 0 deletions internal/hooks/url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package hooks

import (
"net/http"
"strings"
)

type APIURLRequestHook struct{}

var _ beforeRequestHook = (*APIURLRequestHook)(nil)

func (i *APIURLRequestHook) BeforeRequest(hookCtx BeforeRequestContext, req *http.Request) (*http.Request, error) {
switch hookCtx.OperationID {
case "get-organizations-me":
// NOTE(pmalek): This is because we merge OpenAPI specs and /organizations/me
// is only served by the global API.
// @mheap mentioned that we can add operation specific URLs to do away with this.
if strings.HasSuffix(req.URL.Host, "api.konghq.tech") {
req.URL.Host = "global.api.konghq.tech"
} else {
req.URL.Host = "global.api.konghq.com"
}
}
return req, nil
}

0 comments on commit db15240

Please sign in to comment.