From 7633c466b511531d4ca8fec3d43a9a232e65b28d Mon Sep 17 00:00:00 2001 From: Jeremy Chang Date: Wed, 29 Mar 2023 17:07:25 +0000 Subject: [PATCH 1/6] wip --- go/rtl/auth.go | 13 ++- go/rtl/auth_test.go | 190 +++++++++++++++++++++++++++++--------------- go/rtl/settings.go | 1 + 3 files changed, 138 insertions(+), 66 deletions(-) diff --git a/go/rtl/auth.go b/go/rtl/auth.go index e78bc5b92..94a9f1288 100644 --- a/go/rtl/auth.go +++ b/go/rtl/auth.go @@ -142,7 +142,7 @@ func (s *AuthSession) Do(result interface{}, method, ver, path string, reqPars m } // set headers - req.Header.Add("Content-Type", contentTypeHeader) + req.Header.Set("Content-Type", contentTypeHeader) if s.Config.AgentTag != "" { req.Header.Set("User-Agent", s.Config.AgentTag) @@ -151,6 +151,17 @@ func (s *AuthSession) Do(result interface{}, method, ver, path string, reqPars m req.Header.Set("User-Agent", options.AgentTag) } + if s.Config.Headers != nil { + for key, value := range s.Config.Headers { + req.Header.Set(key, value) + } + } + if options != nil && options.Headers != nil { + for key, value := range options.Headers { + req.Header.Set(key, value) + } + } + // set query params setQuery(req.URL, reqPars) diff --git a/go/rtl/auth_test.go b/go/rtl/auth_test.go index f43b550e4..6780484d1 100644 --- a/go/rtl/auth_test.go +++ b/go/rtl/auth_test.go @@ -125,70 +125,6 @@ func TestAuthSession_Do_Authorization(t *testing.T) { }) } -func TestAuthSession_Do_UserAgent(t *testing.T) { - const path = "/someMethod" - const apiVersion = "/4.0" - - t.Run("Do() sets User-Agent header with AuthSession config's AgentTag", func(t *testing.T) { - mux := http.NewServeMux() - setupApi40Login(mux, foreverValidTestToken, http.StatusOK) - server := httptest.NewServer(mux) - defer server.Close() - - mux.HandleFunc("/api"+apiVersion+path, func(w http.ResponseWriter, r *http.Request) { - userAgentHeader := r.Header.Get("User-Agent") - expectedHeader := "some-agent-tag" - if userAgentHeader != expectedHeader { - t.Errorf("User-Agent header not correct. got=%v want=%v", userAgentHeader, expectedHeader) - } - }) - - session := NewAuthSession(ApiSettings{ - BaseUrl: server.URL, - ApiVersion: apiVersion, - AgentTag: "some-agent-tag", - }) - - var r string - err := session.Do(&r, "GET", apiVersion, path, nil, nil, nil) - - if err != nil { - t.Errorf("Do() call failed: %v", err) - } - }) - - t.Run("Do() sets User-Agent header with Do's option's AgentTag, which will overwrite AuthSession config", func(t *testing.T) { - mux := http.NewServeMux() - setupApi40Login(mux, foreverValidTestToken, http.StatusOK) - server := httptest.NewServer(mux) - defer server.Close() - - mux.HandleFunc("/api"+apiVersion+path, func(w http.ResponseWriter, r *http.Request) { - userAgentHeader := r.Header.Get("User-Agent") - expectedHeader := "new-agent-tag" - if userAgentHeader != expectedHeader { - t.Errorf("User-Agent header not correct. got=%v want=%v", userAgentHeader, expectedHeader) - } - }) - - session := NewAuthSession(ApiSettings{ - BaseUrl: server.URL, - ApiVersion: apiVersion, - AgentTag: "some-agent-tag", - }) - - var r string - options := ApiSettings{ - AgentTag: "new-agent-tag", - } - err := session.Do(&r, "GET", apiVersion, path, nil, nil, &options) - - if err != nil { - t.Errorf("Do() call failed: %v", err) - } - }) -} - func TestAuthSession_Do_Parse(t *testing.T) { type stringStruct struct { Field *string `json:"field"` @@ -454,10 +390,75 @@ func TestAuthSession_Do_Parse(t *testing.T) { }) } -func TestAuthSession_Do_Content_Type(t *testing.T) { +func TestAuthSession_Do_Headers(t *testing.T) { const path = "/someMethod" const apiVersion = "/4.0" + t.Run("Do() sets custom headers if Headers is set in the AuthSession's api settings", func(t *testing.T) { + mux := http.NewServeMux() + setupApi40Login(mux, foreverValidTestToken, http.StatusOK) + server := httptest.NewServer(mux) + defer server.Close() + + mux.HandleFunc("/api"+apiVersion+path, func(w http.ResponseWriter, r *http.Request) { + headerValue1 := r.Header.Get("Key1") + headerValue2 := r.Header.Get("Key2") + + expectedHeaderValue1 := "Value1" + expectedHeaderValue2 := "Value2" + if headerValue1 != expectedHeaderValue1 || headerValue2 != expectedHeaderValue2 { + t.Errorf("Custom headers not set correctly. got=%v and %v want=%v and %v", headerValue1, headerValue2, expectedHeaderValue1, expectedHeaderValue2) + } + }) + + s := NewAuthSession(ApiSettings{ + BaseUrl: server.URL, + ApiVersion: apiVersion, + Headers: map[string]string{"Key1":"Value1","Key2":"Value2"}, + }) + + var r string + err := s.Do(&r, "GET", apiVersion, path, nil, nil, nil) + + if err != nil { + t.Errorf("Do() call failed: %v", err) + } + }) + + t.Run("Do()'s options.Headers will overwrite the Headers in the AuthSession's api settings", func(t *testing.T) { + mux := http.NewServeMux() + setupApi40Login(mux, foreverValidTestToken, http.StatusOK) + server := httptest.NewServer(mux) + defer server.Close() + + mux.HandleFunc("/api"+apiVersion+path, func(w http.ResponseWriter, r *http.Request) { + headerValue1 := r.Header.Get("Key1") + headerValue2 := r.Header.Get("Key2") + + expectedHeaderValue1 := "Value1" + expectedHeaderValue2 := "OverwriteValue2" + if headerValue1 != expectedHeaderValue1 || headerValue2 != expectedHeaderValue2 { + t.Errorf("Custom headers not set correctly. got=%v and %v want=%v and %v", headerValue1, headerValue2, expectedHeaderValue1, expectedHeaderValue2) + } + }) + + s := NewAuthSession(ApiSettings{ + BaseUrl: server.URL, + ApiVersion: apiVersion, + Headers: map[string]string{"Key1":"Value1","Key2":"Value2"}, + }) + + options := ApiSettings{ + Headers: map[string]string{"Key1":"Value1","Key2":"OverwriteValue2"}, + } + var r string + err := s.Do(&r, "GET", apiVersion, path, nil, nil, &options) + + if err != nil { + t.Errorf("Do() call failed: %v", err) + } + }) + t.Run("Do() sets Content-Type header to 'application/json' if body is json", func(t *testing.T) { mux := http.NewServeMux() setupApi40Login(mux, foreverValidTestToken, http.StatusOK) @@ -517,6 +518,65 @@ func TestAuthSession_Do_Content_Type(t *testing.T) { t.Errorf("Do() call failed: %v", err) } }) + + t.Run("Do() sets User-Agent header with AuthSession config's AgentTag", func(t *testing.T) { + mux := http.NewServeMux() + setupApi40Login(mux, foreverValidTestToken, http.StatusOK) + server := httptest.NewServer(mux) + defer server.Close() + + mux.HandleFunc("/api"+apiVersion+path, func(w http.ResponseWriter, r *http.Request) { + userAgentHeader := r.Header.Get("User-Agent") + expectedHeader := "some-agent-tag" + if userAgentHeader != expectedHeader { + t.Errorf("User-Agent header not correct. got=%v want=%v", userAgentHeader, expectedHeader) + } + }) + + session := NewAuthSession(ApiSettings{ + BaseUrl: server.URL, + ApiVersion: apiVersion, + AgentTag: "some-agent-tag", + }) + + var r string + err := session.Do(&r, "GET", apiVersion, path, nil, nil, nil) + + if err != nil { + t.Errorf("Do() call failed: %v", err) + } + }) + + t.Run("Do() sets User-Agent header with Do's option's AgentTag, which will overwrite AuthSession config", func(t *testing.T) { + mux := http.NewServeMux() + setupApi40Login(mux, foreverValidTestToken, http.StatusOK) + server := httptest.NewServer(mux) + defer server.Close() + + mux.HandleFunc("/api"+apiVersion+path, func(w http.ResponseWriter, r *http.Request) { + userAgentHeader := r.Header.Get("User-Agent") + expectedHeader := "new-agent-tag" + if userAgentHeader != expectedHeader { + t.Errorf("User-Agent header not correct. got=%v want=%v", userAgentHeader, expectedHeader) + } + }) + + session := NewAuthSession(ApiSettings{ + BaseUrl: server.URL, + ApiVersion: apiVersion, + AgentTag: "some-agent-tag", + }) + + var r string + options := ApiSettings{ + AgentTag: "new-agent-tag", + } + err := session.Do(&r, "GET", apiVersion, path, nil, nil, &options) + + if err != nil { + t.Errorf("Do() call failed: %v", err) + } + }) } func TestAuthSession_Do_Timeout(t *testing.T) { diff --git a/go/rtl/settings.go b/go/rtl/settings.go index 03242e172..f9d1334fa 100644 --- a/go/rtl/settings.go +++ b/go/rtl/settings.go @@ -19,6 +19,7 @@ type ApiSettings struct { ClientId string `ini:"client_id"` ClientSecret string `ini:"client_secret"` ApiVersion string `ini:"api_version"` + Headers map[string]string } var defaultSettings ApiSettings = ApiSettings{ From 295df8c9dc8f18f5743ff41ff8e2ee40397a0155 Mon Sep 17 00:00:00 2001 From: Jeremy Chang Date: Wed, 29 Mar 2023 23:51:52 +0000 Subject: [PATCH 2/6] wip --- go/rtl/auth_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go/rtl/auth_test.go b/go/rtl/auth_test.go index 6780484d1..245f2e8d7 100644 --- a/go/rtl/auth_test.go +++ b/go/rtl/auth_test.go @@ -518,7 +518,7 @@ func TestAuthSession_Do_Headers(t *testing.T) { t.Errorf("Do() call failed: %v", err) } }) - + t.Run("Do() sets User-Agent header with AuthSession config's AgentTag", func(t *testing.T) { mux := http.NewServeMux() setupApi40Login(mux, foreverValidTestToken, http.StatusOK) From c538d3801e0e7dd2d1e59b6a8930daffa13275f1 Mon Sep 17 00:00:00 2001 From: Jeremy Chang Date: Tue, 4 Apr 2023 23:45:14 +0000 Subject: [PATCH 3/6] wip --- go/README.md | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 go/README.md diff --git a/go/README.md b/go/README.md new file mode 100644 index 000000000..9bc4f6323 --- /dev/null +++ b/go/README.md @@ -0,0 +1,118 @@ +# Go Looker SDK + +The Go Looker SDK provides a convenient way to call your Looker instance's [Looker API](https://developers.looker.com/api/overview). The Go Looker SDK supports atleast Go version 1.17.6 and 1.16.13 (subject to change). This SDK is community supported with contributions and discussion from the Looker developer community. We strive for functional parity with our original javascript/typescript SDK. Thanks for using our Go Looker SDK! + +## Basic Usage + +Read through the code snippet below for basic SDK setup and usage. Also `git clone` this sdk-codegen repo and run the [example code](go/example/main.go). + +```go +import ( + "fmt" + "github.com/looker-open-source/sdk-codegen/go/rtl" + v4 "github.com/looker-open-source/sdk-codegen/go/sdk/v4" +) + +func main() { + // Get settings from either looker.ini file OR environment: + // looker.ini file + cfg, err := rtl.NewSettingsFromFile("path/to/looker.ini", nil) + // environment + cfg, err := rtl.NewSettingsFromEnv() + + // Create new auth session with sdk settings. + // The auth session will fetch/refresh the access + // token from your Looker instance's `login` endpoint. + session := rtl.NewAuthSession(cfg) + + // Create new instance of the Go Looker SDK + sdk := v4.NewLookerSDK(session) + + // Call the Looker API e.g. get your user's name + me, err := sdk.Me("", nil) + fmt.Printf("Your name is %s %s\n", *(me.first), *(me.last)) +} +``` + +## Advanced usage + +### Custom headers + +You can set custom headers on Looker Go SDK's requests. They can either be applied to all outgoing requests or per outgoing request. + +#### Custom headers for all requests + +Follow the example code snippet below if you want all outgoing requests to have the same custom headers. + +```go +func main() { + cfg, err := rtl.NewSettingsFromFile("path/to/looker.ini", nil) + + // Set the Headers option in the settings/config + cfg.Headers = map[string]string{ + "HeaderName1": "HeaderValue1", + "HeaderName2": "HeaderValue2", + } + + session := rtl.NewAuthSession(cfg) + sdk := v4.NewLookerSDK(session) +} +``` + +#### Custom headers per request + +Follow the example code snippet below if you want each outgoing request to have different custom headers. **These headers will overwrite any custom headers set in the SDK's settings as outlined in the previous [All Requests section](#custom-headers-for-all-requests).** + +```go +func main() { + cfg, err := rtl.NewSettingsFromFile("path/to/looker.ini", nil) + session := rtl.NewAuthSession(cfg) + sdk := v4.NewLookerSDK(session) + + // Set the headers in the options passed into the SDK method + sdk.Me("", &ApiSettings{Headings: map[string]string{ + "HeaderName1": "HeaderValue1", + "HeaderName2": "HeaderValue2", + }}) +} +``` + +### Timeout + +You can set a custom timeout (in seconds) on Looker Go SDK's requests. The timeout defaults to 120 seconds. A timeout can either be applied to all outgoing requests or per outgoing request. + +#### Timeout for all requests + +Set `timeout` in your sdk's looker.ini file then call `NewSettingsFromFile()`. +```YAML +[Looker] +timeout=60 +``` + +OR + +Set `LOOKERSDK_TIMEOUT` environment variable then call `NewSettingsFromEnv()`. +```bash +LOOKERSDK_TIMEOUT=60 +``` + +#### Timeout per request + +Follow the example code snippet below if you want each outgoing request to have a different timeout. **The timeout will overwrite the timeout set in the SDK's settings as outlined in the previous [All Requests section](#timeout-for-all-requests).** + +```go +import "context" + +func main() { + cfg, err := rtl.NewSettingsFromFile("path/to/looker.ini", nil) + session := rtl.NewAuthSession(cfg) + sdk := v4.NewLookerSDK(session) + + // Set the timeout in the options passed into the SDK method + me, err := sdk.Me("", &ApiSettings{Timeout: 60}) + + if errors.Is(err, context.DeadlineExceeded) { + // Timeout exceeded + } +} +``` From 36e8a028d141bb53537bdf8621aefff45102a491 Mon Sep 17 00:00:00 2001 From: Jeremy Chang Date: Tue, 4 Apr 2023 23:49:46 +0000 Subject: [PATCH 4/6] wip --- go/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go/README.md b/go/README.md index 9bc4f6323..f2f845ecc 100644 --- a/go/README.md +++ b/go/README.md @@ -4,7 +4,7 @@ The Go Looker SDK provides a convenient way to call your Looker instance's [Look ## Basic Usage -Read through the code snippet below for basic SDK setup and usage. Also `git clone` this sdk-codegen repo and run the [example code](go/example/main.go). +Example code snippet below for basic SDK setup and usage. Also `git clone` this sdk-codegen repo and run the [example code](go/example/main.go). ```go import ( From fd2887faecbb44daf3331ceb11052fe3beda54f1 Mon Sep 17 00:00:00 2001 From: Jeremy Chang Date: Wed, 5 Apr 2023 00:53:40 +0000 Subject: [PATCH 5/6] fix readme typo --- go/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go/README.md b/go/README.md index f2f845ecc..ea08c8504 100644 --- a/go/README.md +++ b/go/README.md @@ -1,6 +1,6 @@ # Go Looker SDK -The Go Looker SDK provides a convenient way to call your Looker instance's [Looker API](https://developers.looker.com/api/overview). The Go Looker SDK supports atleast Go version 1.17.6 and 1.16.13 (subject to change). This SDK is community supported with contributions and discussion from the Looker developer community. We strive for functional parity with our original javascript/typescript SDK. Thanks for using our Go Looker SDK! +The Go Looker SDK provides a convenient way to call your Looker instance's [Looker API](https://developers.looker.com/api/overview). The Go Looker SDK supports at least Go version 1.17.6 and 1.16.13 (subject to change). This SDK is community supported with contributions and discussion from the Looker developer community. We strive for functional parity with our original javascript/typescript SDK. Thanks for using our Go Looker SDK! ## Basic Usage From 513d940c758fecf2b2323ffa8f4ad311a74a0820 Mon Sep 17 00:00:00 2001 From: Jeremy Chang Date: Wed, 5 Apr 2023 01:35:01 +0000 Subject: [PATCH 6/6] fixed readme spaces --- go/README.md | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/go/README.md b/go/README.md index ea08c8504..142e8148f 100644 --- a/go/README.md +++ b/go/README.md @@ -9,28 +9,28 @@ Example code snippet below for basic SDK setup and usage. Also `git clone` this ```go import ( "fmt" - "github.com/looker-open-source/sdk-codegen/go/rtl" - v4 "github.com/looker-open-source/sdk-codegen/go/sdk/v4" + "github.com/looker-open-source/sdk-codegen/go/rtl" + v4 "github.com/looker-open-source/sdk-codegen/go/sdk/v4" ) func main() { // Get settings from either looker.ini file OR environment: - // looker.ini file - cfg, err := rtl.NewSettingsFromFile("path/to/looker.ini", nil) + // looker.ini file + cfg, err := rtl.NewSettingsFromFile("path/to/looker.ini", nil) // environment cfg, err := rtl.NewSettingsFromEnv() - // Create new auth session with sdk settings. + // Create new auth session with sdk settings. // The auth session will fetch/refresh the access // token from your Looker instance's `login` endpoint. session := rtl.NewAuthSession(cfg) // Create new instance of the Go Looker SDK - sdk := v4.NewLookerSDK(session) + sdk := v4.NewLookerSDK(session) // Call the Looker API e.g. get your user's name me, err := sdk.Me("", nil) - fmt.Printf("Your name is %s %s\n", *(me.first), *(me.last)) + fmt.Printf("Your name is %s %s\n", *(me.first), *(me.last)) } ``` @@ -46,7 +46,7 @@ Follow the example code snippet below if you want all outgoing requests to have ```go func main() { - cfg, err := rtl.NewSettingsFromFile("path/to/looker.ini", nil) + cfg, err := rtl.NewSettingsFromFile("path/to/looker.ini", nil) // Set the Headers option in the settings/config cfg.Headers = map[string]string{ @@ -55,7 +55,7 @@ func main() { } session := rtl.NewAuthSession(cfg) - sdk := v4.NewLookerSDK(session) + sdk := v4.NewLookerSDK(session) } ``` @@ -65,9 +65,9 @@ Follow the example code snippet below if you want each outgoing request to have ```go func main() { - cfg, err := rtl.NewSettingsFromFile("path/to/looker.ini", nil) + cfg, err := rtl.NewSettingsFromFile("path/to/looker.ini", nil) session := rtl.NewAuthSession(cfg) - sdk := v4.NewLookerSDK(session) + sdk := v4.NewLookerSDK(session) // Set the headers in the options passed into the SDK method sdk.Me("", &ApiSettings{Headings: map[string]string{ @@ -104,9 +104,9 @@ Follow the example code snippet below if you want each outgoing request to have import "context" func main() { - cfg, err := rtl.NewSettingsFromFile("path/to/looker.ini", nil) + cfg, err := rtl.NewSettingsFromFile("path/to/looker.ini", nil) session := rtl.NewAuthSession(cfg) - sdk := v4.NewLookerSDK(session) + sdk := v4.NewLookerSDK(session) // Set the timeout in the options passed into the SDK method me, err := sdk.Me("", &ApiSettings{Timeout: 60})