From 962f684ffb7f3c6a4e571d6aa2db3c1c0f2edc16 Mon Sep 17 00:00:00 2001 From: Mayank Thakur Date: Thu, 2 Nov 2023 19:09:36 +0530 Subject: [PATCH] feat: added hook for intercepting api calls made to the core --- supertokens/models.go | 5 +++-- supertokens/querier.go | 5 ++++- supertokens/supertokens.go | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/supertokens/models.go b/supertokens/models.go index 3c842d45..2a89cd96 100644 --- a/supertokens/models.go +++ b/supertokens/models.go @@ -51,8 +51,9 @@ type TypeInput struct { } type ConnectionInfo struct { - ConnectionURI string - APIKey string + ConnectionURI string + APIKey string + NetworkInterceptor func(*http.Request, UserContext) *http.Request } type APIHandled struct { diff --git a/supertokens/querier.go b/supertokens/querier.go index 263442f8..9d664645 100644 --- a/supertokens/querier.go +++ b/supertokens/querier.go @@ -44,6 +44,7 @@ var ( querierLastTriedIndex int querierLock sync.Mutex querierHostLock sync.Mutex + querierInterceptor func(*http.Request, UserContext) *http.Request ) func SetQuerierApiVersionForTests(version string) { @@ -100,7 +101,7 @@ func GetNewQuerierInstanceOrThrowError(rIDToCore string) (*Querier, error) { return &Querier{RIDToCore: rIDToCore}, nil } -func initQuerier(hosts []QuerierHost, APIKey string) { +func initQuerier(hosts []QuerierHost, APIKey string, interceptor func(*http.Request, UserContext) *http.Request) { if !querierInitCalled { querierInitCalled = true QuerierHosts = hosts @@ -109,6 +110,7 @@ func initQuerier(hosts []QuerierHost, APIKey string) { } querierAPIVersion = "" querierLastTriedIndex = 0 + querierInterceptor = interceptor } } @@ -129,6 +131,7 @@ func (q *Querier) SendPostRequest(path string, data map[string]interface{}) (map if err != nil { return nil, err } + req = querierInterceptor(req, nil) apiVerion, querierAPIVersionError := q.GetQuerierAPIVersion() if querierAPIVersionError != nil { diff --git a/supertokens/supertokens.go b/supertokens/supertokens.go index dcb3ed7e..9c209f38 100644 --- a/supertokens/supertokens.go +++ b/supertokens/supertokens.go @@ -85,7 +85,7 @@ func supertokensInit(config TypeInput) error { BasePath: basePath, }) } - initQuerier(hosts, config.Supertokens.APIKey) + initQuerier(hosts, config.Supertokens.APIKey, config.Supertokens.NetworkInterceptor) superTokens.SuperTokens = *config.Supertokens } else { return errors.New("please provide 'ConnectionURI' value. If you do not want to provide a connection URI, then set config.Supertokens to nil")