Fast API Clients for Python
Project description
neoclient
🚀 Fast API Clients for Python
Installation
pip install neoclient
Documentation
Introduction
The simplest neoclient
file could look like this:
from neoclient import get
@get("https://httpbin.org/ip")
def ip():
...
>>> ip()
{'origin': '1.2.3.4'}
However, it's almost always better to create a NeoClient
instance for easy reusability:
from neoclient import NeoClient
client = NeoClient("https://httpbin.org/")
@client.get("/ip")
def ip():
...
>>> ip()
{'origin': '1.2.3.4'}
Path Parameters
You can declare path parameters with the same syntax used by Python format strings:
from neoclient import NeoClient
client = NeoClient("https://httpbin.org/")
@client.get("/base64/{value}")
def b64decode(value):
...
>>> b64decode("RmFzdENsaWVudCBpcyBhd2Vzb21lIQ==")
'NeoClient is awesome!'
Path parameters with types
You can declare the type of a path parameter in the function using standard Python type annotations:
from neoclient import NeoClient
client = NeoClient("https://httpbin.org/")
@client.get("/base64/{value}")
def b64decode(value: str):
...
In this case, value
is declared to be of type str
.
Explicit path parameters
If you prefer explicitly stating that a parameter is a path parameter, you can use the Path
parameter function:
from neoclient import NeoClient, Path
client = NeoClient("https://httpbin.org/")
@client.get("/base64/{value}")
def b64decode(value: str = Path()):
...
This approach comes with added benefits, such as being able to specify validation constraints.
from neoclient import NeoClient, Path
client = NeoClient("https://httpbin.org/")
@client.get("/base64/{value}")
def b64decode(value: str = Path(default="SGVsbG8sIFdvcmxkIQ==")):
...
>>> b64decode()
'Hello, World!'
Missing path parameters
NeoClient will throw an error if you specify a path parameter in the request path, however do not create a function parameter for it. For example:
from neoclient import NeoClient
client = NeoClient("https://httpbin.org/")
@client.get("/base64/{value}")
def b64decode():
...
>>> b64decode()
IncompatiblePathParameters: Expected ('value',), got ()
Pre-defined Values
If you have a path operation that receives a path parameter, but you want the possible valid path parameter values to be predefined, you can use a standard Python Enum.
from enum import Enum
from neoclient import NeoClient
class Name(str, Enum):
def __str__(self):
return self.value
BOB = "bob"
SALLY = "sally"
client = NeoClient("https://httpbin.org/")
@client.get("/anything/{name}")
def anything(name: Name):
...
>>> anything(Name.BOB)
{
'args': {},
'data': '',
'files': {},
'form': {},
'headers': {
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate, br',
'Host': 'httpbin.org',
'User-Agent': 'neoclient/0.1.10'
},
'json': None,
'method': 'GET',
'origin': '1.2.3.4',
'url': 'https://httpbin.org/anything/bob'
}
Path parameters containing paths
Let's say you have a path operation with a path /do/{commands}
, where you expect commands
to accept an arbitrary number of arguments which should be used as path segments. To achieve this, you can pass the path parameter a sequence:
from typing import Sequence
from neoclient import NeoClient
client = NeoClient("https://httpbin.org/")
@client.get("/anything/{commands}")
def do(commands: Sequence[str]):
...
>>> do(["turn", "left", "then", "turn", "right"])
{
'args': {},
'data': '',
'files': {},
'form': {},
'headers': {
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate, br',
'Host': 'httpbin.org',
'User-Agent': 'neoclient/0.1.10'
},
'json': None,
'method': 'GET',
'origin': '1.2.3.4',
'url': 'https://httpbin.org/anything/turn/left/then/turn/right'
}
Query Parameters
When you declare other function parameters that are not part of the path parameters, they are automatically interpreted as "query" parameters.
from neoclient import NeoClient
app = NeoClient("https://httpbin.org/")
@app.get("/get")
def get(message: str):
...
>>> get("Hello, World!")
{
'args': {'message': 'Hello, World!'},
'headers': {
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate, br',
'Host': 'httpbin.org',
'User-Agent': 'neoclient/0.1.10'
},
'origin': '1.2.3.4',
'url': 'https://httpbin.org/get?message=Hello%2C+World!'
}
Advanced User Guide
NeoClient can turn your HTTP API into a Python protocol.
from neoclient import NeoClient, get
from typing import Protocol
class Httpbin(Protocol):
@get("/get")
def get(self, message: str) -> dict:
...
httpbin: Httpbin = NeoClient("https://httpbin.org/").create(Httpbin) # type: ignore
>>> httpbin.get("Hello, World!")
{
'args': {'message': 'Hello, World!'},
'headers': {
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate, br',
'Host': 'httpbin.org',
'User-Agent': 'neoclient/0.1.10'
},
'origin': '1.2.3.4',
'url': 'https://httpbin.org/get?message=Hello%2C+World!'
}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for neoclient-0.1.13-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2b3e2ad15a8d766cb2180d2879dc34853a8eb7b52921158e1628e51f25ab9e89 |
|
MD5 | 7b21aad82e702a4b30557320f7449bc2 |
|
BLAKE2b-256 | c94006c0a0b45888a7c62fade8bdc788388d42b82ce274726e35642e9a4d7508 |