Skip to main content

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

Project description

ampapi-py

Note: This library is 95% generated code with no soul whatsoever. If you're more interested in a nicer developer experience, check out k8thekat's AMPAPI_Python

License Github Github Issues Discord wakatime

Github Releases PyPI PyPI - Downloads PyPI - Python Version PyPI - Wheel

An API that 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.

Support:

Installation

pip install ampapi

You also need the following packages installed:

pip install requests aiohttp json dataclass_wizard

Examples

NOTE: THESE ARE CURRENTLY OUTDATED AFTER THE REWRITE

CommonAPI Example

from ampapi.modules.CommonAPI import CommonAPI

def main():
    # If you know the module that the instance is using, specify it instead of CommonAPI
    API = CommonAPI("http://localhost:8080/", "admin", "myfancypassword123")
    API.Login()

    # 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}%")

main()

Async CommonAPI Example

import asyncio
from ampapi.modules.CommonAPI import CommonAPI

async def main():
    # If you know the module that the instance is using, specify it instead of CommonAPI
    API = CommonAPI("http://localhost:8080/", "admin", "myfancypassword123")
    await API.LoginAsync()

    # 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}%")

asyncio.run(main())

Example using the ADS to manage an instance

from ampapi.modules.ADS import ADS

API = ADS("http://localhost:8080/", "admin", "myfancypassword123")
API.Login()

# Get the available instances
targets = API.ADSModule.GetInstances()

# In this example, my Hub server is on the second target
# If you're running a standalone setup, you can just use targets[1]
target = targets[1]

hub_instance_id = ""

# Get the available instances
instances = target.AvailableInstances
for instance in instances:
    # Find the instance named "Hub"
    if instance.InstanceName == "Hub":
        hub_instance_id = instance.InstanceID
        break

# Use the instance ID to get the API for the instance
Hub = API.InstanceLogin(hub_instance_id, "Minecraft")

# Get the current CPU usage
currentStatus = Hub.Core.GetStatus()
CPUUsagePercent = currentStatus.Metrics["CPU Usage"].Percent

# Send a message to the console
Hub.Core.SendConsoleMessage(f"say Current CPU usage is {CPUUsagePercent}%")

CommonAPI Example, handling the sessionId and rememberMeToken manually (not recommended)

from ampapi.modules.CommonAPI import CommonAPI

try:
    API = CommonAPI("http://localhost:8080/")

    # 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 loginResult.success:
        print("Login successful")
        API.sessionId = loginResult.sessionID
        API.Core.sessionId = loginResult.sessionID

        # 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:
    # In reality, you'd handle this exception better
    raise Exception(err)

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-2602.4.1.tar.gz (38.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-2602.4.1-py3-none-any.whl (37.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ampapi-2602.4.1.tar.gz
  • Upload date:
  • Size: 38.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.12

File hashes

Hashes for ampapi-2602.4.1.tar.gz
Algorithm Hash digest
SHA256 79408b51a15e372f0e3baf955ee5c1bf46b4615e46c9c1ba2e0178a5639a6e9f
MD5 815e9c0fea6554d697eacbe9b2779100
BLAKE2b-256 b880efb9cab14e71bb46282bdea2a74cfe424a53ad836a096c0601429469af21

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ampapi-2602.4.1-py3-none-any.whl
  • Upload date:
  • Size: 37.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.12

File hashes

Hashes for ampapi-2602.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4e47d50a14fe640dfa4ebfd80c5e58cef2c3dace04adabb37e08fab0b6b2e98b
MD5 2c1e3e021f1dd603df36cb8dd29dfc30
BLAKE2b-256 9b8e84dbde602fe1a78970225bea9ba4fb0b6e0096ffa8af3a0bcd5ee9ef2ffa

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