Skip to content

Commit

Permalink
Port Go and Java changes (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
ewanharris authored Apr 30, 2024
2 parents 051c804 + 0061ad7 commit 6935d1b
Show file tree
Hide file tree
Showing 7 changed files with 462 additions and 306 deletions.
4 changes: 2 additions & 2 deletions config/clients/go/template/README_calling_api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ options := ClientWriteOptions{
// You can rely on the model id set in the configuration or override it for this specific request
AuthorizationModelId: {{packageName}}.PtrString("01GAHCE4YVKPQEKZQHT2R89MQV"),
}
data, err := fgaClient.Write(context.Background()).Body(requestBody).Options(options).Execute()
data, err := fgaClient.Write(context.Background()).Body(body).Options(options).Execute()
```

Convenience `WriteTuples` and `DeleteTuples` methods are also available.
Expand Down Expand Up @@ -314,7 +314,7 @@ options := ClientWriteOptions{
MaxPerChunk: 1, // Maximum number of requests to be sent in a transaction in a particular chunk
},
}
data, err := fgaClient.Write(context.Background()).Body(requestBody).Options(options).Execute()
data, err := fgaClient.Write(context.Background()).Body(body).Options(options).Execute()

// data.Writes = [{
// TupleKey: { User, Relation, Object },
Expand Down
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 6935d1b

Please sign in to comment.