new: dev: tiny url

This commit is contained in:
rachmadaniHaryono 2019-05-14 18:57:05 +08:00
parent 3c926094b2
commit b2c628a27a
2 changed files with 26 additions and 42 deletions

View File

@ -109,46 +109,16 @@ def refresh_bookmark(rec_id: Union[int, None]):
return res
def get_tiny_url(id):
try:
id = int(id)
except ValueError:
return jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, \
{'ContentType': 'application/json'}
res = None
if request.method == 'GET':
shortened_url = getattr(flask.g, 'bukudb', get_bukudb()).tnyfy_url(id)
if shortened_url is not None:
result = {
'url': shortened_url
}
res = jsonify(result)
else:
res = (jsonify(response.response_template['failure']),
status.HTTP_400_BAD_REQUEST,
{'ContentType': 'application/json'})
return res
def get_long_url(id):
try:
id = int(id)
except ValueError:
return jsonify(response.response_template['failure']), status.HTTP_400_BAD_REQUEST, \
{'ContentType': 'application/json'}
res = None
if request.method == 'GET':
bookmark = getattr(flask.g, 'bukudb', get_bukudb()).get_rec_by_id(id)
if bookmark is not None:
result = {
'url': bookmark[1],
}
res = jsonify(result)
else:
res = (jsonify(response.response_template['failure']),
status.HTTP_400_BAD_REQUEST,
{'ContentType': 'application/json'})
def get_tiny_url(rec_id):
shortened_url = getattr(flask.g, 'bukudb', get_bukudb()).tnyfy_url(rec_id)
if shortened_url is not None:
result = {'url': shortened_url}
res = jsonify(result)
else:
res = (
jsonify(response.response_template['failure']),
status.HTTP_400_BAD_REQUEST,
{'ContentType': 'application/json'})
return res
@ -335,8 +305,7 @@ def create_app(db_file=None):
app.add_url_rule('/api/bookmarks/<int:rec_id>', view_func=bookmark_api_view, methods=['GET', 'PUT', 'DELETE'])
app.add_url_rule('/api/bookmarks/refresh', 'refresh_bookmark', refresh_bookmark, defaults={'rec_id': None}, methods=['POST'])
app.add_url_rule('/api/bookmarks/<int:rec_id>/refresh', 'refresh_bookmark', refresh_bookmark, methods=['POST'])
app.add_url_rule('/api/bookmarks/<id>/tiny', 'get_tiny_url', get_tiny_url, methods=['GET'])
app.add_url_rule('/api/bookmarks/<id>/long', 'get_long_url', get_long_url, methods=['GET'])
app.add_url_rule('/api/bookmarks/<int:rec_id>/tiny', 'get_tiny_url', get_tiny_url, methods=['GET'])
app.add_url_rule(
'/api/network_handle',
'network_handle',

View File

@ -59,6 +59,7 @@ def test_api_empty_db(client, url, exp_res):
['/api/bookmarks/1', response_template['failure'], 400, 'put'],
['/api/bookmarks/1', response_template['failure'], 400, 'delete'],
['/api/bookmarks/1/refresh', response_template['failure'], 400, 'post'],
['/api/bookmarks/1/tiny', response_template['failure'], 400, 'get'],
]
)
def test_invalid_id(client, url, exp_res, status_code, method):
@ -146,3 +147,17 @@ def test_refresh_bookmark(client, api_url):
assert rd.status_code == 200
assert rd.get_json() == {
'description': '', 'tags': [], 'title': 'Google', 'url': 'http://google.com'}
@pytest.mark.parametrize(
'url, exp_res, status_code', [
['http://google.com', {'url': 'http://tny.im/2'}, 200],
['chrome://bookmarks/', response_template['failure'], 400],
])
def test_get_tiny_url(client, url, exp_res, status_code):
rd = client.post('/api/bookmarks', data={'url': url})
assert rd.status_code == 200
assert rd.get_json() == response_template['success']
rd = client.get('/api/bookmarks/1/tiny')
assert rd.status_code == status_code
assert rd.get_json() == exp_res