async def test_client_ws_connect(mocker: pytest_mock.MockerFixture): runner_mock = mocker.Mock() runner_mock.wait = AsyncMock() m = mocker.patch("pybotters.client.WebSocketRunner", return_value=runner_mock) hdlr_str = mocker.Mock() hdlr_bytes = mocker.Mock() hdlr_json = mocker.Mock() async with pybotters.Client() as client: ret = await client.ws_connect( "ws://test.org", send_str='{"foo":"bar"}', send_bytes=b'{"foo":"bar"}', send_json={"foo": "bar"}, hdlr_str=hdlr_str, hdlr_bytes=hdlr_bytes, hdlr_json=hdlr_json, ) assert m.called assert m.call_args == [ ("ws://test.org", client._session), { "send_str": '{"foo":"bar"}', "send_bytes": b'{"foo":"bar"}', "send_json": { "foo": "bar" }, "hdlr_str": hdlr_str, "hdlr_bytes": hdlr_bytes, "hdlr_json": hdlr_json, }, ] assert ret == runner_mock
async def main(): async with pybotters.Client(base_url='https://api.coin.z.com', apis=apis) as bot_client: store = pybotters.GMOCoinDataStore() await store.initialize( bot_client.get('/private/v1/positionSummary', params={'symbol': 'BTC_JPY'})) token = await connect_gmo_private_ws(bot_client, store.onmessage) last_put_pos = last_extend_token = time.time() last_pos = 0 db_client = InfluxDBClient(host='localhost', port=8086, database='bots') while True: start = time.time() pos = calc_pos(store.position_summary.find()) if pos != last_pos or last_put_pos + 60 < start: last_pos = pos last_put_pos = start data = [{ "measurement": "gmo_collateral", "fields": { "position": pos } }] db_client.write_points(data) # print(data) if last_extend_token + 600 < start: last_extend_token = start await extend_accesstoken(bot_client, token) await store.position_summary.wait()
async def test_client_request_get(mocker: pytest_mock.MockerFixture): patched = mocker.patch("aiohttp.client.ClientSession._request") async with pybotters.Client() as client: ret = client.request("GET", "http://example.com", params={"foo": "bar"}) assert patched.called assert isinstance(ret, aiohttp.client._RequestContextManager)
async def test_client_warn(mocker: pytest_mock.MockerFixture): apis = {"name1", "key1", "secret1"} base_url = "http://example.com" async with pybotters.Client(apis=apis, base_url=base_url) as client: # type: ignore assert isinstance(client._session, aiohttp.ClientSession) assert not client._session.closed assert client._base_url == base_url assert client._session.closed assert client._session.__dict__["_apis"] == {}
async def order_book_l2(symbol: str) -> dict: """オーダーブックを取得""" async with pybotters.Client(base_url=base_url, apis=apis) as client: r = await client.get( "/orderBook/L2", params={ "symbol": symbol, }, ) data = await r.json() return data
async def get_position_list(symbol: str) -> dict: """ポジションを取得""" async with pybotters.Client(base_url="https://api-testnet.bybit.com/", apis="apis.json") as client: r = await client.get( "/v2/private/position/list", params={ "symbol": symbol, }, ) data = await r.json() return data
async def order_book_l2(symbol: str) -> dict: """オーダーブックを取得""" async with pybotters.Client(base_url="https://api-testnet.bybit.com/", apis="apis.json") as client: r = await client.get( "/v2/public/orderBook/L2", params={ "symbol": symbol, }, ) data = await r.json() return data
async def order_cancel_all(symbol): async with pybotters.Client(base_url="https://api-testnet.bybit.com/", apis="apis.json") as client: r = await client.post( "/v2/private/order/cancelAll", data={ "symbol": symbol, }, ) data = await r.json() logging.debug(pprint.pformat(data)) return data
async def on_status(self, status): if 44196397 == status.user.id and "DOGE" in status.text: if discord is not None: discord.post(content="Elon Musk mentioned DOGE!! \n" + status.text) else: pass if FTX_API_KEY == None or FTX_API_SECRET == None: print('no env') sys.exit(1) else: async with pybotters.Client( apis=api, base_url='https://ftx.com/api') as client: resp = await client.post('/orders', data=parameters) print(resp) else: pass
async def test_client(): apis = { "name1": ["key1", "secret1"], "name2": ["key2", "secret2"], "name3": ["key3", "secret3"], } base_url = "http://example.com" async with pybotters.Client(apis=apis, base_url=base_url) as client: assert isinstance(client._session, aiohttp.ClientSession) assert not client._session.closed assert client._base_url == base_url assert client._session.closed assert client._session.__dict__["_apis"] == { "name1": tuple(["key1", "secret1".encode()]), "name2": tuple(["key2", "secret2".encode()]), "name3": tuple(["key3", "secret3".encode()]), } assert [tuple(x) for x in apis.values() ] != [x for x in client._session.__dict__["_apis"].values()]
async def order_create(symbol, side, order_qty, price, order_type="Limit", time_in_force="GoodTillCancel"): async with pybotters.Client(base_url=base_url, apis=apis) as client: r = await client.post( "/order", data={ "symbol": symbol, "side": side, "orderQty": order_qty, "price": price, "ordType": order_type, "timeInForce": time_in_force, }, ) data = await r.json() logging.debug(pprint.pformat(data)) return data
async def order_create(symbol, side, qty, price, order_type="Limit", time_in_force="GoodTillCancel"): async with pybotters.Client(base_url="https://api-testnet.bybit.com/", apis="apis.json") as client: r = await client.post( "/v2/private/order/create", data={ "side": side, "symbol": symbol, "order_type": order_type, "qty": int(qty), "price": float(price), "time_in_force": time_in_force, }, ) data = await r.json() logging.debug(pprint.pformat(data)) return data
async def order_cancel_all(symbol): async with pybotters.Client(base_url=base_url, apis=apis) as client: r = await client.delete("/order/all", data={"symbol": symbol}) data = await r.json() return data
async def get_open_orders(symbol: str) -> dict: async with pybotters.Client(base_url=base_url, apis=apis) as client: r = await client.get("/order", data={"symbol": symbol}) data = await r.json() return data
async def get_position_list(symbol: str) -> list: """ポジションを取得""" async with pybotters.Client(base_url=base_url, apis=apis) as client: r = await client.get("/position", params={"symbol": symbol}) data = await r.json() return data