A Python implemenation of the Cubecoders AMP API.
Project description
ampapi-python
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.
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
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
ADS = ADS("http://localhost:8080/", "admin", "myfancypassword123", "", "")
# Get the available instances
instancesResult = ADS.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 = ADS.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)
TODO
- Add a check to see if it's been 5min since the last API call, and if so, attempt to re-log
Project details
Release history Release notifications | RSS feed
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.0.tar.gz
(31.4 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
ampapi-1.2.0-py3-none-any.whl
(35.8 kB
view details)
File details
Details for the file ampapi-1.2.0.tar.gz.
File metadata
- Download URL: ampapi-1.2.0.tar.gz
- Upload date:
- Size: 31.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dadb969382526226c6570a03ed33482e899c200248ac10a4c66ebf521d010450
|
|
| MD5 |
96f3005f3a40e95b1b8200e7e240d113
|
|
| BLAKE2b-256 |
082ff73be6120061fda22356b82c8b2e9caa027f533ee19b04af5a4452f34715
|
File details
Details for the file ampapi-1.2.0-py3-none-any.whl.
File metadata
- Download URL: ampapi-1.2.0-py3-none-any.whl
- Upload date:
- Size: 35.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b2c5af0ec752fe8326e9fb7ecdcfd4e6aac8f34092bc24e016b8b335a7fd3c84
|
|
| MD5 |
067d519d41805525736587c1cfa5ad89
|
|
| BLAKE2b-256 |
64db79036b6ad742e4f84b6d18d6fd51899e79a72c6b14b4ac5eb9b1b212f451
|