-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop': v 1.1.5. Quota status and delete image
- Loading branch information
Showing
10 changed files
with
154 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,4 @@ | |
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) | ||
|
||
|
||
__version__ = '1.1.4' | ||
__version__ = '1.1.5' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from attr import attrs, attrib | ||
from . import AliceObject | ||
|
||
|
||
@attrs | ||
class Quota(AliceObject): | ||
"""Quota object. Values in bytes""" | ||
total = attrib(type=int) | ||
used = attrib(type=int) | ||
available = attrib(init=False, default=0) | ||
|
||
def __attrs_post_init__(self): | ||
self.available = self.total - self.used |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import asyncio | ||
import logging | ||
|
||
from aioalice import Dispatcher | ||
|
||
logging.basicConfig(format=u'%(filename)s [LINE:%(lineno)d] #%(levelname)-8s [%(asctime)s] %(message)s', | ||
level=logging.INFO) | ||
|
||
|
||
# Provide your own skill_id and token! | ||
SKILL_ID = '12a34567-c42d-9876-0e3f-123g55h12345' | ||
OAUTH_TOKEN = 'OAuth AQAAAAABCDEFGHI_jklmnopqrstuvwxyz' | ||
|
||
# You can create dp instance without auth: `dp = Dispatcher()`, | ||
# but you'll have to provide it in request | ||
dp = Dispatcher(skill_id=SKILL_ID, oauth_token=OAUTH_TOKEN) | ||
|
||
|
||
async def check_quota_status_and_delete_image(): | ||
try: | ||
# Use `await dp.get_images_quota(OAUTH_TOKEN)` | ||
# If token was not provided on dp's initialisation | ||
quota_before = await dp.get_images_quota() | ||
|
||
# Use `await dp.delete_image(image_id, SKILL_ID, OAUTH_TOKEN)` | ||
# If tokens were not provided on dp's initialisation | ||
success = await dp.delete_image('1234567/ff123f70e0e0cf70079a') | ||
|
||
# recheck to see the difference | ||
quota_after = await dp.get_images_quota() | ||
except Exception: | ||
logging.exception('Oops!') | ||
else: | ||
print(quota_before) | ||
print('Success?', success) | ||
print(quota_after) | ||
print('Freed up', quota_after.available - quota_before.available, 'bytes') | ||
|
||
''' Output: | ||
Quota(total=104857600, used=10423667, available=94433933) | ||
Success? True | ||
Quota(total=104857600, used=10383100, available=94474500) | ||
Freed up 40567 bytes | ||
''' | ||
|
||
# You have to close session manually | ||
# if you called any request outside web app | ||
# Session close is added to on_shutdown list | ||
# in webhhok.configure_app | ||
await dp.close() | ||
|
||
|
||
loop = asyncio.get_event_loop() | ||
tasks = [ | ||
loop.create_task(check_quota_status_and_delete_image()), | ||
] | ||
wait_tasks = asyncio.wait(tasks) | ||
loop.run_until_complete(wait_tasks) | ||
loop.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters