Skip to main content

A Python implemenation of the Cubecoders AMP API.

Project description

ampapi-python

This implementation is under active development, please feel free to contribute or create an issue if you've found anything that needs fixing

This API allows you to communicate with AMP installations from within Python.

Documentation for available API calls can be found by appending /API to the URL of any existing AMP installation.

Please Note: This program is directly based on the ampapi-node implementation and is almost verbatim in most aspects.

Installation

pip install ampapi

or

pip install 'ampapi @ git+https://github.com/p0t4t0sandwich/ampapi-python.git'

You also need the following packages installed:

pip install requests aiohttp json dataclasses

Async Example

import asyncio
from ampapi.ampapi import AMPAPIAsync

async def start() -> None:
    API = AMPAPIAsync("http://localhost:8080/")

    try:
        # The third parameter is either used for 2FA logins, or if no password is specified to use a remembered token from a previous login, or a service login token.
        loginResult = await API.Core_LoginAsync("admin", "myfancypassword123", "", False)

        if "success" in loginResult.keys() and loginResult["success"]:
            print("Login successful")
            API.sessionId = loginResult["sessionID"]

            # API call parameters are simply in the same order as shown in the documentation.
            await API.Core_SendConsoleMessageAsync("say Hello Everyone, this message was sent from the Python API!")
            currentStatus = await API.Core_GetStatusAsync()
            CPUUsagePercent = currentStatus["Metrics"]["CPU Usage"]["Percent"]
            print(f"Current CPU usage is: {CPUUsagePercent}%")

        else:
            print("Login failed")
            print(loginResult)

    except Exception as err:
        print(err)

asyncio.run(start())

Non-Async Example

from ampapi.ampapi import AMPAPI

def start() -> None:
    API = AMPAPI("http://localhost:8080/")

    try:
        # Perform first-stage API initialization.
        APIInitOK = API.init()

        if not APIInitOK:
            print("API Init failed")
            return

        # The third parameter is either used for 2FA logins, or if no password is specified to use a remembered token from a previous login, or a service login token.
        loginResult = API.Core.Login("admin", "myfancypassword123", "", False)

        if "success" in loginResult.keys() and loginResult["success"]:
            print("Login successful")
            API.sessionId = loginResult["sessionID"]

            # Perform second-stage API initialization, we only get the full API data once we're logged in.
            APIInitOK = API.init()
            if not APIInitOK:
                print("API Stage 2 Init failed")
                return

            # API call parameters are simply in the same order as shown in the documentation.
            API.Core.SendConsoleMessage("say Hello Everyone, this message was sent from the Python API!")
            currentStatus = API.Core.GetStatus()
            CPUUsagePercent = currentStatus["Metrics"]["CPU Usage"]["Percent"]
            print(f"Current CPU usage is: {CPUUsagePercent}%")

        else:
            print("Login failed")
            print(loginResult)

    except Exception as err:
        print(err)

start()

Additional Notes

As you may have noticed, the async and non-async implementations differ quite a bit. This is due to lambda functions within python being unable to use async methods. Usually this can be solved by manually defining the function, but as that would be cumbersome, I've opted to create a script to grab the API spec from the AMP API and generate all the needed functions (script found under /utils/ampapi_async_gen.py).

Async implementation: API.Core_LoginAsync()

Sync implementation: API.Core.Login()

Additonally, unlike other languages, the "Zen of Python" prevents dot/property notation for dictionaries (Think API.Core.Login). I've used dataclasses and some class properties to bypass this, so in the end:

API.Core.Login() == API["Core"]["Login"]().

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

ampapi-1.0.9.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

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

ampapi-1.0.9-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file ampapi-1.0.9.tar.gz.

File metadata

  • Download URL: ampapi-1.0.9.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for ampapi-1.0.9.tar.gz
Algorithm Hash digest
SHA256 3776ca6c5b2e3a7d2da3cff7cf45f2d3b780548ff4c0547c8b28d730572b7888
MD5 d4562b5fad1f79ef79394e010b30b7c8
BLAKE2b-256 b18bac7f148d77539fb2fb920f6fe53fa58cd36f33455997995221bce07cdd9d

See more details on using hashes here.

File details

Details for the file ampapi-1.0.9-py3-none-any.whl.

File metadata

  • Download URL: ampapi-1.0.9-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for ampapi-1.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 09191ea2631440464648d2b1e499b45f1ef8def92535c9da8d20d5141604f987
MD5 967adfef7ec86c048cd25f6b6004f2bc
BLAKE2b-256 8264c1b8854dca2f500e3c0d81bc87910183d3187f4a13606e6a88e7f196d8ea

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