Skip to content

Commit

Permalink
add async API
Browse files Browse the repository at this point in the history
  • Loading branch information
rintaro committed Mar 25, 2018
1 parent 0585824 commit 695134d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
61 changes: 61 additions & 0 deletions pybitflyer/async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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:
contents_json = await r.json()
return contents_json
else: # method == "POST":
async with s.post(url, data=json.dumps(params), timeout=self.timeout, headers=header) as r:
contents_json = await r.json()
return contents_json
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

0 comments on commit 695134d

Please sign in to comment.