Metadata-Version: 2.1
Name: opa-python
Version: 0.0.8
Summary: A Python OPA client library
Home-page: https://github.com/heliconhq/opa-python
License: MIT
Author: Gustaf Sjoberg
Author-email: gs@helicon.ai
Requires-Python: >=3.10,<4
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.10
Requires-Dist: requests (>=2.28.1,<3.0.0)
Project-URL: Documentation, https://github.com/heliconhq/opa-python
Description-Content-Type: text/markdown

# OPA Python

Python client library for Open Policy Framework (OPA).

## Installation

    python -m pip install opa-python
    
## Compatibility

The library has been tested with:

- Python 3.10
- OPA 0.47.3
    
## Usage

Create a client instance:

```python
>>> from opa import OPAClient
>>> client = OPAClient(url="http://opa-server/")
```

Verify that the OPA server is up and ready to accept requests:

```python
>>> client.check_health()
True
```
    
Create or update a document:

```python
>>> data = {
...    "users": [
...        "bilbo",
...        "frodo",
...        "gandalf",
...    ],
... }
>>> client.save_document("my.data", data)
```
    
Create or update a policy:

```python
>>> policy = """
... package my.policy
... 
... default allow := false
... 
... allow {
...     data.my.data.users[_] = input.name
... }
... """
>>> client.save_policy("policy-id", policy)
{}
```
    
Request decisions by evaluating input against the policy and data:

```python
>>> client.check_policy("my.policy.allow", {"name": "bilbo"})
True

>>> client.check_policy("my.policy.allow", {"name": "sauron"})
False
```

You can find the full reference in the 
[documentation](https://opa-python.readthedocs.io/en/latest/).

## Local installation

Install [Poetry](https://python-poetry.org/), create new environment and
install the dependencies:

    poetry install
    
## Running the test suite

NOTE: Each individual test that communicates with OPA creates a fresh Docker
container. This is pretty neat from a testing perspective, but means that
running the entire suite takes a bit of time.

Make sure you do not have any services listening to `8181` when you start the
tests! We might add a configuration for setting the port later, or run the
tests in Docker as well.

Run the tests:

    poetry run pytest
    
Run specific test module:

    poetry run pytest tests/test_integration.py
    
## Publishing

Set your credentials:

    poetry config pypi-token.pypi <token>

Build and publish:

    poetry publish --build

