-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
97 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |