Skip to content

Commit

Permalink
Implement async support via client.aquery.
Browse files Browse the repository at this point in the history
Closes #30
  • Loading branch information
jaraco committed Jun 1, 2024
1 parent 12bd0c7 commit 2ccc63c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
1 change: 1 addition & 0 deletions newsfragments/30.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Implement async support via client.aquery.
18 changes: 12 additions & 6 deletions wolframalpha/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class Client:
"""

url = 'https://api.wolframalpha.com/v2/query'

def __init__(self, app_id):
self.app_id = app_id

Expand Down Expand Up @@ -112,13 +114,17 @@ def query(self, input, params=(), **kwargs):
For more details on Assumptions, see
https://products.wolframalpha.com/api/documentation.html#6
"""
data = dict(
input=input,
appid=self.app_id,
)
resp = httpx.get(self.url, params=self.__params(input, **kwargs))
return self.__process(resp)

async def aquery(self, input, **kwargs):
resp = await httpx.aget(self.url, params=self.__params(input, **kwargs))
return self.__process(resp)

def __params(self, input, **kwargs):
return dict(appid=self.app_id, input=input) | kwargs

url = 'https://api.wolframalpha.com/v2/query'
resp = httpx.get(url, params=data | kwargs)
def __process(self, resp):
assert resp.headers['Content-Type'] == 'text/xml;charset=utf-8'
doc = xmltodict.parse(resp.content, postprocessor=Document.make)
return doc['queryresult']
Expand Down

0 comments on commit 2ccc63c

Please sign in to comment.