Skip to content

Commit 7075d0a

Browse files
author
thenamesweretakenalready
committed
🐛 Fix broken tests, previously unknown errors
1 parent fb180e5 commit 7075d0a

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

pyproject.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
[tool.poetry]
22
name = "jokeapi"
33
version = "1.0.0"
4-
description = ""
4+
description = "Python API Wrapper for Sv443's JokeAPI (https://v2.jokeapi.dev)"
55
authors = ["thenamesweretakenalready <[email protected]>"]
66
license = "GPL v3.0"
77

88
[tool.poetry.dependencies]
99
python = "^3.9"
10-
anyio = "^3.1.0"
10+
urllib3 = "^1.26.2"
1111
aiohttp = "^3.7.4"
1212
simplejson = "^3.17.2"
1313
black = "^21.6b0"
1414
pytest = "^6.2.4"
15-
pytest-asyncio = "^5.4.0"
15+
pytest-asyncio = "^0.15.1"
1616

1717
[tool.poetry.dev-dependencies]
1818

src/jokeapi/main.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import urllib
12
import aiohttp
23
import simplejson as json
34
import re
4-
import pprint
55

66

77
class CategoryError(Exception):
@@ -37,6 +37,21 @@ async def post(session, url, data, headers=None):
3737
return response_text, response.headers
3838

3939

40+
# https://stackoverflow.com/a/36724229
41+
class AsyncIterator:
42+
def __init__(self, seq):
43+
self.iter = iter(seq)
44+
45+
def __aiter__(self):
46+
return self
47+
48+
async def __anext__(self):
49+
try:
50+
return next(self.iter)
51+
except StopIteration:
52+
raise StopAsyncIteration
53+
54+
4055
class Joke_Class:
4156
async def init(self):
4257
async with aiohttp.ClientSession() as session:
@@ -56,10 +71,10 @@ async def build_request(
5671
safe_mode=False,
5772
lang="en",
5873
):
59-
r = "https://v2.jokapi.dev/joke/"
74+
r = "https://v2.jokeapi.dev/joke/"
6075

6176
if len(category):
62-
async for c in category:
77+
async for c in AsyncIterator(category):
6378
if not c.title() in self.info["categories"]:
6479
raise CategoryError(
6580
f'''Invalid category selected.
@@ -185,9 +200,10 @@ async def send_request(
185200
print(r)
186201
raise
187202
else:
203+
data = r
188204
if (
189205
len(
190-
" ".join(re.split("error", r.lower())[0:][1:])
206+
" ".join(re.split("error", data.lower())[0:][1:])
191207
.replace("<", "")
192208
.replace("/", "")
193209
.replace(" ", "")
@@ -305,12 +321,9 @@ async def submit_joke(
305321
request["lang"] = lang
306322

307323
data = json.dumps(request).replace("'", '"')
308-
print(data)
309324
data = data.encode("ascii")
310325
url = f"https://v2.jokeapi.dev/submit{'?dry-run'*dry_run}"
311326

312-
313-
pprint.pprint(data)
314327
if auth_token:
315328
headers = {
316329
"Authorization": str(auth_token),

0 commit comments

Comments
 (0)