Skip to content

Commit e72797b

Browse files
authored
Support passing name filter to ListStores (#213)
* Add filter by name for list stores * fix * Make sure to pass along the query parameter
1 parent 2fb61bd commit e72797b

5 files changed

Lines changed: 88 additions & 8 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ Get a paginated list of stores.
238238
options := ClientListStoresOptions{
239239
PageSize: openfga.PtrInt32(10),
240240
ContinuationToken: openfga.PtrString("..."),
241+
Name: openfga.PtrString("FGA Demo Store"), // Optional: filter stores by name
241242
}
242243
stores, err := fgaClient.ListStores(context.Background()).Options(options).Execute()
243244

api_open_fga.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2501,17 +2501,24 @@ type ApiListStoresRequest struct {
25012501
ApiService OpenFgaApi
25022502
pageSize *int32
25032503
continuationToken *string
2504+
name *string
25042505
}
25052506

25062507
func (r ApiListStoresRequest) PageSize(pageSize int32) ApiListStoresRequest {
25072508
r.pageSize = &pageSize
25082509
return r
25092510
}
2511+
25102512
func (r ApiListStoresRequest) ContinuationToken(continuationToken string) ApiListStoresRequest {
25112513
r.continuationToken = &continuationToken
25122514
return r
25132515
}
25142516

2517+
func (r ApiListStoresRequest) Name(name string) ApiListStoresRequest {
2518+
r.name = &name
2519+
return r
2520+
}
2521+
25152522
func (r ApiListStoresRequest) Execute() (ListStoresResponse, *http.Response, error) {
25162523
return r.ApiService.ListStoresExecute(r)
25172524
}
@@ -2558,6 +2565,9 @@ func (a *OpenFgaApiService) ListStoresExecute(r ApiListStoresRequest) (ListStore
25582565
if r.continuationToken != nil {
25592566
localVarQueryParams.Add("continuation_token", parameterToString(*r.continuationToken, ""))
25602567
}
2568+
if r.name != nil {
2569+
localVarQueryParams.Add("name", parameterToString(*r.name, ""))
2570+
}
25612571
// to determine the Content-Type header
25622572
localVarHTTPContentTypes := []string{}
25632573

client/client.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,13 @@ func getContinuationTokenFromRequest(options *ClientPaginationOptions) *string {
181181
return options.ContinuationToken
182182
}
183183

184+
func getNameFromRequest(options *ClientListStoresOptions) *string {
185+
if options == nil {
186+
return nil
187+
}
188+
return options.Name
189+
}
190+
184191
type SdkClient interface {
185192
/* Stores */
186193

@@ -579,6 +586,7 @@ type SdkClientListStoresRequestInterface interface {
579586
type ClientListStoresOptions struct {
580587
PageSize *int32 `json:"page_size,omitempty"`
581588
ContinuationToken *string `json:"continuation_token,omitempty"`
589+
Name *string `json:"name,omitempty"`
582590
}
583591

584592
type ClientListStoresResponse = fgaSdk.ListStoresResponse
@@ -602,13 +610,17 @@ func (request *SdkClientListStoresRequest) GetOptions() *ClientListStoresOptions
602610

603611
func (client *OpenFgaClient) ListStoresExecute(request SdkClientListStoresRequestInterface) (*ClientListStoresResponse, error) {
604612
req := client.OpenFgaApi.ListStores(request.GetContext())
605-
pageSize := getPageSizeFromRequest((*ClientPaginationOptions)(request.GetOptions()))
606-
if pageSize != nil {
607-
req = req.PageSize(*pageSize)
608-
}
609-
continuationToken := getContinuationTokenFromRequest((*ClientPaginationOptions)(request.GetOptions()))
610-
if continuationToken != nil {
611-
req = req.ContinuationToken(*continuationToken)
613+
options := request.GetOptions()
614+
if options != nil {
615+
if options.PageSize != nil {
616+
req = req.PageSize(*options.PageSize)
617+
}
618+
if options.ContinuationToken != nil {
619+
req = req.ContinuationToken(*options.ContinuationToken)
620+
}
621+
if options.Name != nil {
622+
req = req.Name(*options.Name)
623+
}
612624
}
613625
data, _, err := req.Execute()
614626
if err != nil {

client/client_test.go

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323

2424
"github.com/jarcoal/httpmock"
2525

26-
"github.com/openfga/go-sdk"
26+
openfga "github.com/openfga/go-sdk"
2727
. "github.com/openfga/go-sdk/client"
2828
)
2929

@@ -218,6 +218,53 @@ func TestOpenFgaClient(t *testing.T) {
218218
}
219219
})
220220

221+
t.Run("ListStoresWithNameFilter", func(t *testing.T) {
222+
filteredStoreName := "Filtered Store"
223+
test := TestDefinition{
224+
Name: "ListStoresWithNameFilter",
225+
JsonResponse: fmt.Sprintf(`{"stores":[{"id":"01GXSA8YR785C4FYS3C0RTG7B1","name":"%s","created_at":"2023-01-01T23:23:23.000000000Z","updated_at":"2023-01-01T23:23:23.000000000Z"}]}`, filteredStoreName),
226+
ResponseStatus: http.StatusOK,
227+
Method: http.MethodGet,
228+
}
229+
230+
var expectedResponse openfga.ListStoresResponse
231+
if err := json.Unmarshal([]byte(test.JsonResponse), &expectedResponse); err != nil {
232+
t.Fatalf("%v", err)
233+
}
234+
235+
httpmock.Activate()
236+
defer httpmock.DeactivateAndReset()
237+
httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s/stores", fgaClient.GetConfig().ApiUrl),
238+
func(req *http.Request) (*http.Response, error) {
239+
// Verify that the name parameter is included in the query
240+
if req.URL.Query().Get("name") != filteredStoreName {
241+
t.Fatalf("expected name parameter to be '%s', got %s", filteredStoreName, req.URL.Query().Get("name"))
242+
}
243+
resp, err := httpmock.NewJsonResponse(test.ResponseStatus, expectedResponse)
244+
if err != nil {
245+
return httpmock.NewStringResponse(http.StatusInternalServerError, ""), nil
246+
}
247+
return resp, nil
248+
},
249+
)
250+
251+
options := ClientListStoresOptions{
252+
Name: openfga.PtrString(filteredStoreName),
253+
}
254+
got, err := fgaClient.ListStores(context.Background()).Options(options).Execute()
255+
if err != nil {
256+
t.Fatalf("%v", err)
257+
}
258+
259+
if len(got.Stores) != 1 {
260+
t.Fatalf("expected stores of length 1, got %v", len(got.Stores))
261+
}
262+
263+
if got.Stores[0].Name != filteredStoreName {
264+
t.Fatalf("expected store name to be '%s', got %s", filteredStoreName, got.Stores[0].Name)
265+
}
266+
})
267+
221268
t.Run("CreateStore", func(t *testing.T) {
222269
test := TestDefinition{
223270
Name: "CreateStore",

example/example1/example1.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ func mainInner() error {
6868
}
6969
fmt.Printf("Stores Count: %d\n", len(stores.Stores))
7070

71+
// ListStores with name filter
72+
fmt.Println("Listing Stores with name filter")
73+
storesWithFilter, err := fgaClient.ListStores(ctx).Options(client.ClientListStoresOptions{
74+
Name: openfga.PtrString("Test Store"),
75+
}).Execute()
76+
if err != nil {
77+
return err
78+
}
79+
fmt.Printf("Stores with name filter Count: %d\n", len(storesWithFilter.Stores))
80+
7181
// GetStore
7282
fmt.Println("Getting Current Store")
7383
currentStore, err := fgaClient.GetStore(ctx).Execute()

0 commit comments

Comments
 (0)