Giter Club home page Giter Club logo

asf_ipc's Introduction

ASF_IPC

license PyPI PyPI ASF

A simple asynchronous Python 3.6+ wrapper of ArchiSteamFarm IPC API

Powered by aiohttp, ASF_IPC is now fully asynchronous. Synchronous methods are no longer supported. ASF_IPC resolves APIs automatically from the new swagger API of ASF, so ASF_IPC always has the latest APIs data as long as ASF is updated.

Examples

Requirements

  • Python 3.6+
  • aiohttp

Installation

pip3 install -U ASF_IPC

Getting Started

This example shows how to send a command to ASF:

import asyncio
from ASF import IPC

async def command(asf, cmd):
    return await asf.Api.Command.post(body={
        'Command': cmd
    })

async def main():
    # The IPC initialization duration depends on the network
    async with IPC(ipc='http://127.0.0.1:1242', password='YOUR IPC PASSWORD') as asf:
        while True:
            cmd = input('Enter a command: ')
            resp = await command(asf, cmd)
            if resp.success:
                print(resp.result)
            else:
                print(f'Error: {resp.message}')

loop = asyncio.get_event_loop()
output = loop.run_until_complete(main())
loop.close()

Find the endpoint

To get a list of all endpoints of ASF, open your web browser and visit the swagger page of your ASF instance (usually http://127.0.0.1:1242/swagger).

You can see many endpoints with their paths, such as /Api/Bot/{botNames}, this endpoint in ASF_IPC is asf.Api.Bot['botNames']. Change separator to . and {} to [], you will get an endpoint object, which has request methods e.g. get(), post() etc.

Note the botNames here is not the value of the argument, to pass value, use kwargs in request methods.

Some more examples:

asf.Api.ASF  # /Api/ASF
asf.Api.Bot['botNames'].Pause  # /Api/Bot/{botNames}/Pause
asf.Api.WWW.GitHub.Release  # /Api/WWW/GitHub/Release
asf.Api.WWW.GitHub.Release['version']  # /Api/WWW/GitHub/Release/{version}

Send a request

After your endpoint found, you want to send some data to this endpoint, you can use get(), post(), put(), delete() methods, these methods have optional arguments:

  • body (dict): the JSON request body.
  • params (dict): the parameters in URL after a ?.

If you need to pass values to the parameters in the path, for example {botName} in /Api/Bot/{botName}/Redeem, you can pass them as kwargs of the method.

Some examples:

# POST /Api/Bot/mainaccount/Pause
await asf.Api.Bot['botNames'].post(botNames='mainaccount')
# GET /Api/WWW/GitHub/Release
await asf.Api.WWW.GitHub.Release.get()
# POST /Api/Bot/robot with json body {'BotConfig': ...}
await asf.Api.Bot['botName'].post(body={'BotConfig': ...}, botName='robot')

Get a response

After sending a request to the endpoint, we got a response object, which has 3 attributes:

  • Result or result (str): some data returned by ASF.
  • Message or message (str): describes what happened with the request.
  • Success or success (bool): if the request has succeeded.

If ASF_IPC cannot give a value to some attributes, these attributes will be None or empty value.

WebSocket endpoint

Example for /Api/NLog:

import asyncio
from ASF import IPC

async def get_log(asf):
    async for resp in asf.Api.NLog.ws():  # use ws() instead of get(), post()...
        if resp.success:
            print(resp.result)

async def main():
    async with IPC(ipc='http://127.0.0.1:1242', password='YOUR IPC PASSWORD') as asf:
        while True:
            await get_log(asf)

loop = asyncio.get_event_loop()
output = loop.run_until_complete(main())
loop.close()

asf_ipc's People

Contributors

deluxghost avatar justarchi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

hicoo 526462712

asf_ipc's Issues

Redeem及Addlicense等命令在asf新版本上无法使用

发送如‘redeem asf 2777’之类的命令后,asf返回‘未知命令’。其他不带参数的命令如version,无问题。
而使用IPC GUI,发送同样命令,asf不会提示未知命令

9.7日时能正常使用,可能是最近的更新修改了相关API?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.