Coverage for index.py: 94%
42 statements
« prev ^ index » next coverage.py v7.10.7, created at 2026-10-07 00:02 +0000
« prev ^ index » next coverage.py v7.10.7, created at 2026-10-07 00:02 +0000
1import json
2from flask import abort, Flask, Blueprint, jsonify, request
3from flask_cors import cross_origin
4from src.data_manager import DataManager
5from src.const import PREFECTURES
6from src.swagger_settings import set_config
9data_manager = DataManager()
11app = Flask(__name__)
13apiv1 = Blueprint('apiv1', __name__, url_prefix='/api/v1')
16@app.route('/')
17def index():
18 return jsonify({'message': 'Web API to get COVID-19(coronavirus) information of each prefecture in Japan.'})
21@apiv1.route('/total')
22@cross_origin(apiv1)
23def total():
24 """ Get the total number of infected people and deaths in Japan
25 ---
26 parameters:
27 - in: query
28 name: history
29 type: boolean
30 - in: query
31 name: predict
32 type: boolean
33 responses:
34 200:
35 description: Success to get total
36 """
37 if 'history' in request.args and request.args['history'] == 'true':
38 return data_manager.history_total_json, 200, {'Content-Type': 'application/json; charset=utf-8'}
39 elif 'predict' in request.args and request.args['predict'] == 'true':
40 return data_manager.prediction_total_json, 200, {'Content-Type': 'application/json; charset=utf-8'}
41 return data_manager.today_total_json, 200, {'Content-Type': 'application/json; charset=utf-8'}
44@apiv1.route('/prefectures')
45@cross_origin(apiv1)
46def prefectures():
47 """Get the COVID-19(coronavirus) information of each prefecture in Japan
48 ---
49 responses:
50 200:
51 description: Success to get the information of each prefecture
52 """
53 return data_manager.prefectures_json, 200, {'Content-Type': 'application/json; charset=utf-8'}
56@apiv1.route('/positives')
57@cross_origin(apiv1)
58def positives():
59 """ Get the total number of infected people and deaths in Japan
60 ---
61 parameters:
62 - in: query
63 name: prefecture
64 description: Specify the Japanese name. example - 千葉県
65 type: string
66 required: true
67 responses:
68 200:
69 description: Success to get positives
70 400:
71 description: The prefecture parameter is required
72 404:
73 description: Does not find the prefecture
74 """
75 if 'prefecture' in request.args:
76 prefecture = request.args['prefecture']
77 if prefecture in PREFECTURES: 77 ↛ 78line 77 didn't jump to line 78 because the condition on line 77 was never true
78 response_json = data_manager.get_positive_detail_json(prefecture)
79 return response_json, 200, {'Content-Type': 'application/json; charset=utf-8'}
80 else:
81 return abort(404, {'prefecture': f'Does not find {prefecture}'})
82 message = 'The prefecture parameter is required.\
83 ex: https://covid19-japan-web-api.now.sh/api/v1/positives?prefecture=東京都'
84 response_json = json.dumps({'message': message}, ensure_ascii=False)
85 return response_json, 400, {'Content-Type': 'application/json; charset=utf-8'}
88@apiv1.route('/statistics')
89@cross_origin(apiv1)
90def statistics():
91 """Get the COVID-19(coronavirus) statistics of each prefecture in Japan
92 ---
93 responses:
94 200:
95 description: Success to get the statistics of each prefecture
96 """
97 return data_manager.statistics_json, 200, {'Content-Type': 'application/json; charset=utf-8'}
100app.register_blueprint(apiv1)
101set_config(app)