def create(): """ Register the url """ origin_url = request.values.get('origin_url', None) if not origin_url: raise ApiException('"origin_url" is required') MAX_TRY = 5 for cnt in range(MAX_TRY): try: u = Url(short_url=Url.gen_short_url(), origin_url=origin_url) except ValidationError as e: raise ApiException(str(e)) if redis_cli.set(u.redis_key, u.json(), nx=True, ex=u.SHORT_URL_EXPIRE_SECONDS): break if cnt + 1 == MAX_TRY: raise ApiException('Collision happened. Please try again.') return jsonify({'short_url': u.short_url})
def test_generate_short_url(): short_url = Url.gen_short_url() assert len(short_url) == Url.SHORT_URL_LENGTH