Skip to content

nadvotsky/indx-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

indx-api

Summary

This repository contains a Python interface to the INDX Internet-Exchange API.

It is an asynchronous module that supports Python 3.8+ and utilizes AIOHTTP and pydantic.

Implementation Details

The core idea of this project is to automate the entire process of creating language-specific mappings of different API entities and simplify the process of adding new entries or adapting to changes in the API.

This is achieved by using code generation tools, specifically datamodel-code-generator, which translates the OpenAPI schema file into pydantic models.

However, the tools listed above do not fulfill the required need and require manual changes to the generated codebase. For instance, enumerations are converted 'as is', resulting in a sequence that is hard to read and use, such as NUMBER__1 for -1.

Having a semi-automated generation process brings many inconveniences. The self-written solution in this project allows the automation of the generation process entirely, yet customizing the resulting code. By utilizing the Abstract Syntax Tree (AST), the following is possible:

  • Splitting declarations into separate files based on schema prefix.
  • Injecting custom code as a replacement for existing classes.
  • A base for controlling the process of generated entities, allowing easy modification of keywords, model configurations, aliases, and more.

Usage

To start using it, initiate the ClientSession from aiohttp and initialize the Client class.

A request to an endpoint can be made using modules inside indx_api.schema, such as indx_api.schema.balance. Each of such modules typically exports Request, Response, and maybe other classes used to form a request or response.

In Python, it means:

import aiohttp
import asyncio

from indx_api import Client
from indx_api.schema.balance import Request, Response

async def main():
    async with aiohttp.ClientSession() as session:
        client = Client(
            session=session,
            login="<...>",
            password="<...>",
            wmid="<...>",
        )
        
        response: Response = await client.request(Request())
        print(response.profit)

asyncio.run(main())

Also, refer to the playground.py for a more comprehensive example.

Developing

This project is configured to use PDM, a modern Python package and dependency manager. It supports pyproject.toml and makes the overall experience trouble-free.

To download all dependencies and start developing, use the following command:

pdm install

More information can be found in the PDM documentation.

Schema Generation

The OpenAPI schema file is located at src/indx_api/generator/openapi/openapi.toml.

The process of generating indx_api.schema source files can be invoked via the following:

pdm run generate

Wheel Building

One of the advantages of using PDM is the ability to also use PDM-Backend, a handy built-in backend that supports the latest packaging standards.

The build process can be started as follows:

pdm build

Please refer to the PDM-Backend page for more information.

License

This project is licensed under the MIT License. You are free to use, modify, and distribute this software, but please provide attribution.

About

A Python implementation of the INDX Internet-Exchange API using OpenAPI schema transformation.

Topics

Resources

License

Stars

Watchers

Forks

Contributors

Languages