Skip to content

Commit 9b6f886

Browse files
ryanpqovindu-a
andauthored
feat: support start time parameter on read changes (#158)
* fix : change default retry policy * feat-support-start_time-parameter-on-read_changes * Update Changelog - Add default retry change to changelog - Add PR reference to new changelog entries * chore: update changelog adding credit on retry policy change --------- Co-authored-by: Ovindu Atukorala <[email protected]>
1 parent 2adebcc commit 9b6f886

22 files changed

+616
-26
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## [Unreleased](https://github.com/openfga/go-sdk/compare/v0.6.3...HEAD)
4+
5+
- feat: add support for `start_time` parameter in `ReadChanges` endpoint (#158)
6+
- feat: change default retry policy to `3` from `15` - thanks @ovindu-a (#158)
7+
38
## v0.6.3
49

510
### [0.6.3](https://github.com/openfga/go-sdk/compare/v0.6.2...v0.6.3) (2024-10-22)

README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ to update `go.mod` and `go.sum` if you are using them.
102102

103103
We strongly recommend you initialize the `OpenFgaClient` only once and then re-use it throughout your app, otherwise you will incur the cost of having to re-initialize multiple times or at every request, the cost of reduced connection pooling and re-use, and would be particularly costly in the client credentials flow, as that flow will be preformed on every request.
104104

105-
> The `openfgaClient` will by default retry API requests up to 15 times on 429 and 5xx errors.
105+
> The `openfgaClient` will by default retry API requests up to 3 times on 429 and 5xx errors.
106106
107107
#### No Credentials
108108

@@ -439,6 +439,7 @@ options := ClientReadChangesOptions{
439439
ContinuationToken: openfga.PtrString("eyJwayI6IkxBVEVTVF9OU0NPTkZJR19hdXRoMHN0b3JlIiwic2siOiIxem1qbXF3MWZLZExTcUoyN01MdTdqTjh0cWgifQ=="),
440440
// You can rely on the store id set in the configuration or override it for this specific request
441441
StoreId: openfga.PtrString("01FQH7V8BEG3GPQW93KTRFR8JB"),
442+
StartTime: openfga.PtrString("2022-01-01T00:00:00Z"),
442443
}
443444
data, err := fgaClient.ReadChanges(context.Background()).Body(body).Options(options).Execute()
444445

@@ -899,7 +900,7 @@ data, err := fgaClient.WriteAssertions(context.Background()).
899900

900901
### Retries
901902

902-
If a network request fails with a 429 or 5xx error from the server, the SDK will automatically retry the request up to 15 times with a minimum wait time of 100 milliseconds between each attempt.
903+
If a network request fails with a 429 or 5xx error from the server, the SDK will automatically retry the request up to 3 times with a minimum wait time of 100 milliseconds between each attempt.
903904

904905
To customize this behavior, create an `openfga.RetryParams` struct and assign values to the `MaxRetry` and `MinWaitInMs` fields. `MaxRetry` determines the maximum number of retries (up to 15), while `MinWaitInMs` sets the minimum wait time between retries in milliseconds.
905906

@@ -959,6 +960,7 @@ Class | Method | HTTP request | Description
959960
- [Any](docs/Any.md)
960961
- [Assertion](docs/Assertion.md)
961962
- [AssertionTupleKey](docs/AssertionTupleKey.md)
963+
- [AuthErrorCode](docs/AuthErrorCode.md)
962964
- [AuthorizationModel](docs/AuthorizationModel.md)
963965
- [CheckRequest](docs/CheckRequest.md)
964966
- [CheckRequestTupleKey](docs/CheckRequestTupleKey.md)
@@ -977,6 +979,7 @@ Class | Method | HTTP request | Description
977979
- [ExpandRequestTupleKey](docs/ExpandRequestTupleKey.md)
978980
- [ExpandResponse](docs/ExpandResponse.md)
979981
- [FgaObject](docs/FgaObject.md)
982+
- [ForbiddenResponse](docs/ForbiddenResponse.md)
980983
- [GetStoreResponse](docs/GetStoreResponse.md)
981984
- [InternalErrorCode](docs/InternalErrorCode.md)
982985
- [InternalErrorMessageResponse](docs/InternalErrorMessageResponse.md)

api_open_fga.go

+44-6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type OpenFgaApi interface {
4040
A `contextual_tuples` object may also be included in the body of the request. This object contains one field `tuple_keys`, which is an array of tuple keys. Each of these tuples may have an associated `condition`.
4141
You may also provide an `authorization_model_id` in the body. This will be used to assert that the input `tuple_key` is valid for the model specified. If not specified, the assertion will be made against the latest authorization model ID. It is strongly recommended to specify authorization model id for better performance.
4242
You may also provide a `context` object that will be used to evaluate the conditioned tuples in the system. It is strongly recommended to provide a value for all the input parameters of all the conditions, to ensure that all tuples be evaluated correctly.
43+
By default, the Check API caches results for a short time to optimize performance. You may specify a value of `HIGHER_CONSISTENCY` for the optional `consistency` parameter in the body to inform the server that higher conisistency is preferred at the expense of increased latency. Consideration should be given to the increased latency if requesting higher consistency.
4344
The response will return whether the relationship exists in the field `allowed`.
4445
4546
Some exceptions apply, but in general, if a Check API responds with `{allowed: true}`, then you can expect the equivalent ListObjects query to return the object, and viceversa.
@@ -139,6 +140,18 @@ type OpenFgaApi interface {
139140
}
140141
```
141142
will return `{ "allowed": true }`, even though a specific user of the userset `group:finance#member` does not have the `reader` relationship with the given object.
143+
### Requesting higher consistency
144+
By default, the Check API caches results for a short time to optimize performance. You may request higher consistency to inform the server that higher consistency should be preferred at the expense of increased latency. Care should be taken when requesting higher consistency due to the increased latency.
145+
```json
146+
{
147+
"tuple_key": {
148+
"user": "group:finance#member",
149+
"relation": "reader",
150+
"object": "document:2021-budget"
151+
},
152+
"consistency": "HIGHER_CONSISTENCY"
153+
}
154+
```
142155
143156
* @param ctx _context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
144157
* @param storeId
@@ -265,6 +278,7 @@ type OpenFgaApi interface {
265278
An `authorization_model_id` may be specified in the body. If it is not specified, the latest authorization model ID will be used. It is strongly recommended to specify authorization model id for better performance.
266279
You may also specify `contextual_tuples` that will be treated as regular tuples. Each of these tuples may have an associated `condition`.
267280
You may also provide a `context` object that will be used to evaluate the conditioned tuples in the system. It is strongly recommended to provide a value for all the input parameters of all the conditions, to ensure that all tuples be evaluated correctly.
281+
By default, the Check API caches results for a short time to optimize performance. You may specify a value of `HIGHER_CONSISTENCY` for the optional `consistency` parameter in the body to inform the server that higher conisistency is preferred at the expense of increased latency. Consideration should be given to the increased latency if requesting higher consistency.
268282
The response will contain the related objects in an array in the "objects" field of the response and they will be strings in the object format `<type>:<id>` (e.g. "document:roadmap").
269283
The number of objects in the response array will be limited by the execution timeout specified in the flag OPENFGA_LIST_OBJECTS_DEADLINE and by the upper bound specified in the flag OPENFGA_LIST_OBJECTS_MAX_RESULTS, whichever is hit first.
270284
The objects given will not be sorted, and therefore two identical calls can give a given different set of objects.
@@ -304,7 +318,7 @@ type OpenFgaApi interface {
304318
You may also specify `contextual_tuples` that will be treated as regular tuples. Each of these tuples may have an associated `condition`.
305319
You may also provide a `context` object that will be used to evaluate the conditioned tuples in the system. It is strongly recommended to provide a value for all the input parameters of all the conditions, to ensure that all tuples be evaluated correctly.
306320
The response will contain the related users in an array in the "users" field of the response. These results may include specific objects, usersets
307-
or type-bound public access. Each of these types of results is encoded in its own type and not represented as a string.In cases where a type-bound public acces result is returned (e.g. `user:*`), it cannot be inferred that all subjects
321+
or type-bound public access. Each of these types of results is encoded in its own type and not represented as a string.In cases where a type-bound public access result is returned (e.g. `user:*`), it cannot be inferred that all subjects
308322
of that type have a relation to the object; it is possible that negations exist and checks should still be queried
309323
on individual subjects to ensure access to that document.The number of users in the response array will be limited by the execution timeout specified in the flag OPENFGA_LIST_USERS_DEADLINE and by the upper bound specified in the flag OPENFGA_LIST_USERS_MAX_RESULTS, whichever is hit first.
310324
The returned users will not be sorted, and therefore two identical calls may yield different sets of users.
@@ -435,7 +449,7 @@ type OpenFgaApi interface {
435449

436450
/*
437451
* ReadAssertions Read assertions for an authorization model ID
438-
* The ReadAssertions API will return, for a given authorization model id, all the assertions stored for it. An assertion is an object that contains a tuple key, and the expectation of whether a call to the Check API of that tuple key will return true or false.
452+
* The ReadAssertions API will return, for a given authorization model id, all the assertions stored for it.
439453
* @param ctx _context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
440454
* @param storeId
441455
* @param authorizationModelId
@@ -629,7 +643,7 @@ type OpenFgaApi interface {
629643

630644
/*
631645
* WriteAssertions Upsert assertions for an authorization model ID
632-
* The WriteAssertions API will upsert new assertions for an authorization model id, or overwrite the existing ones. An assertion is an object that contains a tuple key, and the expectation of whether a call to the Check API of that tuple key will return true or false.
646+
* The WriteAssertions API will upsert new assertions for an authorization model id, or overwrite the existing ones. An assertion is an object that contains a tuple key, the expectation of whether a call to the Check API of that tuple key will return true or false, and optionally a list of contextual tuples.
633647
* @param ctx _context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
634648
* @param storeId
635649
* @param authorizationModelId
@@ -728,6 +742,7 @@ To arrive at a result, the API uses: an authorization model, explicit tuples wri
728742
A `contextual_tuples` object may also be included in the body of the request. This object contains one field `tuple_keys`, which is an array of tuple keys. Each of these tuples may have an associated `condition`.
729743
You may also provide an `authorization_model_id` in the body. This will be used to assert that the input `tuple_key` is valid for the model specified. If not specified, the assertion will be made against the latest authorization model ID. It is strongly recommended to specify authorization model id for better performance.
730744
You may also provide a `context` object that will be used to evaluate the conditioned tuples in the system. It is strongly recommended to provide a value for all the input parameters of all the conditions, to ensure that all tuples be evaluated correctly.
745+
By default, the Check API caches results for a short time to optimize performance. You may specify a value of `HIGHER_CONSISTENCY` for the optional `consistency` parameter in the body to inform the server that higher conisistency is preferred at the expense of increased latency. Consideration should be given to the increased latency if requesting higher consistency.
731746
The response will return whether the relationship exists in the field `allowed`.
732747
733748
Some exceptions apply, but in general, if a Check API responds with `{allowed: true}`, then you can expect the equivalent ListObjects query to return the object, and viceversa.
@@ -845,6 +860,20 @@ the following query
845860
846861
```
847862
will return `{ "allowed": true }`, even though a specific user of the userset `group:finance#member` does not have the `reader` relationship with the given object.
863+
### Requesting higher consistency
864+
By default, the Check API caches results for a short time to optimize performance. You may request higher consistency to inform the server that higher consistency should be preferred at the expense of increased latency. Care should be taken when requesting higher consistency due to the increased latency.
865+
```json
866+
867+
{
868+
"tuple_key": {
869+
"user": "group:finance#member",
870+
"relation": "reader",
871+
"object": "document:2021-budget"
872+
},
873+
"consistency": "HIGHER_CONSISTENCY"
874+
}
875+
876+
```
848877
849878
- @param ctx _context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
850879
- @param storeId
@@ -2290,6 +2319,7 @@ func (r ApiListObjectsRequest) Execute() (ListObjectsResponse, *_nethttp.Respons
22902319
An `authorization_model_id` may be specified in the body. If it is not specified, the latest authorization model ID will be used. It is strongly recommended to specify authorization model id for better performance.
22912320
You may also specify `contextual_tuples` that will be treated as regular tuples. Each of these tuples may have an associated `condition`.
22922321
You may also provide a `context` object that will be used to evaluate the conditioned tuples in the system. It is strongly recommended to provide a value for all the input parameters of all the conditions, to ensure that all tuples be evaluated correctly.
2322+
By default, the Check API caches results for a short time to optimize performance. You may specify a value of `HIGHER_CONSISTENCY` for the optional `consistency` parameter in the body to inform the server that higher conisistency is preferred at the expense of increased latency. Consideration should be given to the increased latency if requesting higher consistency.
22932323
The response will contain the related objects in an array in the "objects" field of the response and they will be strings in the object format `<type>:<id>` (e.g. "document:roadmap").
22942324
The number of objects in the response array will be limited by the execution timeout specified in the flag OPENFGA_LIST_OBJECTS_DEADLINE and by the upper bound specified in the flag OPENFGA_LIST_OBJECTS_MAX_RESULTS, whichever is hit first.
22952325
The objects given will not be sorted, and therefore two identical calls can give a given different set of objects.
@@ -2867,7 +2897,7 @@ An `authorization_model_id` may be specified in the body. If it is not specified
28672897
You may also specify `contextual_tuples` that will be treated as regular tuples. Each of these tuples may have an associated `condition`.
28682898
You may also provide a `context` object that will be used to evaluate the conditioned tuples in the system. It is strongly recommended to provide a value for all the input parameters of all the conditions, to ensure that all tuples be evaluated correctly.
28692899
The response will contain the related users in an array in the "users" field of the response. These results may include specific objects, usersets
2870-
or type-bound public access. Each of these types of results is encoded in its own type and not represented as a string.In cases where a type-bound public acces result is returned (e.g. `user:*`), it cannot be inferred that all subjects
2900+
or type-bound public access. Each of these types of results is encoded in its own type and not represented as a string.In cases where a type-bound public access result is returned (e.g. `user:*`), it cannot be inferred that all subjects
28712901
of that type have a relation to the object; it is possible that negations exist and checks should still be queried
28722902
on individual subjects to ensure access to that document.The number of users in the response array will be limited by the execution timeout specified in the flag OPENFGA_LIST_USERS_DEADLINE and by the upper bound specified in the flag OPENFGA_LIST_USERS_MAX_RESULTS, whichever is hit first.
28732903
The returned users will not be sorted, and therefore two identical calls may yield different sets of users.
@@ -3550,7 +3580,7 @@ func (r ApiReadAssertionsRequest) Execute() (ReadAssertionsResponse, *_nethttp.R
35503580

35513581
/*
35523582
* ReadAssertions Read assertions for an authorization model ID
3553-
* The ReadAssertions API will return, for a given authorization model id, all the assertions stored for it. An assertion is an object that contains a tuple key, and the expectation of whether a call to the Check API of that tuple key will return true or false.
3583+
* The ReadAssertions API will return, for a given authorization model id, all the assertions stored for it.
35543584
* @param ctx _context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
35553585
* @param storeId
35563586
* @param authorizationModelId
@@ -4489,6 +4519,7 @@ type ApiReadChangesRequest struct {
44894519
type_ *string
44904520
pageSize *int32
44914521
continuationToken *string
4522+
startTime *time.Time
44924523
}
44934524

44944525
func (r ApiReadChangesRequest) Type_(type_ string) ApiReadChangesRequest {
@@ -4503,6 +4534,10 @@ func (r ApiReadChangesRequest) ContinuationToken(continuationToken string) ApiRe
45034534
r.continuationToken = &continuationToken
45044535
return r
45054536
}
4537+
func (r ApiReadChangesRequest) StartTime(startTime time.Time) ApiReadChangesRequest {
4538+
r.startTime = &startTime
4539+
return r
4540+
}
45064541

45074542
func (r ApiReadChangesRequest) Execute() (ReadChangesResponse, *_nethttp.Response, error) {
45084543
return r.ApiService.ReadChangesExecute(r)
@@ -4571,6 +4606,9 @@ func (a *OpenFgaApiService) ReadChangesExecute(r ApiReadChangesRequest) (ReadCha
45714606
if r.continuationToken != nil {
45724607
localVarQueryParams.Add("continuation_token", parameterToString(*r.continuationToken, ""))
45734608
}
4609+
if r.startTime != nil {
4610+
localVarQueryParams.Add("start_time", parameterToString(*r.startTime, ""))
4611+
}
45744612
// to determine the Content-Type header
45754613
localVarHTTPContentTypes := []string{}
45764614

@@ -5136,7 +5174,7 @@ func (r ApiWriteAssertionsRequest) Execute() (*_nethttp.Response, error) {
51365174

51375175
/*
51385176
* WriteAssertions Upsert assertions for an authorization model ID
5139-
* The WriteAssertions API will upsert new assertions for an authorization model id, or overwrite the existing ones. An assertion is an object that contains a tuple key, and the expectation of whether a call to the Check API of that tuple key will return true or false.
5177+
* The WriteAssertions API will upsert new assertions for an authorization model id, or overwrite the existing ones. An assertion is an object that contains a tuple key, the expectation of whether a call to the Check API of that tuple key will return true or false, and optionally a list of contextual tuples.
51405178
* @param ctx _context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
51415179
* @param storeId
51425180
* @param authorizationModelId

api_open_fga_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919
"net/http"
2020
"testing"
21+
"time"
2122

2223
"github.com/jarcoal/httpmock"
2324
"github.com/openfga/go-sdk/credentials"
@@ -794,9 +795,11 @@ func TestOpenFgaApi(t *testing.T) {
794795
return resp, nil
795796
},
796797
)
798+
startTime, err := time.Parse(time.RFC3339, "2022-01-01T00:00:00Z")
797799
got, response, err := apiClient.OpenFgaApi.ReadChanges(context.Background(), "01GXSB9YR785C4FYS3C0RTG7B2").
798800
Type_("repo").
799801
PageSize(25).
802+
StartTime(startTime).
800803
ContinuationToken("eyJwayI6IkxBVEVTVF9OU0NPTkZJR19hdXRoMHN0b3JlIiwic2siOiIxem1qbXF3MWZLZExTcUoyN01MdTdqTjh0cWgifQ==").
801804
Execute()
802805
if err != nil {

0 commit comments

Comments
 (0)