From 31686b37bfe2cda6f88cec11a88bf67763959e9f Mon Sep 17 00:00:00 2001 From: Philipp Reiter Date: Mon, 27 Jan 2025 16:02:50 +0000 Subject: [PATCH 1/2] fix(go-sdk): Do not ignore request level storeId override in non-transactional write --- config/clients/go/CHANGELOG.md.mustache | 3 +- .../go/template/client/client.mustache | 2 ++ .../go/template/client/client_test.mustache | 32 +++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/config/clients/go/CHANGELOG.md.mustache b/config/clients/go/CHANGELOG.md.mustache index 72dfc26e3..b41312504 100644 --- a/config/clients/go/CHANGELOG.md.mustache +++ b/config/clients/go/CHANGELOG.md.mustache @@ -2,7 +2,8 @@ ## [Unreleased](https://github.com/openfga/go-sdk/compare/v{{packageVersion}}...HEAD) -- feat: add support for `start_time` parameter in `ReadChanges` endpoint +- feat: add support for `start_time` parameter in `ReadChanges` endpoint (#158) +- fix: correctly set request level storeId in non-transactional write (#162) ## v0.6.3 diff --git a/config/clients/go/template/client/client.mustache b/config/clients/go/template/client/client.mustache index 8f81dc8a0..851e017ee 100644 --- a/config/clients/go/template/client/client.mustache +++ b/config/clients/go/template/client/client.mustache @@ -1593,6 +1593,7 @@ func (client *{{appShortName}}Client) WriteExecute(request SdkClientWriteRequest }, options: &ClientWriteOptions{ AuthorizationModelId: authorizationModelId, + StoreId: request.GetStoreIdOverride(), }, }) @@ -1636,6 +1637,7 @@ func (client *{{appShortName}}Client) WriteExecute(request SdkClientWriteRequest }, options: &ClientWriteOptions{ AuthorizationModelId: authorizationModelId, + StoreId: request.GetStoreIdOverride(), }, }) diff --git a/config/clients/go/template/client/client_test.mustache b/config/clients/go/template/client/client_test.mustache index a9430fff9..452bc306d 100644 --- a/config/clients/go/template/client/client_test.mustache +++ b/config/clients/go/template/client/client_test.mustache @@ -1481,6 +1481,38 @@ func Test{{appShortName}}Client(t *testing.T) { t.Fatalf("%v", err) } } + + // store can be overridden + storeOverrideOptions := ClientWriteOptions{ + AuthorizationModelId: openfga.PtrString(authModelId), + Transaction: &TransactionOptions{ + Disable: true, + MaxParallelRequests: 5, + MaxPerChunk: 1, + }, + StoreId: openfga.PtrString("7777HCE4YVKPQEKZQHT2R89MQV"), + } + httpmock.Reset() + httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s/stores/%s/%s", fgaClient.GetConfig().ApiUrl, *storeOverrideOptions.StoreId, test.RequestPath), + func(req *http.Request) (*http.Response, error) { + resp, err := httpmock.NewJsonResponse(test.ResponseStatus, expectedResponse) + if err != nil { + return httpmock.NewStringResponse(http.StatusInternalServerError, ""), nil + } + return resp, nil + }, + ) + data, err = fgaClient.Write(context.Background()).Body(requestBody).Options(storeOverrideOptions).Execute() + if err != nil { + t.Fatalf("%v", data) + } + + for index := 0; index < len(data.Writes); index++ { + response := data.Writes[index] + if response.Error != nil { + t.Fatalf("OpenFgaClient.%v()|%d/ %v", test.Name, index, response.Error) + } + } }) t.Run("Write with invalid auth", func(t *testing.T) { From 36997b094b9dcb9e0a180e61960fe072baa49879 Mon Sep 17 00:00:00 2001 From: Jim Anderson Date: Mon, 27 Jan 2025 16:15:49 +0000 Subject: [PATCH 2/2] fix(go-sdk): api client should set default telemetry if not specified --- config/clients/go/CHANGELOG.md.mustache | 1 + config/clients/go/config.overrides.json | 4 +++ .../clients/go/template/api_client.mustache | 3 +++ .../go/template/api_client_test.mustache | 25 +++++++++++++++++++ 4 files changed, 33 insertions(+) create mode 100644 config/clients/go/template/api_client_test.mustache diff --git a/config/clients/go/CHANGELOG.md.mustache b/config/clients/go/CHANGELOG.md.mustache index b41312504..8a0012355 100644 --- a/config/clients/go/CHANGELOG.md.mustache +++ b/config/clients/go/CHANGELOG.md.mustache @@ -4,6 +4,7 @@ - feat: add support for `start_time` parameter in `ReadChanges` endpoint (#158) - fix: correctly set request level storeId in non-transactional write (#162) +- fix: api client should set default telemetry if not specified (#160) ## v0.6.3 diff --git a/config/clients/go/config.overrides.json b/config/clients/go/config.overrides.json index 00c9c33c2..9f130c2b8 100644 --- a/config/clients/go/config.overrides.json +++ b/config/clients/go/config.overrides.json @@ -44,6 +44,10 @@ "destinationFilename": "client/errors.go", "templateType": "SupportingFiles" }, + "api_client_test.mustache": { + "destinationFilename": "api_client_test.go", + "templateType": "SupportingFiles" + }, "api_client.mustache": { "destinationFilename": "api_client.go", "templateType": "SupportingFiles" diff --git a/config/clients/go/template/api_client.mustache b/config/clients/go/template/api_client.mustache index d41caa3db..218411f14 100644 --- a/config/clients/go/template/api_client.mustache +++ b/config/clients/go/template/api_client.mustache @@ -65,6 +65,9 @@ type service struct { // NewAPIClient creates a new API client. Requires a userAgent string describing your application. // optionally a custom http.Client to allow for advanced features such as caching. func NewAPIClient(cfg *Configuration) *APIClient { + if cfg.Telemetry == nil { + cfg.Telemetry = telemetry.DefaultTelemetryConfiguration() + } if cfg.HTTPClient == nil { if cfg.Credentials == nil { cfg.HTTPClient = http.DefaultClient diff --git a/config/clients/go/template/api_client_test.mustache b/config/clients/go/template/api_client_test.mustache new file mode 100644 index 000000000..b5f42664c --- /dev/null +++ b/config/clients/go/template/api_client_test.mustache @@ -0,0 +1,25 @@ +{{>partial_header}} +package {{packageName}} + + +import ( + "{{gitHost}}/{{gitUserId}}/{{gitRepoId}}/telemetry" + "net/http" + "testing" + "time" +) + +func TestApiClientCreatedWithDefaultTelemetry(t *testing.T) { + cfg := Configuration{ + HTTPClient: &http.Client{Timeout: 10 * time.Second}, + ApiUrl: "http://localhost:8080/", + } + _ = NewAPIClient(&cfg) + + telemetry1 := telemetry.Get(telemetry.TelemetryFactoryParameters{Configuration: cfg.Telemetry}) + telemetry2 := telemetry.Get(telemetry.TelemetryFactoryParameters{Configuration: cfg.Telemetry}) + + if telemetry1 != telemetry2 { + t.Fatalf("Telemetry instance should be the same") + } +} \ No newline at end of file