# Python client for ows-features
---
This Python client is designed to interact with `ows-features` for determining the states of feature flags. This is primarily within the context of an HTTP request, from which the `userContext` can be inferred from the Grass headers. However, there is some functionality for using this package within a CLI / daemon context.

This package is published to our internal Pypi server.

# Installation:

```bash
pip install -i https://pypi.theorchard.io/pypi/ owsfeatures[flask]
```

Alternatively, put the following in your requirements file:

```
-i https://pypi.theorchard.io/pypi/

owsfeatures[flask]

```

# Usage:

## The Flask extra:

The `[flask]` extra allows use of the decorator described below. If omitted you will need to use `/models/ows_features` directly.


####  Use decorator in handler:

```python
from owsfeatures import load_features


@app.route('/product', methods=['POST'])
@features.load_features
...
```

## Check if a feature is enabled:

Wrapping the  `is_feature_enabled` def with one to check specifically if `manufacturing_obligation` is enabled:

```python
from owsfeatures import is_feature_enabled


def is_manufacturing_obligation_enabled():
    """Check manufacturing_obligation flag.

    Returns:
        bool: whether manufacturing_obligation flag is enabled.
    """
    return is_feature_enabled('manufacturing_obligation')
```

## Pytest plugin
You can easily mock features service usage by builtin fixture:
```python
from owsfeatures import features

def test_feature_engine_flag(feature_engine):
    """Test feature engine fixture works."""
    feature_engine.force_flag('awesome_feature')
    result = features.is_feature_enabled('awesome_feature')
    assert result is True
```

## CLI / daemon (no user context):

In this use case, we have a process running that cannot determine a features user context because there are no HTTP request headers because it's a CLI script / daemon. In this mode, we can really only use a fully `enabled` or `control` active variant, and cannot rely on enabling for a specific label or user.

### Basic Usage - get_active_variant(feature_name, service_name)

In the `daemon-whatever` service, at some point before you want to fork execution path based on feature flag:

```python
from owsfeatures.models import ows_features
from daemon_whatever import config

label_copy_export_enabled = ows_features.get_active_variant(
	'label_copy_export', 'daemon-whatever', config.ENVIRONMENT) == 'enabled'

# ... your code that makes use of this flag
```