### Integration with OpenFGA API Source: https://context7.com/maxmindlin/openfga-dsl-parser-python/llms.txt A complete example showing how to parse a DSL model using `dsl_to_json` and then send the resulting JSON to the OpenFGA HTTP API to create an authorization model. ```APIDOC ## Integration with OpenFGA API ### Description Complete example of parsing DSL and using it with OpenFGA HTTP API. ### Method `POST` (for the OpenFGA API call) ### Endpoint `/stores/{store_id}/authorization-models` (OpenFGA API endpoint) ### Parameters #### Path Parameters - **store_id** (string) - Required - The ID of the OpenFGA store. #### Query Parameters None #### Request Body (for `dsl_to_json` function) - **dsl_model** (string) - Required - The OpenFGA DSL syntax as a string. #### Request Body (for OpenFGA API) - **model_dict** (object) - Required - The parsed JSON authorization model. ### Request Example ```python import json import requests from openfga_dsl_parser import dsl_to_json # Define authorization model dsl_model = """ type user type repository relations define owner as self define contributor as self define reader as self or contributor """ # Parse to JSON model_json = dsl_to_json(dsl_model) model_dict = json.loads(model_json) # Send to OpenFGA API openfga_url = "http://localhost:8080" store_id = "01HVMMBBQZX5SKQW1F2Z7J6XQZ" response = requests.post( f"{openfga_url}/stores/{store_id}/authorization-models", json=model_dict, headers={"Content-Type": "application/json"} ) if response.status_code == 201: print(f"Model created: {response.json()['authorization_model_id']}") else: print(f"Error: {response.status_code} - {response.text}") ``` ### Response #### Success Response (201 Created - OpenFGA API) - **authorization_model_id** (string) - The ID of the newly created authorization model. #### Response Example (Success) ```json { "authorization_model_id": "01HVZF43QY672T1M9WXVY9J2P7", "created_at": "2023-10-27T10:30:00Z", "type_definitions": [ { "type": "user" }, { "type": "repository", "relations": { "owner": {"this": {}}, "contributor": {"this": {}}, "reader": { "union": { "this": {}, "computed_userset": {"object": "self", "relation": "contributor"} } } } } ] } ``` #### Error Response (OpenFGA API) - **error** (object) - Details about the error if the API call fails. #### Response Example (Error) ```json { "error": { "code": "invalid_argument", "message": "Authorization model is invalid." } } ``` ``` -------------------------------- ### Integrate OpenFGA DSL Parsing with OpenFGA API in Python Source: https://context7.com/maxmindlin/openfga-dsl-parser-python/llms.txt A complete example demonstrating how to parse OpenFGA DSL into JSON using Python and then send this JSON model to the OpenFGA HTTP API. This snippet utilizes `openfga_dsl_parser`, `json`, and `requests` libraries. It shows the process of defining a model, converting it, and making a POST request to create an authorization model in an OpenFGA store. ```python import json import requests from openfga_dsl_parser import dsl_to_json # Define authorization model dsl_model = """type user type repository relations define owner as self define contributor as self define reader as self or contributor""" # Parse to JSON model_json = dsl_to_json(dsl_model) model_dict = json.loads(model_json) # Send to OpenFGA API openfga_url = "http://localhost:8080" store_id = "01HVMMBBQZX5SKQW1F2Z7J6XQZ" response = requests.post( f"{openfga_url}/stores/{store_id}/authorization-models", json=model_dict, headers={"Content-Type": "application/json"} ) if response.status_code == 201: print(f"Model created: {response.json()['authorization_model_id']}") else: print(f"Error: {response.status_code} - {response.text}") ``` -------------------------------- ### Handle Invalid DSL Syntax Errors in Python Source: https://context7.com/maxmindlin/openfga-dsl-parser-python/llms.txt Properly catches and handles parsing errors when DSL syntax is malformed using Python. This example demonstrates the use of a try-except block to manage `ValueError` exceptions raised by `dsl_to_json` for invalid DSL inputs, such as incomplete relation definitions. It ensures graceful failure and error reporting. ```python from openfga_dsl_parser import dsl_to_json invalid_dsl = """type document relations define owner as define viewer""" # Incomplete relation definitions try: result = dsl_to_json(invalid_dsl) print(result) except ValueError as e: print(f"Failed to parse DSL: {e}") # Handle error appropriately - log, return error response, etc. ``` -------------------------------- ### Parse Complex Relations with Exclusions in Python Source: https://context7.com/maxmindlin/openfga-dsl-parser-python/llms.txt Handles advanced authorization rules, including exclusion operators, for fine-grained access control using Python. This example demonstrates parsing a DSL with complex relation definitions like 'self or member from parent but not owner'. It includes error handling for invalid syntax, returning a JSON result or a ValueError. ```python from openfga_dsl_parser import dsl_to_json dsl = """type organization relations define admin as self define member as self type folder relations define parent as self define owner as self or admin from parent define viewer as self or member from parent but not owner""" try: json_result = dsl_to_json(dsl) print(json_result) except ValueError as e: print(f"Invalid DSL syntax: {e}") ``` -------------------------------- ### Error Handling for Invalid DSL Source: https://context7.com/maxmindlin/openfga-dsl-parser-python/llms.txt Demonstrates how to properly catch and handle parsing errors when the DSL syntax is malformed using a try-except block with `dsl_to_json`. ```APIDOC ## Error Handling for Invalid DSL ### Description Properly catch and handle parsing errors when DSL syntax is malformed. ### Method `POST` (conceptual, as this is a library function, not an HTTP endpoint) ### Endpoint N/A (Python function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **invalid_dsl** (string) - Required - The malformed OpenFGA DSL syntax. ### Request Example ```python from openfga_dsl_parser import dsl_to_json invalid_dsl = """ type document relations define owner as define viewer """ try: result = dsl_to_json(invalid_dsl) print(result) except ValueError as e: print(f"Failed to parse DSL: {e}") ``` ### Response #### Success Response (200) - **result** (string) - If parsing were successful (which it won't be with invalid DSL), this would be the JSON output. #### Response Example (Error Handling) ``` Failed to parse DSL: Expected an object or userset after 'owner', found 'type' keyword at line 3, column 20 ``` ``` -------------------------------- ### Parse Basic Type Definition with Self Relations in Python Source: https://context7.com/maxmindlin/openfga-dsl-parser-python/llms.txt Demonstrates parsing a simple authorization model with basic self-relations for user assignments using Python. This snippet showcases the `dsl_to_json` function for a minimal DSL structure. The output is a JSON representation of the type definitions and relation configurations, suitable for OpenFGA API interactions. ```python from openfga_dsl_parser import dsl_to_json dsl = """type user type document relations define owner as self define viewer as self""" result = dsl_to_json(dsl) print(result) # Output: JSON representation with type definitions and relation configurations ``` -------------------------------- ### Basic Type Definition with Self Relations Source: https://context7.com/maxmindlin/openfga-dsl-parser-python/llms.txt Demonstrates parsing a simple authorization model with basic self-relations for user assignments using the `dsl_to_json` function. ```APIDOC ## Basic Type Definition with Self Relations ### Description Parse a simple authorization model with basic self-relations for user assignments. ### Method `POST` (conceptual, as this is a library function, not an HTTP endpoint) ### Endpoint N/A (Python function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **dsl** (string) - Required - The OpenFGA DSL syntax for a simple model. ### Request Example ```python from openfga_dsl_parser import dsl_to_json dsl = """ type user type document relations define owner as self define viewer as self """ result = dsl_to_json(dsl) print(result) ``` ### Response #### Success Response (200) - **result** (string) - JSON representation of the authorization model. #### Response Example ```json { "schema_version": "1.1", "type_definitions": [ { "type": "user" }, { "type": "document", "relations": { "owner": { "this": {} }, "viewer": { "this": {} } } } ] } ``` ``` -------------------------------- ### Multi-Type Hierarchical Model Source: https://context7.com/maxmindlin/openfga-dsl-parser-python/llms.txt Defines a complete authorization model with multiple types and hierarchical relationships using the `dsl_to_json` function. ```APIDOC ## Multi-Type Hierarchical Model ### Description Define a complete authorization model with multiple types and hierarchical relationships. ### Method `POST` (conceptual, as this is a library function, not an HTTP endpoint) ### Endpoint N/A (Python function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **authorization_model** (string) - Required - The OpenFGA DSL syntax for a multi-type hierarchical model. ### Request Example ```python from openfga_dsl_parser import dsl_to_json authorization_model = """ type user type workspace relations define admin as self define member as self type project relations define workspace as self define admin as admin from workspace define editor as self or admin define viewer as self or editor or member from workspace """ json_output = dsl_to_json(authorization_model) print(json_output) ``` ### Response #### Success Response (200) - **json_output** (string) - JSON representation of the hierarchical authorization model. #### Response Example ```json { "schema_version": "1.1", "type_definitions": [ { "type": "user" }, { "type": "workspace", "relations": { "admin": {"this": {}}, "member": {"this": {}} } }, { "type": "project", "relations": { "workspace": {"this": {}}, "admin": {"computed_userset": {"object": "self", "relation": "workspace"}}, "editor": { "union": { "this": {}, "computed_userset": {"object": "self", "relation": "admin"} } }, "viewer": { "union": { "left": { "union": { "this": {}, "computed_userset": {"object": "self", "relation": "editor"} } }, "right": { "computed_userset": {"object": "workspace", "relation": "member"} } } } } } ] } ``` ``` -------------------------------- ### Complex Relations with Exclusions Source: https://context7.com/maxmindlin/openfga-dsl-parser-python/llms.txt Handles advanced authorization rules including exclusion operators for fine-grained access control using the `dsl_to_json` function. ```APIDOC ## Complex Relations with Exclusions ### Description Handle advanced authorization rules including exclusion operators for fine-grained access control. ### Method `POST` (conceptual, as this is a library function, not an HTTP endpoint) ### Endpoint N/A (Python function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **dsl** (string) - Required - The OpenFGA DSL syntax with complex relations and exclusions. ### Request Example ```python from openfga_dsl_parser import dsl_to_json dsl = """ type organization relations define admin as self define member as self type folder relations define parent as self define owner as self or admin from parent define viewer as self or member from parent but not owner """ try: json_result = dsl_to_json(dsl) print(json_result) except ValueError as e: print(f"Invalid DSL syntax: {e}") ``` ### Response #### Success Response (200) - **json_result** (string) - JSON representation of the authorization model with complex relations. #### Response Example ```json { "schema_version": "1.1", "type_definitions": [ { "type": "organization", "relations": { "admin": {"this": {}}, "member": {"this": {}} } }, { "type": "folder", "relations": { "parent": {"this": {}}, "owner": { "union": { "this": {}, "computed_userset": {"object": "parent", "relation": "admin"} } }, "viewer": { "difference": { "left": { "union": { "this": {}, "computed_userset": {"object": "parent", "relation": "member"} } }, "right": { "computed_userset": {"object": "self", "relation": "owner"} } } } } } ] } ``` ``` -------------------------------- ### Convert OpenFGA DSL to JSON using Python Source: https://context7.com/maxmindlin/openfga-dsl-parser-python/llms.txt Parses an OpenFGA DSL string into a JSON string compatible with the OpenFGA HTTP API. This function is the primary interface for converting DSL models. It expects a valid DSL string as input and returns a JSON string upon successful parsing, or raises a ValueError for invalid syntax. No external dependencies beyond the library itself are required for basic conversion. ```python from openfga_dsl_parser import dsl_to_json # Define authorization model using DSL syntax dsl_input = """type group relations define member as self type resource relations define writer as self define reader as self but not writer""" # Convert DSL to JSON format try: json_output = dsl_to_json(dsl_input) print(json_output) except ValueError as e: print(f"Parse error: {e}") ``` -------------------------------- ### Parse Multi-Type Hierarchical Model in Python Source: https://context7.com/maxmindlin/openfga-dsl-parser-python/llms.txt Defines a complete authorization model with multiple types and hierarchical relationships using Python. This snippet illustrates parsing a more comprehensive DSL structure involving nested types and inherited relations, such as 'admin from workspace'. The output is the JSON representation of this complex model. ```python from openfga_dsl_parser import dsl_to_json authorization_model = """type user type workspace relations define admin as self define member as self type project relations define workspace as self define admin as admin from workspace define editor as self or admin define viewer as self or editor or member from workspace""" json_output = dsl_to_json(authorization_model) print(json_output) ``` -------------------------------- ### Parse OpenFGA DSL to JSON Source: https://github.com/maxmindlin/openfga-dsl-parser-python/blob/master/README.md Converts an OpenFGA DSL string into a JSON representation. This function is the primary utility for transforming authorization models defined in DSL format to the JSON format expected by the OpenFGA API. ```python from openfga_dsl_parser import dsl_to_json input = """type group relations define member as self type resource relations define writer as self define reader as self but not writer""" json = dsl_to_json(input) print(json) ``` -------------------------------- ### DSL to JSON Conversion Source: https://context7.com/maxmindlin/openfga-dsl-parser-python/llms.txt Parses an OpenFGA DSL string and converts it into a JSON string representation compatible with the OpenFGA HTTP API. This is the primary function for transforming authorization models. ```APIDOC ## dsl_to_json Function ### Description Parses an OpenFGA DSL string and converts it into a JSON string representation compatible with the OpenFGA HTTP API. ### Method `POST` (conceptual, as this is a library function, not an HTTP endpoint) ### Endpoint N/A (Python function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **dsl_input** (string) - Required - The OpenFGA DSL syntax as a string. ### Request Example ```python from openfga_dsl_parser import dsl_to_json dsl_input = """ type group relations define member as self type resource relations define writer as self define reader as self but not writer """ try: json_output = dsl_to_json(dsl_input) print(json_output) except ValueError as e: print(f"Parse error: {e}") ``` ### Response #### Success Response (200) - **json_output** (string) - A JSON string representing the parsed OpenFGA authorization model. #### Response Example ```json { "schema_version": "1.1", "type_definitions": [ { "type": "group", "relations": { "member": { "this": {} } } }, { "type": "resource", "relations": { "writer": { "this": {} }, "reader": { "this": {}, "not": { "this": {} } } } } ] } ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.