No issue - change the shape of the API to be driven by date, rather than file name

This commit is contained in:
Mike Taylor 2020-05-28 15:06:49 -05:00
Родитель 6868c48670
Коммит 1105042293
6 изменённых файлов: 12 добавлений и 13 удалений

Просмотреть файл

@ -47,7 +47,7 @@ This app exposes the following HTTP endpoints
* `/lists`: see all lists available for download
* `/lists/trexa-2020-05-21.csv`: download a full, single list (150,000+ sites)
* `/api/lists/trexa-2020-05-21.csv?count=N`: download a single list, trimmed to N sites
* `/api/lists/2020-05-21?count=N`: download a single list, trimmed to N sites
## License

Просмотреть файл

Просмотреть файл

@ -23,11 +23,11 @@ def test_index(client):
def test_api_download(client):
"""Test downloading files from the API endpoint."""
rv = client.get('/api/lists/test_file_with_thirty_lines.csv')
rv = client.get('/api/lists/05-28-2020')
assert rv.status_code == 200
rv = client.get('/api/lists/test_file_with_thirty_lines.csv?count=2')
rv = client.get('/api/lists/05-28-2020?count=2')
assert rv.status_code == 200
rv = client.get('/api/lists/test_file_with_thirty_lines.csv?lol=wat')
rv = client.get('/api/lists/05-28-2020?lol=wat')
assert rv.status_code == 200
rv = client.get('/api/lists/what_in_the_world.py')
assert rv.status_code == 404
@ -35,12 +35,12 @@ def test_api_download(client):
def test_api_download_length(client):
"""Test the count param works as expected."""
rv = client.get('/api/lists/test_file_with_thirty_lines.csv?count=10')
rv = client.get('/api/lists/05-28-2020?count=10')
# pop off the last empty list item (because '\n' comes at the end)
assert len(rv.data.split(b'\n')[:-1]) == 10
assert b'11, login.tmall.com' not in rv.data
rv = client.get('/api/lists/test_file_with_thirty_lines.csv?a=b&count=10')
rv = client.get('/api/lists/05-28-2020?a=b&count=10')
assert len(rv.data.split(b'\n')[:-1]) == 10
assert b'11, login.tmall.com' not in rv.data
rv = client.get('/api/lists/test_file_with_thirty_lines.csv?count=')
rv = client.get('/api/lists/05-28-2020?count=')
assert len(rv.data.split(b'\n')[:-1]) == 30

Просмотреть файл

@ -5,7 +5,7 @@ from trexa.api.endpoints import trim_csv
test_dir = os.path.dirname(os.path.abspath(__file__))
test_csv = os.path.join(test_dir, 'fixtures',
'test_file_with_thirty_lines.csv')
'trexa-05-28-2020.csv')
def get_csv_length(desired_length):

Просмотреть файл

@ -33,8 +33,7 @@ def test_lists_endpoint(client):
rv = client.get('/lists')
assert rv.status_code == 200
assert b'Downloads' in rv.data
assert (b'<a download href="lists/test_file'
b'_with_thirty_lines.csv">') in rv.data
assert b'<a download href="lists/trexa-05-28-2020.csv' in rv.data
if trexa.app.config['ENV'] == 'development':

Просмотреть файл

@ -21,8 +21,8 @@ def index():
return abort(403)
@api_bp.route('/lists/<path:file_name>')
def trim_file(file_name):
@api_bp.route('/lists/<list_date>')
def trim_file(list_date):
"""API route to serve count-limited lists.
If there's no count arg (or its garbage), just serve the whole thing
@ -30,7 +30,7 @@ def trim_file(file_name):
"""
count = request.args.get('count')
csv_path = safe_join(os.path.abspath(
current_app.config['FINAL_LIST_DEST']), file_name)
current_app.config['FINAL_LIST_DEST']), f'trexa-{list_date}.csv')
if not os.path.exists(csv_path):
return abort(404)
return Response(trim_csv(csv_path, count=count), mimetype='text/csv')