Skip to content
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,23 @@ Check out the example python files or the list of endpoints below for more infor
endpoints and methods. Usage examples on the `HTTP` methods can
be found in the [examples folder](https://github.com/bybit-exchange/pybit/tree/master/examples).

## Async Usage
You can retrieve a specific market like so:
```python
from pybit.asyncio.unified_trading import AsyncHTTP
```
Create an HTTP session:
```python
client = AsyncHTTP(
testnet=False,
api_key="...",
api_secret="...",
)
await client.init_client()
# Or use context-manager
async with AsyncHTTP(testnet=False, api_key="...", api_secret="...") as client:
await client.get_orderbook(category="linear", symbol="BTCUSDT")
```

## Contact
Reach out for support on your chosen platform:
Expand Down
21 changes: 21 additions & 0 deletions examples/async_http_quickstart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import asyncio

from pybit.asyncio.unified_trading import AsyncHTTP

API_KEY = "..."
API_SECRET = "..."


async def test():
async with AsyncHTTP(api_key=API_KEY, api_secret=API_SECRET, testnet=True) as client:
print(await client.get_orderbook(category="linear", symbol="BTCUSDT"))

print(await client.place_order(
category="linear",
symbol="BTCUSDT",
side="Buy",
orderType="Market",
qty="0.001",
))

asyncio.run(test())
34 changes: 34 additions & 0 deletions examples/async_websocket_example_quickstart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import asyncio

from pybit.asyncio.ws import AsyncWebsocketClient


API_KEY = "..."
API_SECRET = "..."


async def test_public():
client = AsyncWebsocketClient(testnet=True, channel_type="linear")
stream = client.futures_kline_stream(symbols=["kline.60.BTCUSDT", "kline.60.ETHUSDT", "kline.60.SOLUSDT"])
async with stream as ws:
while True:
print(await ws.recv())


asyncio.run(test_public())


async def test_private():
client = AsyncWebsocketClient(
testnet=True,
channel_type="private",
api_key=API_KEY,
api_secret=API_SECRET,
)
stream = client.user_futures_stream()
async with stream as ws:
while True:
print(await ws.recv())


asyncio.run(test_private())
Empty file added pybit/asyncio/__init__.py
Empty file.
46 changes: 46 additions & 0 deletions pybit/asyncio/builder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from typing import Optional

from pybit import _http_manager


class RequestBuilder:
def __init__(
self,
api_key: str,
api_secret: str,
rsa_authentication: bool,
proxy: Optional[str] = None
):
self._http_manager = _http_manager._V5HTTPManager(
api_key=api_key,
api_secret=api_secret,
rsa_authentication=rsa_authentication
)
self._proxy = proxy

def clean_query(self, query: Optional[dict]) -> dict:
return self._http_manager._clean_query(query)

def prepare_payload(self, method: str, parameters: dict) -> str:
if not parameters:
return ""
return self._http_manager.prepare_payload(method, parameters)

def _auth(self, payload, recv_window, timestamp):
return self._http_manager._auth(payload, recv_window, timestamp)

def prepare_headers(self, payload: str, recv_window: int) -> dict:
return self._http_manager._prepare_headers(payload, recv_window)

def prepare_request(self, method:str, path: str, headers: dict, payload: str) -> dict:
request = {
"url": path,
"headers": headers,
}
if method == "GET":
request["url"] += f"?{payload}"
else:
request["data"] = payload
if self._proxy:
request["proxy"] = self._proxy
return request
Loading