From 12bd0c7452ef297b156cae9eb8b8884741f2c251 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 1 Jun 2024 06:54:44 -0400 Subject: [PATCH] Switch to httpx to ease async support. Ref #30 --- pyproject.toml | 1 + wolframalpha/__init__.py | 16 ++++++---------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index d4c7e01..280ff4e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,6 +21,7 @@ dependencies = [ "xmltodict", "more_itertools", "jaraco.context", + "httpx", ] dynamic = ["version"] diff --git a/wolframalpha/__init__.py b/wolframalpha/__init__.py index 5114abc..b392252 100644 --- a/wolframalpha/__init__.py +++ b/wolframalpha/__init__.py @@ -2,12 +2,11 @@ import json import getpass import os -import urllib.parse -import urllib.request import contextlib import collections from typing import Dict, Any, Callable, Tuple +import httpx import xmltodict from jaraco.context import suppress from more_itertools import always_iterable @@ -117,14 +116,11 @@ def query(self, input, params=(), **kwargs): input=input, appid=self.app_id, ) - data = itertools.chain(params, data.items(), kwargs.items()) - - query = urllib.parse.urlencode(tuple(data)) - url = 'https://api.wolframalpha.com/v2/query?' + query - resp = urllib.request.urlopen(url) - assert resp.headers.get_content_type() == 'text/xml' - assert resp.headers.get_param('charset') == 'utf-8' - doc = xmltodict.parse(resp, postprocessor=Document.make) + + url = 'https://api.wolframalpha.com/v2/query' + resp = httpx.get(url, params=data | kwargs) + assert resp.headers['Content-Type'] == 'text/xml;charset=utf-8' + doc = xmltodict.parse(resp.content, postprocessor=Document.make) return doc['queryresult']