Skip to content

Commit cbe955e

Browse files
committed
feat: Add option to pass external context into Go SDK's HTTP calls
1 parent 0c6b5c0 commit cbe955e

File tree

3 files changed

+86
-12
lines changed

3 files changed

+86
-12
lines changed

go/rtl/auth.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,13 @@ func NewAuthSessionWithTransport(config ApiSettings, transport http.RoundTripper
6767
AuthStyle: oauth2.AuthStyleInParams,
6868
}
6969

70+
bgCtx := context.Background()
71+
if config.Context != nil {
72+
bgCtx = config.Context
73+
}
74+
7075
ctx := context.WithValue(
71-
context.Background(),
76+
bgCtx,
7277
oauth2.HTTPClient,
7378
// Will set "x-looker-appid" Header on TokenURL requests
7479
&http.Client{Transport: appIdHeaderTransport},
@@ -123,18 +128,25 @@ func (s *AuthSession) Do(result interface{}, method, ver, path string, reqPars m
123128
}
124129
}
125130

126-
// create request context with timeout
127-
var timeoutInSeconds int32 = 120 //seconds
128-
if s.Config.Timeout != 0 {
129-
timeoutInSeconds = s.Config.Timeout
130-
}
131-
if options != nil && options.Timeout != 0 {
132-
timeoutInSeconds = options.Timeout
131+
var ctx context.Context
132+
if options != nil && options.Context != nil {
133+
ctx = options.Context
134+
} else if s.Config.Context != nil {
135+
ctx = s.Config.Context
136+
} else {
137+
// create request context with timeout
138+
var timeoutInSeconds int32 = 120 //seconds
139+
if s.Config.Timeout != 0 {
140+
timeoutInSeconds = s.Config.Timeout
141+
}
142+
if options != nil && options.Timeout != 0 {
143+
timeoutInSeconds = options.Timeout
144+
}
145+
var cncl context.CancelFunc
146+
ctx, cncl = context.WithTimeout(context.Background(), time.Second*time.Duration(timeoutInSeconds))
147+
defer cncl()
133148
}
134149

135-
ctx, cncl := context.WithTimeout(context.Background(), time.Second*time.Duration(timeoutInSeconds))
136-
defer cncl()
137-
138150
// create new request
139151
req, err := http.NewRequestWithContext(ctx, method, u, bytes.NewBufferString(bodyString))
140152
if err != nil {

go/rtl/auth_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,65 @@ func TestAuthSession_Do_Timeout(t *testing.T) {
635635
t.Errorf("Do() call did not error with context.DeadlineExceeded, got=%v", err)
636636
}
637637
})
638+
639+
t.Run("Do() follows Context set in AuthSession config", func(t *testing.T) {
640+
mux := http.NewServeMux()
641+
setupApi40Login(mux, foreverValidTestToken, http.StatusOK)
642+
server := httptest.NewServer(mux)
643+
defer server.Close()
644+
645+
mux.HandleFunc("/api"+apiVersion+path, func(w http.ResponseWriter, r *http.Request) {
646+
time.Sleep(4 * time.Second)
647+
})
648+
649+
ctx, cncl := context.WithTimeout(context.Background(), 1*time.Second)
650+
defer cncl()
651+
652+
session := NewAuthSession(ApiSettings{
653+
BaseUrl: server.URL,
654+
ApiVersion: apiVersion,
655+
Context: ctx,
656+
})
657+
658+
err := session.Do(nil, "GET", apiVersion, path, nil, nil, nil)
659+
660+
if err == nil {
661+
t.Errorf("Do() call did not error/timeout")
662+
} else if !errors.Is(err, context.DeadlineExceeded) {
663+
t.Errorf("Do() call did not error with context.DeadlineExceeded, got=%v", err)
664+
}
665+
})
666+
667+
t.Run("Do() follows Context set in Do()'s options", func(t *testing.T) {
668+
mux := http.NewServeMux()
669+
setupApi40Login(mux, foreverValidTestToken, http.StatusOK)
670+
server := httptest.NewServer(mux)
671+
defer server.Close()
672+
673+
mux.HandleFunc("/api"+apiVersion+path, func(w http.ResponseWriter, r *http.Request) {
674+
time.Sleep(4 * time.Second)
675+
})
676+
677+
ctx, cncl := context.WithTimeout(context.Background(), 1*time.Second)
678+
defer cncl()
679+
680+
session := NewAuthSession(ApiSettings{
681+
BaseUrl: server.URL,
682+
ApiVersion: apiVersion,
683+
})
684+
685+
options := ApiSettings{
686+
Context: ctx,
687+
}
688+
689+
err := session.Do(nil, "GET", apiVersion, path, nil, nil, &options)
690+
691+
if err == nil {
692+
t.Errorf("Do() call did not error/timeout")
693+
} else if !errors.Is(err, context.DeadlineExceeded) {
694+
t.Errorf("Do() call did not error with context.DeadlineExceeded, got=%v", err)
695+
}
696+
})
638697
}
639698

640699
func TestSetQuery(t *testing.T) {

go/rtl/settings.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package rtl
22

33
import (
4+
"context"
45
"fmt"
5-
"gopkg.in/ini.v1"
66
"os"
77
"strconv"
88
"strings"
9+
10+
"gopkg.in/ini.v1"
911
)
1012

1113
var defaultSectionName string = "Looker"
@@ -20,6 +22,7 @@ type ApiSettings struct {
2022
ClientSecret string `ini:"client_secret"`
2123
ApiVersion string `ini:"api_version"`
2224
Headers map[string]string
25+
Context context.Context
2326
}
2427

2528
var defaultSettings ApiSettings = ApiSettings{

0 commit comments

Comments
 (0)