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
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
CommonAPI Example
from ampapi.auth import BasicAuthProvider
from ampapi.modules import CommonAPI
from ampapi.types import MetricInfo, StatusResponse
def main():
authProvider = BasicAuthProvider(
panelUrl="http://localhost:8080/",
username="admin",
password="myfancypassword123"
)
API = CommonAPI(authProvider)
API.Core.SendConsoleMessage("say Hello Everyone, this message was sent from the Python API!")
currentStatus: StatusResponse = API.Core.GetStatus()
cpuUsage: MetricInfo = currentStatus.Metrics.get("CPU Usage")
print(f"Current CPU usage is: {cpuUsage.Percent}%")
main()
Async CommonAPI Example
import asyncio
from ampapi.auth import BasicAuthProviderAsync
from ampapi.modules import CommonAPIAsync
from ampapi.types import MetricInfo, StatusResponse
async def main():
authProvider = BasicAuthProviderAsync(
panelUrl="http://localhost:8080/",
username="admin",
password="myfancypassword123"
)
API = CommonAPIAsync(authProvider)
await API.Core.SendConsoleMessage("say Hello Everyone, this message was sent from the Python API!")
currentStatus: StatusResponse = await API.Core.GetStatus()
cpuUsage: MetricInfo = currentStatus.Metrics.get("CPU Usage")
print(f"Current CPU usage is: {cpuUsage.Percent}%")
asyncio.run(main())
THE BELOW EXAMPLES ARE OUTDATED
Example using the ADS to manage an instance
from ampapi.modules.ADS import ADS
API = ADS("http://localhost:8080/", "admin", "myfancypassword123")
API.Login()
targets = API.ADSModule.GetInstances()
target = targets[1]
hub_instance_id = ""
instances = target.AvailableInstances
for instance in instances:
if instance.InstanceName == "Hub":
hub_instance_id = instance.InstanceID
break
Hub = API.InstanceLogin(hub_instance_id, "Minecraft")
currentStatus = Hub.Core.GetStatus()
CPUUsagePercent = currentStatus.Metrics["CPU Usage"].Percent
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/")
loginResult = API.Core.Login("admin", "myfancypassword123", "", False)
if loginResult.success:
print("Login successful")
API.sessionId = loginResult.sessionID
API.Core.sessionId = loginResult.sessionID
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:
raise Exception(err)