From 2ccc63c1058f8c1b84b7e48e491d8e067ec4607e Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 1 Jun 2024 07:03:04 -0400 Subject: [PATCH] Implement async support via client.aquery. Closes #30 --- newsfragments/30.feature.rst | 1 + wolframalpha/__init__.py | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) create mode 100644 newsfragments/30.feature.rst diff --git a/newsfragments/30.feature.rst b/newsfragments/30.feature.rst new file mode 100644 index 0000000..ba39301 --- /dev/null +++ b/newsfragments/30.feature.rst @@ -0,0 +1 @@ +Implement async support via client.aquery. \ No newline at end of file diff --git a/wolframalpha/__init__.py b/wolframalpha/__init__.py index b392252..288d4b1 100644 --- a/wolframalpha/__init__.py +++ b/wolframalpha/__init__.py @@ -67,6 +67,8 @@ class Client: """ + url = 'https://api.wolframalpha.com/v2/query' + def __init__(self, app_id): self.app_id = app_id @@ -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']