provenaclient.utils.helpers

Created Date: Monday June 17th 2024 +1000 Author: Peter Baker —– Last Modified: Monday June 17th 2024 5:00:47 pm +1000 Modified By: Peter Baker —– Description: General helper functions which are useful across the client library. —– HISTORY: Date By Comments ———- — ———————————————————

Attributes

BaseModelType

ItemModelType

JsonData

HttpxFileUpload

ParamTypes

Functions

convert_to_item_subtype(...)

Converts a string into ItemSubType supported enum type.

get_and_validate_file_path(→ Optional[str])

Determine and validate the file path for writing a file.

validate_existing_path(→ None)

Validates a provided file path, and checks if

write_file_helper(→ None)

Writes the provided content (string or bytes) to a file at the specified file path.

read_file_helper(→ str)

Reads a valid file and returns its content

build_params_exclude_none(→ Dict[str, ParamTypes])

Takes a raw params dict with optional args and returns filtered.

py_to_dict(→ JsonData)

This helper function converts a Pydantic model to a Python dictionary.

handle_model_parsing(→ BaseModelType)

This generic helper function parses a HTTP Response into a

parse_json_payload(→ JsonData)

Parses a HTTPX response object into JSON handling the error if any occurs.

handle_err_codes(→ None)

This helper function checks the status code of the HTTP response and raises

check_status_response(→ None)

Parses JSON data as StatusResponse model, then asserts success is true,

check_codes_and_parse_json(→ JsonData)

Given raw response, validates codes and parses as JSON.

handle_response_non_status(→ BaseModelType)

Given the raw response from http client, and the model, will validate

handle_response_with_status(→ BaseModelType)

Given the raw response from http client, and the model, will validate

Module Contents

provenaclient.utils.helpers.BaseModelType
provenaclient.utils.helpers.ItemModelType
provenaclient.utils.helpers.JsonData
provenaclient.utils.helpers.HttpxFileUpload
provenaclient.utils.helpers.ParamTypes
provenaclient.utils.helpers.convert_to_item_subtype(item_subtype_str: str | None) ProvenaInterfaces.RegistryModels.ItemSubType[source]

Converts a string into ItemSubType supported enum type.

Parameters:

item_subtype_str (Optional[str]) – Optional string containing similar enum text.

Returns:

Enum type of ItemSubType.

Return type:

ItemSubType

Raises:
  • ValueError – Item subtype field was not present.

  • ValueError – Item subtype cannot be converted to ENUM.

provenaclient.utils.helpers.get_and_validate_file_path(file_path: str | None, write_to_file: bool, default_file_name: str) str | None[source]

Determine and validate the file path for writing a file.

If file_path is not provided and write_to_file is True then will use a dynamic default file name.

Parameters:
  • file_path (Optional[str]) – The path to save the file at.

  • write_to_file (bool) – A boolean flag indicating whether writing to the file is enabled.

  • default_file_name (str) – The default file name to use if file_path is not provided.

Returns:

The validated file path, or None if writing to file is not enabled.

Return type:

Optional[str]

Raises:

ValueError – If a file path is provided but writing to the file is not enabled.

provenaclient.utils.helpers.validate_existing_path(file_path: str) None[source]

Validates a provided file path, and checks if the directory exists.

Parameters:

file_path (str) – The file path to validate.

Raises:
  • ValueError – If the directory part of the path does not exist.

  • IOError – If an I/O error occurs during file operations.

  • Exception – For any other exceptions that may occur.

provenaclient.utils.helpers.write_file_helper(file_path: str, content: str | bytes) None[source]

Writes the provided content (string or bytes) to a file at the specified file path.

Parameters:
  • file_name (str) – The name of the file to write content into.

  • content (Union[str, bytes]) –

    The content to be written to the file. It can be either:
    • A str, which will be written in text mode.

    • A bytes object, which will be written in binary mode.

Raises:
  • IOError – If an I/O error occurs during file operations.

  • Exception – For non-I/O related exceptions that may occur during file writing.

