From 5b37f5759c9cbda24e532da86215167693a24dee Mon Sep 17 00:00:00 2001 From: Elia El Lazkani Date: Sat, 5 Oct 2019 20:04:19 +0200 Subject: [PATCH] Minor fixes --- requirements/development.txt | 1 + shortenit/web.py | 27 +++++++++++++++++++-------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/requirements/development.txt b/requirements/development.txt index 240e0e2..42e722f 100644 --- a/requirements/development.txt +++ b/requirements/development.txt @@ -1 +1,2 @@ +-r requirements.txt setuptools-git diff --git a/shortenit/web.py b/shortenit/web.py index 637c4da..6b62bee 100644 --- a/shortenit/web.py +++ b/shortenit/web.py @@ -3,7 +3,7 @@ import logging import aiohttp_jinja2 import jinja2 -import trafaret as t +import trafaret from aiohttp import web from pathlib import Path @@ -40,6 +40,8 @@ class Web: name='index') self.router.add_post('/shortenit', self.handler.shortenit, name='shortenit') + self.router.add_put('/pointers/favicon.ico', self.handler.favicon, + name='favicon') self.router.add_get('/{identifier}', self.handler.redirect, name='redirect') @@ -50,9 +52,9 @@ class SiteHandler: self.database = database self.shorten_url = shorten_url self.lenghten_url = lenghten_url - self.shortenit_load_format = t.Dict({ - t.Key('url'): t.URL, - t.Key('timestamp'): t.Int + self.shortenit_load_format = trafaret.Dict({ + trafaret.Key('url'): trafaret.URL, + trafaret.Key('timestamp'): trafaret.Int }) async def shortenit(self, request): @@ -60,9 +62,15 @@ class SiteHandler: self.logger.debug(data) try: data = self.shortenit_load_format(data) - except t.DataError: - raise web.HTTPBadRequest('URL is not valid') - short_url = self.shorten_url(self.database, data['url'], data['timestamp']) + except Exception as e: + self.logger.debug(e) + raise web.HTTPBadRequest() + try: + short_url = self.shorten_url( + self.database, data['url'], data['timestamp']) + except KeyError as e: + self.logger.debug(e) + raise web.HTTPBadRequest() self.logger.debug(short_url) return web.json_response({"url": short_url}) @@ -71,7 +79,10 @@ class SiteHandler: url = self.lenghten_url(self.database, identifier) if not url: raise web.HTTPNotFound() - return web.HTTPFound(location=url) + raise web.HTTPFound(location=url) + + async def favicon(self, request): + raise web.Response(text={}) @aiohttp_jinja2.template('index.html') async def index(self, request):