Skip to content

Commit

Permalink
fix(go-sdk): do not call read auth model on batchcheck and write
Browse files Browse the repository at this point in the history
  • Loading branch information
ewanharris committed Apr 30, 2024
1 parent cfb7d36 commit 0061ad7
Show file tree
Hide file tree
Showing 2 changed files with 192 additions and 124 deletions.
50 changes: 24 additions & 26 deletions config/clients/go/template/client/client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -451,19 +451,6 @@ func (client *{{appShortName}}Client) getAuthorizationModelId(authorizationModel
return &modelId, nil
}

// helper function to validate the connection (i.e., get token)
func (client *{{appShortName}}Client) checkValidApiConnection(ctx _context.Context, authorizationModelId *string) error {
if authorizationModelId != nil && *authorizationModelId != "" {
_, _, err := client.{{appShortName}}Api.ReadAuthorizationModel(ctx, *authorizationModelId).Execute()
return err
} else {
_, err := client.ReadAuthorizationModels(ctx).Options(ClientReadAuthorizationModelsOptions{
PageSize: fgaSdk.PtrInt32(1),
}).Execute()
return err
}
}

/* Stores */

// / ListStores
Expand Down Expand Up @@ -1392,17 +1379,13 @@ func (client *{{appShortName}}Client) WriteExecute(request SdkClientWriteRequest
}

writeGroup, ctx := errgroup.WithContext(request.GetContext())
err = client.checkValidApiConnection(ctx, authorizationModelId)
if err != nil {
return nil, err
}

writeGroup.SetLimit(int(maxParallelReqs))
writeResponses := make([]ClientWriteResponse, len(writeChunks))
for index, writeBody := range writeChunks {
index, writeBody := index, writeBody
writeGroup.Go(func() error {
singleResponse, _ := client.WriteExecute(&SdkClientWriteRequest{
singleResponse, err := client.WriteExecute(&SdkClientWriteRequest{
ctx: ctx,
Client: client,
body: &ClientWriteRequest{
Expand All @@ -1413,13 +1396,21 @@ func (client *{{appShortName}}Client) WriteExecute(request SdkClientWriteRequest
},
})

if _, ok := err.(fgaSdk.FgaApiAuthenticationError); ok {
return err
}

writeResponses[index] = *singleResponse

return nil
})
}

_ = writeGroup.Wait()
err = writeGroup.Wait()
// If an error was returned then it will be an authentication error so we want to return
if err != nil {
return &response, err
}

var deleteChunkSize = int(maxPerChunk)
var deleteChunks [][]ClientTupleKeyWithoutCondition
Expand All @@ -1437,7 +1428,7 @@ func (client *{{appShortName}}Client) WriteExecute(request SdkClientWriteRequest
for index, deleteBody := range deleteChunks {
index, deleteBody := index, deleteBody
deleteGroup.Go(func() error {
singleResponse, _ := client.WriteExecute(&SdkClientWriteRequest{
singleResponse, err := client.WriteExecute(&SdkClientWriteRequest{
ctx: ctx,
Client: client,
body: &ClientWriteRequest{
Expand All @@ -1448,13 +1439,21 @@ func (client *{{appShortName}}Client) WriteExecute(request SdkClientWriteRequest
},
})

if _, ok := err.(fgaSdk.FgaApiAuthenticationError); ok {
return err
}

deleteResponses[index] = *singleResponse

return nil
})
}

_ = deleteGroup.Wait()
err = deleteGroup.Wait()
if err != nil {
// If an error was returned then it will be an authentication error so we want to return
return &response, err
}

for _, writeResponse := range writeResponses {
for _, writeSingleResponse := range writeResponse.Writes {
Expand Down Expand Up @@ -1797,11 +1796,6 @@ func (client *{{appShortName}}Client) BatchCheckExecute(request SdkClientBatchCh
return nil, err
}

group.Go(func() error {
// if the connection is probelmatic, we will return error to the overall
// response rather than individual response
return client.checkValidApiConnection(ctx, authorizationModelId)
})
for index, checkBody := range *request.GetBody() {
index, checkBody := index, checkBody
group.Go(func() error {
Expand All @@ -1814,6 +1808,10 @@ func (client *{{appShortName}}Client) BatchCheckExecute(request SdkClientBatchCh
},
})

if _, ok := err.(fgaSdk.FgaApiAuthenticationError); ok {
return err
}

response[index] = ClientBatchCheckSingleResponse{
Request: checkBody,
ClientCheckResponse: *singleResponse,
Expand Down
Loading

0 comments on commit 0061ad7

Please sign in to comment.