Skip to content

Commit

Permalink
Fix async
Browse files Browse the repository at this point in the history
  • Loading branch information
Martmists-GH committed Sep 9, 2017
1 parent 520905e commit 2004bdf
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 43 deletions.
82 changes: 40 additions & 42 deletions owo/async_owo.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,27 @@ def async_upload_files(key, *files, **kwargs):
filename=file.lower()
)

with aiohttp.ClientSession(loop=loop) as session:
with (
yield from session.post(BASE_URL+UPLOAD_PATH, data=mp,
params={"key": key},
headers=headers)) as response:
if response.status != 200:
raise ValueError("Expected 200, got {}\n{}".format(
response.status, (yield from response.text())))

for item in (yield from response.json())["files"]:
if item.get("error") is True:
raise ValueError("Expected 200, got {}\n{}".format(
item["errorcode"], item["description"]))

if verbose:
results[item["name"]] = {
base: base+item["url"]
for base in UPLOAD_BASES
}

else:
results[item["name"]] = UPLOAD_STANDARD+item["url"]
session = aiohttp.ClientSession(loop=loop)
response = yield from session.post(BASE_URL+UPLOAD_PATH, data=mp,
params={"key": key},
headers=headers)
if response.status != 200:
raise ValueError("Expected 200, got {}\n{}".format(
response.status, (yield from response.text())))

for item in (yield from response.json())["files"]:
if item.get("error") is True:
raise ValueError("Expected 200, got {}\n{}".format(
item["errorcode"], item["description"]))

if verbose:
results[item["name"]] = {
base: base+item["url"]
for base in UPLOAD_BASES
}

else:
results[item["name"]] = UPLOAD_STANDARD+item["url"]

return results

Expand All @@ -73,26 +72,25 @@ def async_shorten_urls(key, *urls, **kwargs):

results = []

with aiohttp.ClientSession(loop=loop) as session:
for url in urls:
with (
yield from session.get(BASE_URL+SHORTEN_PATH,
params={"action": "shorten",
"url": url,
"key": key},
headers=headers)) as response:
if response.status != 200:
raise ValueError("Expected 200, got {}\n{}".format(
response.status, (yield from response.text())))

path = (yield from response.text()).split("/")[-1]
if verbose:
results.append({
base: base+path
for base in SHORTEN_BASES
})
else:
results.append(SHORTEN_STANDARD + path)
session = aiohttp.ClientSession(loop=loop)
for url in urls:
response = yield from session.get(BASE_URL+SHORTEN_PATH,
params={"action": "shorten",
"url": url,
"key": key},
headers=headers)
if response.status != 200:
raise ValueError("Expected 200, got {}\n{}".format(
response.status, (yield from response.text())))

path = (yield from response.text()).split("/")[-1]
if verbose:
results.append({
base: base+path
for base in SHORTEN_BASES
})
else:
results.append(SHORTEN_STANDARD + path)

return results

Expand Down
2 changes: 1 addition & 1 deletion test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import owo


key = os.environ["TRAVIS_KEY"]
key = os.environ.get("TRAVIS_KEY") or "1ae1c6d8-cc48-4fc4-8724-52b8f85d9847"
loop = asyncio.get_event_loop()


Expand Down

0 comments on commit 2004bdf

Please sign in to comment.