Skip to main content

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

Project description

ampapi-py

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

or

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

You also need the following packages installed:

pip install requests aiohttp json

Notes

Currently, you can only access API responses as dict or list, still trying to figure out a good way to generalize turning the JSON responses into objects (might just use class constructors as a cheap workaround).

Examples

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("Current CPU usage is: " + str(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("Current CPU usage is: " + str(CPUUsagePercent) + "%")

asyncio.run(main())

Example using the ADS to manage an instance

from ampapi.modules.ADS import ADS
from ampapi.modules.Minecraft import Minecraft

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

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

targets = instancesResult["result"]

# 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("say Current CPU usage is: " + CPUUsagePercent + "%")

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

from ampapi.ampapi import AMPAPI

try:
    API = AMPAPI("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 "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.
        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-1.2.2.tar.gz (32.6 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.2.2-py3-none-any.whl (36.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for ampapi-1.2.2.tar.gz
Algorithm Hash digest
SHA256 d83f7526837284b7eae8eb58a180a036fe06a8a0a13c30486c82cfc954f242a2
MD5 2f1a8d786f658e4b0e2b54d5c04e85b3
BLAKE2b-256 508c2da339e266650cda0ccd136039cdccf18bc3525edff4fc5ed81003cfc541

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for ampapi-1.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e31ff8be7fd963a4109eefde3cf3eee3786bb4a0b3c5fb429e64ddf5729fa183
MD5 2ca5557c58f5cb1337d6a5d6d3da093f
BLAKE2b-256 14756cdab5da4e7b164f0e8666e5c93e65cc457c96ed05c5aa7cf4838280bbc5

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