# Utils

`oaaclient` utility functions.

Copyright 2022 Veza Technologies Inc.

Use of this source code is governed by the MIT license that can be found in the LICENSE file or at <https://opensource.org/licenses/MIT>.

helper functions commonly used by OAA integrations

***

## function `log_arg_error`

```python
log_arg_error(log: object, arg: str = None, env: str = None) → None
```

Helper function for logging errors when loading parameters

Helper function used to create consistent messages in connectors when required parameters can be set at command line or as environment variables.

Message can include information on parameter and/or environment variable but must provide one.

**Args:**

* `log` (object): logging facility object to log to
* `arg` (str, optional): Command line option for parameter such as `--veza-url`. Defaults to None.
* `env` (str, optional): OS Environment variable for parameter such as `VEZA_URL`. Defaults to None.

**Raises:**

* `Exception`: if neither `arg` or `env` are supplied

***

## function `load_json_from_file`

```python
load_json_from_file(json_path: str) → dict
```

Load JSON from file

**Args:**

* `json_path` (str): path to JSON file on disk

**Raises:**

* `Exception`: Unable to process JSON
* `Exception`: Error reading JSON file

**Returns:**

* `dict`: JSON decoded to dictionary

***

## function `encode_icon_file`

```python
encode_icon_file(icon_path: str) → str
```

read an icon file to a base64 encoded string

**Args:**

* `icon_path` (str): Path to icon file on disk

**Returns:**

* `str`: base64 encoding of file

***

## function `exists_in_query_array`

```python
exists_in_query_array(value_to_find, input_array) → bool
```

***

## function `build_report`

```python
build_report(veza_con, report_definition: dict) → dict
```

Creates or updates a Veza report from a dictionary

Creates a report and containing queries from a dictionary definition. Function will create any queries it does not find based on name. If a query with the same name already exists the existing query will be added to the report.

If a report already exists with the same name any missing queries will be added to the report.

`report_definition` must be a dictionary with `name` for the report and `queries` list of Veza query definitions:

{"name": "My Report", "queries": \[{..},{...}]}

**Args:**

* `veza_con` (OAAClient): OAAClient connection to make Veza API calls
* `report_definition` (dict): Report definition

**Raises:**

* `ValueError`: Missing name or queries key

**Returns:**

* `dict`: API response from Report creation or update

***

## function `truncate_string`

```python
truncate_string(source_str: str, length: int = 256) → str
```

Helper function to truncate strings

Helper function to truncate strings to conform to maximum length requirements for templates.

Returns a string that is the first N bytes of the source string

**Args:**

* `source_str` (str): Source string to truncate
* `length` (int, optional): Length to shorten to. Defaults to 256.

**Returns:**

* `str`: truncated string

***
