Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add async client #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,31 @@ You need to specify API key and API Secret by creating pybitflyer.API instance.
time_in_force="GTC"
)

Async Module
~~~~~~~~~~~~

If you use Python 3.5.3 or higher, you can use asynchronous client.

.. code:: python

import asyncio
import pybitflyer.async

loop = asyncio.get_event_loop()
api = pybitflyer.async.API(loop=loop)

# send a single request
loop.run_until_complete(api.ticker())

# you can send multiple requests asynchronously
coros = [api.gethealth(), api.ticker()]
results = loop.run_until_complete(asyncio.gather(*coros))
# the results are stored in the same order as coros
for result in results:
print(result)



More detail
~~~~~~~~~~~

Expand Down
67 changes: 67 additions & 0 deletions pybitflyer/async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import time
import json
import hmac
import urllib
import hashlib
import asyncio
import aiohttp
import pybitflyer


class API(pybitflyer.API):

def __init__(self, api_key=None, api_secret=None, timeout=None, loop=None):
super(API, self).__init__(api_key, api_secret, timeout)
self.loop = loop if loop else asyncio.get_event_loop()

def _create_header_and_body(self, endpoint, method, params):
if method == "POST":
body = json.dumps(params)
else:
if params:
body = "?" + urllib.parse.urlencode(params)
else:
body = ""

if self.api_key and self.api_secret:
access_timestamp = str(time.time())
api_secret = str.encode(self.api_secret)
text = str.encode(access_timestamp + method + endpoint + body)
access_sign = hmac.new(api_secret,
text,
hashlib.sha256).hexdigest()
auth_header = {
"ACCESS-KEY": self.api_key,
"ACCESS-TIMESTAMP": access_timestamp,
"ACCESS-SIGN": access_sign,
"Content-Type": "application/json"
}

else:
auth_header = {}

return auth_header, body

# override
async def request(self, endpoint, method="GET", params=None):
url = self.api_url + endpoint
header, body = self._create_header_and_body(endpoint, method, params)
async with aiohttp.ClientSession(loop=self.loop) as s:
try:
if method == 'GET':
async with s.get(url, params=params, timeout=self.timeout, headers=header) as r:
try:
contents = await r.json()
except:
contents = await r.text()
return contents
else: # method == "POST":
async with s.post(url, data=json.dumps(params), timeout=self.timeout, headers=header) as r:
try:
contents = await r.json()
except:
contents = await r.text()
return contents
except Exception as e:
print(e)
raise e
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
author="yag_ays",
author_email="[email protected]",
url="https://github.com/yagays/pybitflyer",
install_requires=['requests'],
install_requires=['requests', 'aiohttp'],
keywords=["bitcoin", "bitflyer", "wrapper", "REST API"],
classifiers=[
"Programming Language :: Python",
Expand Down