Skip to main content

An SDK for interacting with CityPower's APIs.

Project description

logafault - SDK for Logging Faults with City Power

logafault is a Python SDK that allows users to programmatically log faults with City Power, provided they have a valid username and password. This package interacts with City Power's website and API to authenticate users, retrieve fault data, and log new faults.

Installation

You can install logafault directly from PyPI:

pip install logafault

Usage

1. Authenticate and Get Session Cookie

To interact with the City Power APIs, you first need to authenticate with your username and password to get a session cookie. This cookie is required for subsequent API requests.

from logafault.auth import get_cookie, AuthError

# Replace with your City Power username and password
username = "your_username"
password = "your_password"

try:
    cookie = get_cookie(username, password)
    print("Successfully authenticated!")
except AuthError as e:
    print(f"Authentication failed: {e}")

2. Fetch All Logged Faults

Once authenticated, you can retrieve all logged faults associated with your account. The get_all_faults function fetches the list of faults.

from logafault.faults import get_all_faults, FaultsAPIError

try:
    faults = get_all_faults(cookie)
    print("Logged faults:", faults)
except FaultsAPIError as e:
    print(f"Failed to fetch faults: {e}")

3. Log a New Fault

You can also log a new fault using the log_fault function. You will need to provide the necessary fault data in the form of a dictionary.

from logafault.faults import log_fault, FaultsAPIError

# Sample fault data (replace with actual fault details)
fault_data = {
    "workType": "NS", # No Supply
    "childWorkType": "NSA", # No Supply (Area)
    "customLookupCode2": "Prepaid",
    "description": "My bat signal is off.",
    "custom2": "1234567890", # Meter Number
    "custom4": "", # Alternate Account Number
    "contactNumber": "0123456789",
    "contactName": "Bruce Wayne",
}

try:
    response = log_fault(cookie, fault_data)
    print("Fault logged successfully:", response)
except FaultsAPIError as e:
    print(f"Failed to log fault: {e}")

Functions

get_cookie(username: str, password: str) -> str

Logs in to the City Power website and retrieves the session cookies needed for authentication.

Arguments:

  • username (str): The username for authentication.
  • password (str): The password for authentication.

Returns:

  • A string containing the session cookie for future requests.

Raises:

  • AuthError: If login fails or required cookies are missing.

get_all_faults(cookie: str) -> list[dict]

Fetches all logged faults from the City Power API.

Arguments:

  • cookie (str): The session cookie to authenticate the request.

Returns:

  • A list of dictionaries representing the logged faults.

Raises:

  • FaultsAPIError: If the request fails or the response is invalid.

log_fault(cookie: str, fault_data: dict) -> dict

Logs a new fault to the City Power API.

Arguments:

  • cookie (str): The session cookie to authenticate the request.
  • fault_data (dict): A dictionary containing fault details.

Returns:

  • A dictionary with the API response containing the fault details.

Raises:

  • FaultsAPIError: If the request fails or the response is invalid.

Error Handling

AuthError

This exception is raised if there is an issue with authentication, such as incorrect credentials or missing cookies.

FaultsAPIError

This exception is raised if there is an issue with the faults API, including problems with fetching faults or logging a new fault.


Development

If you want to contribute to the development of logafault, clone the repository and install the development dependencies:

git clone https://github.com/cliftondhanee/logafault.git
cd logafault
make setup-dev

Running Tests

To run the unit tests:

make test

Linting

We use flake8 for linting. You can run it with:

make lint

License

This project is licensed under the MIT License - see the LICENSE file for details.


Contact

For more information, feel free to open an issue on GitHub or contact the author:


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

logafault-1.1.0.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

logafault-1.1.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file logafault-1.1.0.tar.gz.

File metadata

  • Download URL: logafault-1.1.0.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.0 CPython/3.13.1

File hashes

Hashes for logafault-1.1.0.tar.gz
Algorithm Hash digest
SHA256 73e37006dc1b461a1595db79a597e0d0127e226017603d4655ea24a7ff789adf
MD5 d2e2e194fd235c59c572ccc80ddbdd54
BLAKE2b-256 9a5d1d958b59e1bede61450868f8e45d2805cc90e2217e78e5a54fcf99d116e8

See more details on using hashes here.

File details

Details for the file logafault-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: logafault-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.0 CPython/3.13.1

File hashes

Hashes for logafault-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9a832e75b818611d1aa8c5f9d0541ac03010c49aab460f2aeb72b4699e37ef47
MD5 20cc89a0ecfe3feb72f6e0a8fa7cf7a2
BLAKE2b-256 bc36f911e0cde422c381b2d0073698ba0775965f6c5e975740a3d7c9285f2b1a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page