-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
94 lines (73 loc) · 2.67 KB
/
app.py
File metadata and controls
94 lines (73 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import os
from dotenv import load_dotenv
from flask import Flask, jsonify, request
from fibonacci_calculator import fibonacci
load_dotenv()
app = Flask(__name__)
MAX_N_VALUE = 100000
@app.route('/', methods=['GET'])
def index():
"""
このAPIの使用方法を示す情報を返す。
Returns:
- json: APIの使用方法を示す情報。
- int: HTTPステータスコード
"""
usage = {
"message": "Welcome to the Fibonacci API!",
"usage": {
"endpoint": "/fib",
"method": "GET",
"parameters": {
"n": "An integer value for which you want to get the Fibonacci number."
},
"headers": {
"Content-Type": "application/json"
}
}
}
return jsonify(usage), 200
def create_error_response(status_code, message):
"""
エラーメッセージを含むJSONレスポンスを作成し、返す。
Parameters:
- status_code (int): HTTPステータスコード
- message (str): エラーメッセージ
Returns:
- json: エラーメッセージを含むJSON。
- int: HTTPステータスコード
"""
return jsonify({"status": status_code, "message": message}), status_code
@app.route('/fib', methods=['GET'])
def get_fibonacci():
"""
フィボナッチ数列のn番目の値を計算して返す。
Returns:
- json: フィボナッチ数列のn番目の値。
- int: HTTPステータスコード
Raises:
- ValueError: 不正なパラメータ値の場合。
"""
try:
# リクエストヘッダーがJSON形式かどうか確認する
if request.headers.get("Content-Type") != "application/json":
return create_error_response(400, "Invalid header. Expected Content-Type: application/json.")
n_str = request.args.get('n')
if not n_str:
return create_error_response(400, "Parameter 'n' is missing.")
if not n_str.isdigit():
return create_error_response(400, "Parameter 'n' should be a positive integer.")
n = int(n_str)
if n == 0:
raise ValueError("Parameter 'n' should be a positive integer.")
if n > MAX_N_VALUE:
raise ValueError(f"Value too large. Maximum allowed is {MAX_N_VALUE}.")
result = fibonacci(n)
return jsonify({"result": result}), 200
except ValueError as e:
return create_error_response(400, str(e))
except Exception:
return create_error_response(500, "Internal Server Error.")
if __name__ == "__main__":
DEBUG_MODE = os.environ.get('DEBUG_MODE', 'False').lower() == 'true'
app.run(debug=DEBUG_MODE)