def shortener(request): url = check_url( request.params.get('url'), request.registry.settings ) if len(url) >= 2046: # we only accept URL shorter or equal to 2046 characters # Index restriction in DynamoDB url_short = 'toolong' else: # DynamoDB v2 high-level abstraction try: table = get_dynamodb_table(table_name='shorturl') except Exception as e: raise exc.HTTPBadRequest('Error during connection %s' % e) url_short = _add_item(table, url) # Use env specific URLs if request.host not in ('api.geo.admin.ch', 'api3.geo.admin.ch'): host_url = make_api_url(request) + '/shorten/' else: host_url = ''.join((request.scheme, '://s.geo.admin.ch/')) return { 'shorturl': host_url + url_short }
def qrcode(request): url = quoting( check_url(request.params.get('url'), request.registry.settings)) url = _shorten_url(request, url) img = _make_qrcode_img(url) response = Response(img, content_type='image/png') return response
def qrcode(request): url = quoting(check_url( request.params.get('url') )) url = _shorten_url(request, url) img = _make_qrcode_img(url) response = Response(img, content_type='image/png') return response
def test_check_url(self): from pyramid.httpexceptions import HTTPBadRequest url = None config = {'shortener.allowed_hosts': 'admin.ch,swisstopo.ch,bgdi.ch'} with self.assertRaises(HTTPBadRequest): check_url(url, config) url = 'dummy' with self.assertRaises(HTTPBadRequest): check_url(url, config) url = 'http://dummy.ch' with self.assertRaises(HTTPBadRequest): check_url(url, config) url = 'http://admin.ch' self.assertEqual(url, check_url(url, config))
def shortener(request): url = request.params.get('url') if len(url) >= 2046: # we only accept URL shorter or equal to 2046 characters # Index restriction in DynamoDB url_short = 'toolong' else: # pragma: no cover url_short = check_url(url, request.registry.settings) # DynamoDB v2 high-level abstraction try: table = get_dynamodb_table(table_name='shorturl') except Exception as e: raise exc.HTTPInternalServerError('Error during connection %s' % e) url_short = _add_item(table, url) # Use env specific URLs if request.host not in ('api.geo.admin.ch', 'api3.geo.admin.ch'): host_url = make_api_url(request) + '/shorten/' else: host_url = ''.join((request.scheme, '://s.geo.admin.ch/')) return {'shorturl': host_url + url_short}
def shortener(request): url = check_url(request.params.get('url')) if len(url) >= 2046: # we only accept URL shorter or equal to 2046 characters # Index restriction in DynamoDB url_short = 'toolong' else: # DynamoDB v2 high-level abstraction try: table = Table('shorturl', connection=connect_to_region('eu-west-1')) except Exception as e: raise exc.HTTPBadRequest('Error during connection %s' % e) url_short = _add_item(table, url) # Use env specific URLs if request.host not in ('api.geo.admin.ch', 'api3.geo.admin.ch'): host_url = make_api_url(request) + '/shorten/' else: host_url = ''.join((request.scheme, '://s.geo.admin.ch/')) return {'shorturl': host_url + url_short}
def test_check_url(self): from pyramid.httpexceptions import HTTPBadRequest url = None config = {'shortener.allowed_hosts': 'admin.ch,swisstopo.ch,bgdi.ch'} try: check_url(url, config) except Exception as e: self.assertTrue(isinstance(e, HTTPBadRequest)) url = 'dummy' try: check_url(url, config) except Exception as e: self.assertTrue(isinstance(e, HTTPBadRequest)) url = 'http://dummy.com' try: check_url(url, config) except Exception as e: self.assertTrue(isinstance(e, HTTPBadRequest)) url = 'http://admin.ch' self.assertEqual(url, check_url(url, config))