provenaclient.utils.helpers.read_file_helper(file_path: str) str[source]

Reads a valid file and returns its content

Parameters:

file_path (str) – The path of an existing created file.

Returns:

A string representation of the file contents.

Return type:

str

Raises:

Exception – If there any error with reading the file this general exception is raised.

provenaclient.utils.helpers.build_params_exclude_none(params: Mapping[str, ParamTypes | None]) Dict[str, ParamTypes][source]

Takes a raw params dict with optional args and returns filtered.

Parameters:

params (Dict[str, Optional[ParamTypes]]) – The input raw dict

Returns:

The filtered param list with no None values

Return type:

Dict[str, ParamTypes]

provenaclient.utils.helpers.py_to_dict(model: pydantic.BaseModel) JsonData[source]

This helper function converts a Pydantic model to a Python dictionary.

Requires a pydantic dump into serialised JSON to be safe against all object types

Parameters:

model (BaseModel) – The instance of the model that needs to be converted to a dict.

Returns:

A python dictionary object which contains the fields and values of the base model that are not none.

Return type:

JsonData

provenaclient.utils.helpers.handle_model_parsing(json_data: JsonData, model: Type[BaseModelType]) BaseModelType[source]

This generic helper function parses a HTTP Response into a python datatype based on a pydantic defined model.

Parameters:
  • json_data (JsonData) – The response received after HTTP request.

  • model (type[T]) – The type of the model being casted from HTTP response into python datatype. For example: MintResponse, RegistryFetchResponse.

Returns:

Returns a python datatype that conforms to the structure of the provided model.

Return type:

T

provenaclient.utils.helpers.parse_json_payload(response: httpx.Response) JsonData[source]

Parses a HTTPX response object into JSON handling the error if any occurs.

Parameters:

response (Response) – The raw HTTP response.

Raises:

ValidationException – If JSON decoding fails, handles and raises error.

Returns:

The returned dictionary object representing JSON data.

Return type:

JsonData

provenaclient.utils.helpers.handle_err_codes(response: httpx.Response, error_message: str | None) None[source]

This helper function checks the status code of the HTTP response and raises a custom exception accordingly.

Also embeds error info from JSON or text result.

Parameters:

response (Response) – The httpx.response object.

Raises:
provenaclient.utils.helpers.check_status_response(json_data: JsonData) None[source]

Parses JSON data as StatusResponse model, then asserts success is true, throwing exception with embedded details if not.

Parameters:

json_data (Dict) – The JSON data to parse

Raises:

Exception – Exception if status if False

provenaclient.utils.helpers.check_codes_and_parse_json(response: httpx.Response, error_message: str | None) JsonData[source]

Given raw response, validates codes and parses as JSON.

Parameters:
  • response (Response) – The raw http client response

  • error_message (Optional[str]) – The error message to embed if any

Returns:

The json data post parse

Return type:

JsonData

provenaclient.utils.helpers.handle_response_non_status(response: httpx.Response, model: Type[BaseModelType], error_message: str | None) BaseModelType[source]

Given the raw response from http client, and the model, will validate

  • 200 OK code (with common errors handled)

  • Parsed as JSON

  • Parsed as desired final model

Returns the parsed pydantic object.

Parameters:
  • response (Response) – The raw response from http client

  • model (Type[T]) – The model type (not instance) to parse against

  • error_message (Optional[str]) – The error message to embed into exceptions.

Returns:

The parsed model

Return type:

T

provenaclient.utils.helpers.handle_response_with_status(response: httpx.Response, model: Type[BaseModelType], error_message: str | None) BaseModelType[source]

Given the raw response from http client, and the model, will validate

  • 200 OK code (with common errors handled)

  • Parsed as JSON

  • Parsed as StatusResponse and asserted for success=true, throwing with details if not

  • Parsed as desired final model

Returns the parsed pydantic object.

Parameters:
  • response (Response) – The raw response from http client

  • model (Type[T]) – The model type (not instance) to parse against

  • error_message (Optional[str]) – The error message to embed into exceptions.

Returns:

The parsed model

Return type:

T