AMPAPI_Python_wrapper
CubeCoders AMP API wrapper in Python.
Key Features
- Pythonic API wrapper using
async
and await
.
- Data is in dataclasses for easier management and interaction.
- Optional parameter per function or global to disable formatting of data.
- Parent classes
ADSInstance
and AMPInstance
to group endpoints together and make handling of multiple Instances easier.
- This will also limit Instance specific API endpoints (eg. Minecraft) to that Instance type only.
Installing
Python 3.9 or higher is required
To install run the below command to install the required pip packages from Requirements
Pypi
-> https://pypi.org/project/cubecoders-amp-api-wrapper/
pip install cubecoders-amp-api-wrapper
pip install cubecoders-amp-api-wrapper
Basic Usage
- First you need to fill out the APIParams class with the required fields (url, user and password).
- Pass the APIParams class into the Bridge class parameter api_params.
- You only need to make ONE bridge class; the rest of the API classes will get the same object and handle logging in for you.
- You can then use the Parent class ADSInstance() or the smaller class AMPInstance() or any of the API classes as a stand alone.
Quick Example -
_params = APIParams(url="http://192.168.13.130:8080",
user="amp_username",
password="amp_password")
_bridge = Bridge(api_params=_params)
async def Sample_API():
"""
Example Method to call Instance Endpoints and create the ADS Instance class.
"""
ADS: ADSInstance = ADSInstance()
await ADS.get_instances()
arkinstance: AMPInstance | AMPMinecraftInstance = ADS.AvailableInstances[1]
mcinstance: AMPInstance | AMPMinecraftInstance = ADS.AvailableInstances[2]
await arkinstance.take_backup(title="ARK1_backup", description="This is an ARK backup", sticky=True)
await mcinstance.mc_add_to_whitelist(user_or_uuid="k8_thekat")
mcinstance.InstanceName
mcinstance.InstanceID
if mcinstance.AppState == State_enum.Stopped:
await mcinstance.start_instance()
mcinstance.Status.Metrics
players: list[Players] = await mcinstance.get_user_list()
await mcinstance.mc_kick_user_by_id(id=players[0].id)
async def Sample_Analytics_API():
"""
Example Method to use Instance Analytics.
"""
ADS: ADSInstance = ADSInstance()
await ADS.get_instances()
mcinstance: AMPInstance | AMPMinecraftInstance = ADS.AvailableInstances[2]
Analytics: Analytics_Summary = await mcinstance.get_analytics_summary()
Analytics.topPlayers
Analytics.stats
country_filter: Analytics_Filter = Analytics_Filter(Country="US")
filtered_analytics: Analytics_Summary = await mcinstance.get_analytics_summary(filters=country_filter)
user_filter: Analytics_Filter = Analytics_Filter(Username="k8_thekat")
filtered_analytics2: Analytics_Summary = await mcinstance.get_analytics_summary(filters=user_filter)
Controlling data formatting -
- All JSON data returned from the API endpoints is formatted into a Dataclass/Enum/etc if possible.
- Data formatting can be controlled globally or locally by changing the
format_data
parameter of the method to False
.
- When turning off the data formatting, typically you will get a
dictionary
back.
Example -
ADS.format_data = False
arkinstance.format_data = True
await arkinstance.get_updates(format_data=False)
await arkinstance.get_role_data(format_data=True)