Skip to content

Commit

Permalink
feat: added hook for intercepting api calls made to the core
Browse files Browse the repository at this point in the history
  • Loading branch information
IamMayankThakur committed Nov 2, 2023
1 parent 583b953 commit 962f684
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
5 changes: 3 additions & 2 deletions supertokens/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion supertokens/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var (
querierLastTriedIndex int
querierLock sync.Mutex
querierHostLock sync.Mutex
querierInterceptor func(*http.Request, UserContext) *http.Request
)

func SetQuerierApiVersionForTests(version string) {
Expand Down Expand Up @@ -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
Expand All @@ -109,6 +110,7 @@ func initQuerier(hosts []QuerierHost, APIKey string) {
}
querierAPIVersion = ""
querierLastTriedIndex = 0
querierInterceptor = interceptor
}
}

Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion supertokens/supertokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 962f684

Please sign in to comment.