Skip to content

Commit

Permalink
feat!: support consistency parameter (#117)
Browse files Browse the repository at this point in the history
* feat!: Support using consistency params

* fix: use new type name values in example
  • Loading branch information
jimmyjames authored Aug 23, 2024
1 parent 9449c5e commit d1fa227
Show file tree
Hide file tree
Showing 43 changed files with 1,040 additions and 168 deletions.
2 changes: 2 additions & 0 deletions .openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ docs/Computed.md
docs/Condition.md
docs/ConditionMetadata.md
docs/ConditionParamTypeRef.md
docs/ConsistencyPreference.md
docs/ContextualTupleKeys.md
docs/CreateStoreRequest.md
docs/CreateStoreResponse.md
Expand Down Expand Up @@ -123,6 +124,7 @@ model_computed.go
model_condition.go
model_condition_metadata.go
model_condition_param_type_ref.go
model_consistency_preference.go
model_contextual_tuple_keys.go
model_create_store_request.go
model_create_store_response.go
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ options := ClientGetStoreOptions{
// You can rely on the store id set in the configuration or override it for this specific request
StoreId: openfga.PtrString("01FQH7V8BEG3GPQW93KTRFR8JB"),
}
store, err := fgaClient.GetStore(context.Background()).Options(options).Execute()
store, err := fgaClient.GetStore(context.Background()).Options(options)Execute()
if err != nil {
// handle error
}
Expand Down Expand Up @@ -941,7 +941,7 @@ Class | Method | HTTP request | Description
*OpenFgaApi* | [**GetStore**](docs/OpenFgaApi.md#getstore) | **Get** /stores/{store_id} | Get a store
*OpenFgaApi* | [**ListObjects**](docs/OpenFgaApi.md#listobjects) | **Post** /stores/{store_id}/list-objects | List all objects of the given type that the user has a relation with
*OpenFgaApi* | [**ListStores**](docs/OpenFgaApi.md#liststores) | **Get** /stores | List all stores
*OpenFgaApi* | [**ListUsers**](docs/OpenFgaApi.md#listusers) | **Post** /stores/{store_id}/list-users | [EXPERIMENTAL] List the users matching the provided filter who have a certain relation to a particular type.
*OpenFgaApi* | [**ListUsers**](docs/OpenFgaApi.md#listusers) | **Post** /stores/{store_id}/list-users | List the users matching the provided filter who have a certain relation to a particular type.
*OpenFgaApi* | [**Read**](docs/OpenFgaApi.md#read) | **Post** /stores/{store_id}/read | Get tuples from the store that matches a query, without following userset rewrite rules
*OpenFgaApi* | [**ReadAssertions**](docs/OpenFgaApi.md#readassertions) | **Get** /stores/{store_id}/assertions/{authorization_model_id} | Read assertions for an authorization model ID
*OpenFgaApi* | [**ReadAuthorizationModel**](docs/OpenFgaApi.md#readauthorizationmodel) | **Get** /stores/{store_id}/authorization-models/{id} | Return a particular version of an authorization model
Expand All @@ -966,6 +966,7 @@ Class | Method | HTTP request | Description
- [Condition](docs/Condition.md)
- [ConditionMetadata](docs/ConditionMetadata.md)
- [ConditionParamTypeRef](docs/ConditionParamTypeRef.md)
- [ConsistencyPreference](docs/ConsistencyPreference.md)
- [ContextualTupleKeys](docs/ContextualTupleKeys.md)
- [CreateStoreRequest](docs/CreateStoreRequest.md)
- [CreateStoreResponse](docs/CreateStoreResponse.md)
Expand Down
10 changes: 4 additions & 6 deletions api_open_fga.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,9 @@ type OpenFgaApi interface {
ListStoresExecute(r ApiListStoresRequest) (ListStoresResponse, *_nethttp.Response, error)

/*
* ListUsers [EXPERIMENTAL] List the users matching the provided filter who have a certain relation to a particular type.
* ListUsers List the users matching the provided filter who have a certain relation to a particular type.
* The ListUsers API returns a list of all the users of a specific type that have a relation to a given object.
This API is available in an experimental capacity and can be enabled with the `--experimentals enable-list-users` flag.
To arrive at a result, the API uses: an authorization model, explicit tuples written through the Write API, contextual tuples present in the request, and implicit tuples that exist by virtue of applying set theory (such as `document:2021-budget#viewer@document:2021-budget#viewer`; the set of users who are viewers of `document:2021-budget` are the set of users who are the viewers of `document:2021-budget`).
To arrive at a result, the API uses: an authorization model, explicit tuples written through the Write API, contextual tuples present in the request, and implicit tuples that exist by virtue of applying set theory (such as `document:2021-budget#viewer@document:2021-budget#viewer`; the set of users who are viewers of `document:2021-budget` are the set of users who are the viewers of `document:2021-budget`).
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.
You may also specify `contextual_tuples` that will be treated as regular tuples. Each of these tuples may have an associated `condition`.
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.
Expand Down Expand Up @@ -2700,11 +2699,10 @@ func (r ApiListUsersRequest) Execute() (ListUsersResponse, *_nethttp.Response, e
}

/*
- ListUsers [EXPERIMENTAL] List the users matching the provided filter who have a certain relation to a particular type.
- ListUsers List the users matching the provided filter who have a certain relation to a particular type.
- The ListUsers API returns a list of all the users of a specific type that have a relation to a given object.
This API is available in an experimental capacity and can be enabled with the `--experimentals enable-list-users` flag.
To arrive at a result, the API uses: an authorization model, explicit tuples written through the Write API, contextual tuples present in the request, and implicit tuples that exist by virtue of applying set theory (such as `document:2021-budget#viewer@document:2021-budget#viewer`; the set of users who are viewers of `document:2021-budget` are the set of users who are the viewers of `document:2021-budget`).
To arrive at a result, the API uses: an authorization model, explicit tuples written through the Write API, contextual tuples present in the request, and implicit tuples that exist by virtue of applying set theory (such as `document:2021-budget#viewer@document:2021-budget#viewer`; the set of users who are viewers of `document:2021-budget` are the set of users who are the viewers of `document:2021-budget`).
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.
You may also specify `contextual_tuples` that will be treated as regular tuples. Each of these tuples may have an associated `condition`.
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.
Expand Down
12 changes: 6 additions & 6 deletions api_open_fga_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1025,8 +1025,8 @@ func TestOpenFgaApi(t *testing.T) {
t.Fatalf("Expected status code to be 400 but actual %d", validationError.ResponseStatusCode())
}

if validationError.ResponseCode() != VALIDATION_ERROR {
t.Fatalf("Expected response code to be VALIDATION_ERROR but actual %s", validationError.ResponseCode())
if validationError.ResponseCode() != ERRORCODE_VALIDATION_ERROR {
t.Fatalf("Expected response code to be ERRORCODE_VALIDATION_ERROR but actual %s", validationError.ResponseCode())
}
})

Expand Down Expand Up @@ -1146,8 +1146,8 @@ func TestOpenFgaApi(t *testing.T) {
t.Fatalf("Expected status code to be 404 but actual %d", notFoundError.ResponseStatusCode())
}

if notFoundError.ResponseCode() != UNDEFINED_ENDPOINT {
t.Fatalf("Expected response code to be UNDEFINED_ENDPOINT but actual %s", notFoundError.ResponseCode())
if notFoundError.ResponseCode() != NOTFOUNDERRORCODE_UNDEFINED_ENDPOINT {
t.Fatalf("Expected response code to be NOTFOUNDERRORCODE_UNDEFINED_ENDPOINT but actual %s", notFoundError.ResponseCode())
}
})

Expand Down Expand Up @@ -1342,8 +1342,8 @@ func TestOpenFgaApi(t *testing.T) {
t.Fatalf("Expected status code to be 500 but actual %d", internalError.ResponseStatusCode())
}

if internalError.ResponseCode() != INTERNAL_ERROR {
t.Fatalf("Expected response code to be INTERNAL_ERROR but actual %s", internalError.ResponseCode())
if internalError.ResponseCode() != INTERNALERRORCODE_INTERNAL_ERROR {
t.Fatalf("Expected response code to be INTERNALERRORCODE_INTERNAL_ERROR but actual %s", internalError.ResponseCode())
}
})
}
98 changes: 65 additions & 33 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1238,9 +1238,10 @@ type ClientReadRequest struct {
}

type ClientReadOptions struct {
PageSize *int32 `json:"page_size,omitempty"`
ContinuationToken *string `json:"continuation_token,omitempty"`
StoreId *string `json:"store_id,omitempty"`
PageSize *int32 `json:"page_size,omitempty"`
ContinuationToken *string `json:"continuation_token,omitempty"`
StoreId *string `json:"store_id,omitempty"`
Consistency *fgaSdk.ConsistencyPreference `json:"consistency,omitempty"`
}

type ClientReadResponse = fgaSdk.ReadResponse
Expand Down Expand Up @@ -1287,14 +1288,17 @@ func (request *SdkClientReadRequest) GetOptions() *ClientReadOptions {

func (client *OpenFgaClient) ReadExecute(request SdkClientReadRequestInterface) (*ClientReadResponse, error) {
pagingOpts := ClientPaginationOptions{}
var consistency *fgaSdk.ConsistencyPreference
if request.GetOptions() != nil {
pagingOpts.PageSize = request.GetOptions().PageSize
pagingOpts.ContinuationToken = request.GetOptions().ContinuationToken
consistency = request.GetOptions().Consistency
}

body := fgaSdk.ReadRequest{
PageSize: getPageSizeFromRequest(&pagingOpts),
ContinuationToken: getContinuationTokenFromRequest(&pagingOpts),
Consistency: consistency,
}
if request.GetBody() != nil && (request.GetBody().User != nil || request.GetBody().Relation != nil || request.GetBody().Object != nil) {
body.TupleKey = &fgaSdk.ReadRequestTupleKey{
Expand Down Expand Up @@ -1812,8 +1816,9 @@ type ClientCheckRequest struct {
}

type ClientCheckOptions struct {
AuthorizationModelId *string `json:"authorization_model_id,omitempty"`
StoreId *string `json:"store_id,omitempty"`
AuthorizationModelId *string `json:"authorization_model_id,omitempty"`
StoreId *string `json:"store_id,omitempty"`
Consistency *fgaSdk.ConsistencyPreference `json:"consistency,omitempty"`
}

type ClientCheckResponse struct {
Expand Down Expand Up @@ -1894,6 +1899,10 @@ func (client *OpenFgaClient) CheckExecute(request SdkClientCheckRequestInterface
AuthorizationModelId: authorizationModelId,
}

if request.GetOptions() != nil {
requestBody.Consistency = request.GetOptions().Consistency
}

data, httpResponse, err := client.OpenFgaApi.Check(request.GetContext(), *storeId).Body(requestBody).Execute()
return &ClientCheckResponse{CheckResponse: data, HttpResponse: httpResponse}, err
}
Expand Down Expand Up @@ -1923,9 +1932,10 @@ type SdkClientBatchCheckRequestInterface interface {
type ClientBatchCheckBody = []ClientCheckRequest

type ClientBatchCheckOptions struct {
AuthorizationModelId *string `json:"authorization_model_id,omitempty"`
StoreId *string `json:"store_id,omitempty"`
MaxParallelRequests *int32 `json:"max_parallel_requests,omitempty"`
AuthorizationModelId *string `json:"authorization_model_id,omitempty"`
StoreId *string `json:"store_id,omitempty"`
MaxParallelRequests *int32 `json:"max_parallel_requests,omitempty"`
Consistency *fgaSdk.ConsistencyPreference `json:"consistency,omitempty"`
}

type ClientBatchCheckSingleResponse struct {
Expand Down Expand Up @@ -2065,8 +2075,9 @@ type ClientExpandRequest struct {
}

type ClientExpandOptions struct {
AuthorizationModelId *string `json:"authorization_model_id,omitempty"`
StoreId *string `json:"store_id,omitempty"`
AuthorizationModelId *string `json:"authorization_model_id,omitempty"`
StoreId *string `json:"store_id,omitempty"`
Consistency *fgaSdk.ConsistencyPreference `json:"consistency,omitempty"`
}

type ClientExpandResponse = fgaSdk.ExpandResponse
Expand Down Expand Up @@ -2128,13 +2139,19 @@ func (client *OpenFgaClient) ExpandExecute(request SdkClientExpandRequestInterfa
return nil, err
}

data, _, err := client.OpenFgaApi.Expand(request.GetContext(), *storeId).Body(fgaSdk.ExpandRequest{
body := fgaSdk.ExpandRequest{
TupleKey: fgaSdk.ExpandRequestTupleKey{
Relation: request.GetBody().Relation,
Object: request.GetBody().Object,
},
AuthorizationModelId: authorizationModelId,
}).Execute()
}

if request.GetOptions() != nil {
body.Consistency = request.GetOptions().Consistency
}

data, _, err := client.OpenFgaApi.Expand(request.GetContext(), *storeId).Body(body).Execute()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -2171,8 +2188,9 @@ type ClientListObjectsRequest struct {
}

type ClientListObjectsOptions struct {
AuthorizationModelId *string `json:"authorization_model_id,omitempty"`
StoreId *string `json:"store_id,omitempty"`
AuthorizationModelId *string `json:"authorization_model_id,omitempty"`
StoreId *string `json:"store_id,omitempty"`
Consistency *fgaSdk.ConsistencyPreference `json:"consistency,omitempty"`
}

type ClientListObjectsResponse = fgaSdk.ListObjectsResponse
Expand Down Expand Up @@ -2239,14 +2257,18 @@ func (client *OpenFgaClient) ListObjectsExecute(request SdkClientListObjectsRequ
if err != nil {
return nil, err
}
data, _, err := client.OpenFgaApi.ListObjects(request.GetContext(), *storeId).Body(fgaSdk.ListObjectsRequest{
body := fgaSdk.ListObjectsRequest{
User: request.GetBody().User,
Relation: request.GetBody().Relation,
Type: request.GetBody().Type,
ContextualTuples: fgaSdk.NewContextualTupleKeys(contextualTuples),
Context: request.GetBody().Context,
AuthorizationModelId: authorizationModelId,
}).Execute()
}
if request.GetOptions() != nil {
body.Consistency = request.GetOptions().Consistency
}
data, _, err := client.OpenFgaApi.ListObjects(request.GetContext(), *storeId).Body(body).Execute()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -2284,9 +2306,10 @@ type ClientListRelationsRequest struct {
}

type ClientListRelationsOptions struct {
AuthorizationModelId *string `json:"authorization_model_id,omitempty"`
MaxParallelRequests *int32 `json:"max_parallel_requests,omitempty"`
StoreId *string `json:"store_id,omitempty"`
AuthorizationModelId *string `json:"authorization_model_id,omitempty"`
MaxParallelRequests *int32 `json:"max_parallel_requests,omitempty"`
StoreId *string `json:"store_id,omitempty"`
Consistency *fgaSdk.ConsistencyPreference `json:"consistency,omitempty"`
}

type ClientListRelationsResponse struct {
Expand Down Expand Up @@ -2369,19 +2392,21 @@ func (client *OpenFgaClient) ListRelationsExecute(request SdkClientListRelations
if err != nil {
return nil, err
}
var maxParallelReqs *int32

options := &ClientBatchCheckOptions{
AuthorizationModelId: authorizationModelId,
StoreId: storeId,
}
if request.GetOptions() != nil {
maxParallelReqs = request.GetOptions().MaxParallelRequests
options.Consistency = request.GetOptions().Consistency
options.MaxParallelRequests = request.GetOptions().MaxParallelRequests
}

batchResponse, err := client.BatchCheckExecute(&SdkClientBatchCheckRequest{
ctx: request.GetContext(),
Client: client,
body: &batchRequestBody,
options: &ClientBatchCheckOptions{
AuthorizationModelId: authorizationModelId,
StoreId: storeId,
MaxParallelRequests: maxParallelReqs,
},
ctx: request.GetContext(),
Client: client,
body: &batchRequestBody,
options: options,
})

if err != nil {
Expand Down Expand Up @@ -2429,8 +2454,9 @@ type ClientListUsersRequest struct {
}

type ClientListUsersOptions struct {
AuthorizationModelId *string `json:"authorization_model_id,omitempty"`
StoreId *string `json:"store_id,omitempty"`
AuthorizationModelId *string `json:"authorization_model_id,omitempty"`
StoreId *string `json:"store_id,omitempty"`
Consistency *fgaSdk.ConsistencyPreference `json:"consistency,omitempty"`
}

type ClientListUsersResponse = fgaSdk.ListUsersResponse
Expand Down Expand Up @@ -2497,14 +2523,20 @@ func (client *OpenFgaClient) ListUsersExecute(request SdkClientListUsersRequestI
if err != nil {
return nil, err
}
data, _, err := client.OpenFgaApi.ListUsers(request.GetContext(), *storeId).Body(fgaSdk.ListUsersRequest{
body := fgaSdk.ListUsersRequest{
Object: request.GetBody().Object,
Relation: request.GetBody().Relation,
UserFilters: request.GetBody().UserFilters,
ContextualTuples: &fgaSdk.NewContextualTupleKeys(contextualTuples).TupleKeys,
Context: request.GetBody().Context,
AuthorizationModelId: authorizationModelId,
}).Execute()
}

if request.GetOptions() != nil {
body.Consistency = request.GetOptions().Consistency
}

data, _, err := client.OpenFgaApi.ListUsers(request.GetContext(), *storeId).Body(body).Execute()
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit d1fa227

Please sign in to comment.