From 3f5560dcc23d01d84361b0e26417e684fa6caf07 Mon Sep 17 00:00:00 2001 From: AlekseevAV Date: Wed, 10 Oct 2018 13:28:37 +0300 Subject: [PATCH 1/9] Fix rename EntityToken -> EntityTokens --- aioalice/types/entity.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aioalice/types/entity.py b/aioalice/types/entity.py index 28d0bee..ddeb1c5 100644 --- a/aioalice/types/entity.py +++ b/aioalice/types/entity.py @@ -3,7 +3,7 @@ from aioalice.utils import ensure_cls from aioalice.utils.helper import Helper, HelperMode, Item -from . import AliceObject, EntityToken, EntityValue +from . import AliceObject, EntityTokens, EntityValue log = logging.getLogger(__name__) @@ -12,7 +12,7 @@ class Entity(AliceObject): """Entity object""" type = attrib(type=str) - tokens = attrib(convert=ensure_cls(EntityToken)) + tokens = attrib(convert=ensure_cls(EntityTokens)) value = attrib(convert=ensure_cls(EntityValue)) @type.validator From 1add832e531d1395080d3dd8ae0705dbedf7b586 Mon Sep 17 00:00:00 2001 From: ASMfreaK Date: Sun, 14 Oct 2018 18:19:44 +0300 Subject: [PATCH 2/9] Fix tests add support for `web_request` field in AliceRequest Signed-off-by: ASMfreaK --- .gitignore | 1 + aioalice/dispatcher/webhook.py | 2 +- aioalice/types/alice_request.py | 3 +- tests/test_types.py | 49 +++++++++++++++++---------------- 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index 894a44c..d62d9fb 100644 --- a/.gitignore +++ b/.gitignore @@ -102,3 +102,4 @@ venv.bak/ # mypy .mypy_cache/ +Pipfile* diff --git a/aioalice/dispatcher/webhook.py b/aioalice/dispatcher/webhook.py index 9893b8a..44c5baa 100644 --- a/aioalice/dispatcher/webhook.py +++ b/aioalice/dispatcher/webhook.py @@ -56,7 +56,7 @@ async def parse_request(self): """ data = await self.request.json() try: - return AliceRequest(**data) + return AliceRequest(self.request, **data) except Exception: log.exception('Exception loading AliceRequest from\n%r', data) raise diff --git a/aioalice/types/alice_request.py b/aioalice/types/alice_request.py index 7811e67..f8bf337 100644 --- a/aioalice/types/alice_request.py +++ b/aioalice/types/alice_request.py @@ -1,4 +1,5 @@ from attr import attrs, attrib +from aiohttp.web import Request as WebRequest from aioalice.utils import ensure_cls from . import AliceObject, Meta, Session, \ Card, Request, Response, AliceResponse @@ -7,7 +8,7 @@ @attrs class AliceRequest(AliceObject): """AliceRequest is a request from Alice API""" - + original_request = attrib(type=WebRequest) meta = attrib(convert=ensure_cls(Meta)) request = attrib(convert=ensure_cls(Request)) session = attrib(convert=ensure_cls(Session)) diff --git a/tests/test_types.py b/tests/test_types.py index 4e3dc3e..b03f026 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -53,25 +53,28 @@ def test_entity_tokens(self): self._test_entity_tokens(et, ENTITY_TOKEN) def _test_entity_value(self, ev, dct): + if not isinstance(dct, dict): + #self.assertEqual(ev, dct) + return for key in ( - 'first_name', - 'patronymic_name', - 'last_name', - 'country', - 'city', - 'street', - 'house_number', - 'airport', - 'year', - 'year_is_relative', - 'month', - 'month_is_relative', - 'day', - 'day_is_relative', - 'hour', - 'hour_is_relative', - 'minute', - 'minute_is_relative', + 'first_name', + 'patronymic_name', + 'last_name', + 'country', + 'city', + 'street', + 'house_number', + 'airport', + 'year', + 'year_is_relative', + 'month', + 'month_is_relative', + 'day', + 'day_is_relative', + 'hour', + 'hour_is_relative', + 'minute', + 'minute_is_relative', 'value',): if key in dct: print('\nKey occured', key, '\n') @@ -184,7 +187,7 @@ def _test_alice_request(self, arq, dct): self._test_meta(arq.meta, dct['meta']) def _test_alice_request_from_dct(self, dct): - alice_request = types.AliceRequest(**dct) + alice_request = types.AliceRequest(None, **dct) self._test_alice_request(alice_request, dct) def test_alice_request(self): @@ -204,7 +207,7 @@ def test_alice_response(self): self._test_alice_response(alice_response, ALICE_RESPONSE_WITH_BUTTONS) def test_response_from_request(self): - alice_request = types.AliceRequest(**ALICE_REQUEST) + alice_request = types.AliceRequest(None, **ALICE_REQUEST) alice_response = alice_request.response( EXPECTED_RESPONSE['response']['text'] @@ -212,7 +215,7 @@ def test_response_from_request(self): self._assert_payload(alice_response, EXPECTED_RESPONSE) def test_response_from_request2(self): - alice_request = types.AliceRequest(**ALICE_REQUEST) + alice_request = types.AliceRequest(None, **ALICE_REQUEST) alice_response = alice_request.response( RESPONSE_TEXT, tts=TTS, buttons=[types.Button(BUTTON_TEXT, url=URL)] @@ -220,7 +223,7 @@ def test_response_from_request2(self): self._assert_payload(alice_response, EXPECTED_RESPONSE_WITH_BUTTONS) def test_response_big_image_from_request(self): - alice_request = types.AliceRequest(**ALICE_REQUEST) + alice_request = types.AliceRequest(None, **ALICE_REQUEST) alice_response = alice_request.response_big_image( RESPONSE_TEXT, IMAGE_ID, CARD_TITLE, CARD_DESCR, types.MediaButton(BUTTON_TEXT, URL, MB_PAYLOAD), @@ -229,7 +232,7 @@ def test_response_big_image_from_request(self): self._assert_payload(alice_response, EXPECTED_ALICE_RESPONSE_BIG_IMAGE_WITH_BUTTON) def test_response_items_list_from_request(self): - alice_request = types.AliceRequest(**ALICE_REQUEST) + alice_request = types.AliceRequest(None, **ALICE_REQUEST) alice_response = alice_request.response_items_list( RESPONSE_TEXT, CARD_HEADER_TEXT, [types.Image(**IMAGE)], From 6a7d787e71347fc48ab950446074ed3591c89807 Mon Sep 17 00:00:00 2001 From: Suren Khorenyan Date: Thu, 25 Oct 2018 11:18:23 +0300 Subject: [PATCH 3/9] Remove unnecessary check in _test_entity_value --- tests/test_types.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/test_types.py b/tests/test_types.py index 678c8e6..b012881 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -59,9 +59,6 @@ def test_entity_tokens(self): self._test_entity_tokens(et, ENTITY_TOKEN) def _test_entity_value(self, ev, dct): - if not isinstance(dct, dict): - #self.assertEqual(ev, dct) - return for key in ( 'first_name', 'patronymic_name', From 5d77e6805cd9d27fb65899e70cde56582420b544 Mon Sep 17 00:00:00 2001 From: Stanislav Serebrennikov Date: Fri, 26 Oct 2018 15:49:02 +0800 Subject: [PATCH 4/9] Update README.md --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 7fac7c8..f3ed4ac 100644 --- a/README.md +++ b/README.md @@ -65,3 +65,7 @@ async def handle_all_requests(alice_request): ### JSON serializing Если вы хотите использовать более быструю библиотеку для работы с JSON, установите [rapidjson](https://github.com/python-rapidjson/python-rapidjson) или [ujson](https://github.com/esnme/ultrajson). Библиотека определится и будет использована автоматически. + +### Навыки использующие библиотеку + +* [Игра в Ерундопель](https://github.com/Goodsmileduck/erundopel) From 95ce7f27e169845aaf13c8ab54a5b01e7d4b0593 Mon Sep 17 00:00:00 2001 From: Stanislav Serebrennikov Date: Fri, 26 Oct 2018 15:51:34 +0800 Subject: [PATCH 5/9] Update README-en.md --- README-en.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README-en.md b/README-en.md index f4d1d48..8b23063 100644 --- a/README-en.md +++ b/README-en.md @@ -65,3 +65,7 @@ async def handle_all_requests(alice_request): ### JSON serializing If you want to use a faster json library, install [rapidjson](https://github.com/python-rapidjson/python-rapidjson) or [ujson](https://github.com/esnme/ultrajson), it will be detected and used automatically + +### Skills using the library + +* [Erundopel game](https://github.com/Goodsmileduck/erundopel) From 905fb8baef245c0362baaaf7a21e37d2e32dba5e Mon Sep 17 00:00:00 2001 From: Suren Khorenyan Date: Fri, 26 Oct 2018 13:26:39 +0300 Subject: [PATCH 6/9] Update README-en.md --- README-en.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README-en.md b/README-en.md index 8b23063..029bda5 100644 --- a/README-en.md +++ b/README-en.md @@ -66,6 +66,8 @@ async def handle_all_requests(alice_request): If you want to use a faster json library, install [rapidjson](https://github.com/python-rapidjson/python-rapidjson) or [ujson](https://github.com/esnme/ultrajson), it will be detected and used automatically -### Skills using the library +___ -* [Erundopel game](https://github.com/Goodsmileduck/erundopel) +### Skills using aioAlice + +* [The Erundopel game](https://github.com/Goodsmileduck/erundopel) From 732f6fbaf6682b4bf1746b94b0b0ade4b8bbb8b7 Mon Sep 17 00:00:00 2001 From: Suren Khorenyan Date: Fri, 26 Oct 2018 13:27:38 +0300 Subject: [PATCH 7/9] Update README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f3ed4ac..49af14c 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,8 @@ async def handle_all_requests(alice_request): Если вы хотите использовать более быструю библиотеку для работы с JSON, установите [rapidjson](https://github.com/python-rapidjson/python-rapidjson) или [ujson](https://github.com/esnme/ultrajson). Библиотека определится и будет использована автоматически. -### Навыки использующие библиотеку +___ + +### Навыки с использованием aioAlice * [Игра в Ерундопель](https://github.com/Goodsmileduck/erundopel) From e483335c686a420e11bac8bb651a108a21765b5e Mon Sep 17 00:00:00 2001 From: Suren Khorenyan Date: Tue, 27 Nov 2018 12:13:15 +0300 Subject: [PATCH 8/9] Add some hints about testing and deployment into README --- README-en.md | 31 +++++++++++++++++++++++++++++++ README-pypa.md | 37 +++++++++++++++++++++++++++++++++++++ README.md | 31 +++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+) diff --git a/README-en.md b/README-en.md index 029bda5..95d2eb5 100644 --- a/README-en.md +++ b/README-en.md @@ -71,3 +71,34 @@ ___ ### Skills using aioAlice * [The Erundopel game](https://github.com/Goodsmileduck/erundopel) + + +___ + +### Testing and deployment + + +In all examples the next configuration is used: + +```python +WEBHOOK_URL_PATH = '/my-alice-webhook/' # webhook endpoint + +WEBAPP_HOST = 'localhost' # running on local machine +WEBAPP_PORT = 3001 # we can use any port that is not use by other apps +``` + +For testing purposes you can use [ngrok](https://ngrok.com/), so set webhook to `https://1a2b3c4d5e.ngrok.io/my-alice-webhook/` (endpoint has to be `WEBHOOK_URL_PATH`, because WebApp expects to get updates only there), post has to be `WEBAPP_PORT` (in this example it is 3001) + + +For production you can use Nginx, then edit Nginx configuration and add these lines inside the `server` block: + +``` +location /my-alice-webhook/ { # WEBHOOK_URL_PATH + proxy_pass http://127.0.0.1:3001/; # addr to reach WebApp, in this case it is localhost and port is 3001 + proxy_redirect off; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Host $server_name; +} +``` diff --git a/README-pypa.md b/README-pypa.md index 3880a24..d19a987 100644 --- a/README-pypa.md +++ b/README-pypa.md @@ -60,3 +60,40 @@ async def handle_all_requests(alice_request): ### JSON serializing If you want to use a faster json library, install [rapidjson](https://github.com/python-rapidjson/python-rapidjson) or [ujson](https://github.com/esnme/ultrajson), it will be detected and used automatically + +___ + +### Skills using aioAlice + +* [The Erundopel game](https://github.com/Goodsmileduck/erundopel) + + +___ + +### Testing and deployment + + +In all examples the next configuration is used: + +```python +WEBHOOK_URL_PATH = '/my-alice-webhook/' # webhook endpoint + +WEBAPP_HOST = 'localhost' # running on local machine +WEBAPP_PORT = 3001 # we can use any port that is not use by other apps +``` + +For testing purposes you can use [ngrok](https://ngrok.com/), so set webhook to `https://1a2b3c4d5e.ngrok.io/my-alice-webhook/` (endpoint has to be `WEBHOOK_URL_PATH`, because WebApp expects to get updates only there), post has to be `WEBAPP_PORT` (in this example it is 3001) + + +For production you can use Nginx, then edit Nginx configuration and add these lines inside the `server` block: + +``` +location /my-alice-webhook/ { # WEBHOOK_URL_PATH + proxy_pass http://127.0.0.1:3001/; # addr to reach WebApp, in this case it is localhost and port is 3001 + proxy_redirect off; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Host $server_name; +} +``` diff --git a/README.md b/README.md index 49af14c..273b5f7 100644 --- a/README.md +++ b/README.md @@ -71,3 +71,34 @@ ___ ### Навыки с использованием aioAlice * [Игра в Ерундопель](https://github.com/Goodsmileduck/erundopel) + + +___ + +### Тестирование и деплой + + +В примерах используется следующая конфигурация: + +```python +WEBHOOK_URL_PATH = '/my-alice-webhook/' # webhook endpoint + +WEBAPP_HOST = 'localhost' # запускаем на локальной машине +WEBAPP_PORT = 3001 # испльзуем любой не занятый порт +``` + +Для тестирования можно использовать [ngrok](https://ngrok.com/), тогда вебхук нужно будет установить на `https://1a2b3c4d5e.ngrok.io/my-alice-webhook/` (endpoint должен быть `WEBHOOK_URL_PATH`, так как WebApp ожидает получать обновления именно там), порт в настройках нужно указать `WEBAPP_PORT` (в данном случае 3001) + + +Для продакшена можно использовать Nginx, тогда в конфигурации Nginx внутри блока `server` необходимо добавить: + +``` +location /my-alice-webhook/ { # WEBHOOK_URL_PATH + proxy_pass http://127.0.0.1:3001/; # адрес до запущенного WebApp, в нашем случае это localhost и порт 3001 + proxy_redirect off; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Host $server_name; +} +``` From 143607f0978a6d2c84c1ce9c892511e12f3cf049 Mon Sep 17 00:00:00 2001 From: Suren Khorenyan Date: Tue, 27 Nov 2018 12:28:39 +0300 Subject: [PATCH 9/9] Upgrade version to 1.2.2 --- aioalice/__init__.py | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/aioalice/__init__.py b/aioalice/__init__.py index 80ea433..a874bcb 100644 --- a/aioalice/__init__.py +++ b/aioalice/__init__.py @@ -11,4 +11,4 @@ asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) -__version__ = '1.2.1' +__version__ = '1.2.2' diff --git a/setup.py b/setup.py index 89f1d5f..0a8c044 100644 --- a/setup.py +++ b/setup.py @@ -16,7 +16,7 @@ if sys.version_info < MINIMAL_PY_VERSION: raise RuntimeError('aioAlice works only with Python {}+'.format('.'.join(map(str, MINIMAL_PY_VERSION)))) -__version__ = '1.2.1' +__version__ = '1.2.2' def get_description():