📜 ⬆️ ⬇️

Submit the cool project: your API-client from one configuration file


Hi, Habrahabr! I recently made a tool here that generates a whole client wrapper over any API that you want from a single yml file, and then immediately loads it into the Python Package Index (PyPi). Yes, the result can be set in any project with the help of pip and start using. More under the cut!

Example


Look at the code of fictional http requests to the Game of Thrones API server that you would write using the requests library.

import requests create_jon_snow_user = requests.post( 'https://gameofthrones.com/api/v1/user', params={ 'id': 7, 'name': 'Jon', 'surname': 'Snow', } ) get_jon_snow_user = requests.get( 'https://gameofthrones.com/api/v1/user', params={'id': 7} ) create_jon_snow_castle = requests.post( 'https://gameofthrones.com/api/v1/user/castle', params={ 'id': 7, 'castle': 'Winterfell', } ) 

And now for the code that makes it possible to use syntactic, comfortable for the client (developer), constructions.

 from gameofthrones_api import gameofthrones_api_client as got_api_client create_jon_snow_user = got_api_alient.user.create({ 'id': 7, 'name': 'Jon', 'surname': 'Snow' }) get_jon_snow_user = got_api_alient.user.get({'id': 7}) create_jon_snow_castle = got_api_alient.user.castle.create({ 'id': 7, 'castle': 'Winterfell', }) 

How it works


This feature opens the acg tool ( link to Github ), but for this it needs a file called .acg.yml in the following form:
')
 pypi: username: dmytrostriletskyi password: d843rnd3 acg: name: gameofthronesapi version: 0.1.5 api: https://gameofthrones.com/api/v1 services: user: url: /user endpoints: create:post, get:get user.castle: url: /user/castle endpoints: create:post 

You specify your PyPi username and password so that acg downloads the client to your account and this package can be available for installation via pip - acg writes the parameters to the .pypirc file that is needed to download your package to the Python Package Index (PyPi).

Now to the back-end server to which you are writing the client:


Acg installation


You can install acg using pip.

 $ pip install acg 

Pip3:

 $ pip3 install acg 

Or compile the source code:

 $ python setup.py install 

Acg command


We created .acg.yml , entered our settings there, opened the terminal in the configuration directory:

 $ acg 

You also continue to work with acg if you want to update the configuration and client accordingly. Remember that for each new download you need to change the version of your client package. Do not forget to use the flag --no-cache-dir or -U when installing a new version of your package.

In order to avoid conflicts, I advise you to delete all the generated projects (folders) of your project (each time acg builds the project again).

How to install and use your client


Installation will require the familiar `pip install {name}` command, where the package name will be the name value in the configuration file.

It is also easy to use, import the object with all the necessary parameters from its installed module. The client always repeats the package name, but complements itself with the _client part.

For example, you wrote configurations for the project telegrambotapi .
Then use the client in the Python code like this:

 from telegrambotapi import telegrambotapi_client 

Source: https://habr.com/ru/post/339150/


All Articles