-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflask-api.py
More file actions
176 lines (153 loc) · 4.66 KB
/
flask-api.py
File metadata and controls
176 lines (153 loc) · 4.66 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from flask import Flask, jsonify, redirect, request
from pymongo import MongoClient
import config
import certifi
from flasgger import Swagger
app = Flask(__name__)
Swagger(app)
client = MongoClient(config.MONGO_URI,tlsCAFile=certifi.where())
db = client["sample_guides"]
col = db["planets"]
@app.route('/')
def home():
return redirect("/apidocs")
@app.route('/planets_list',methods=['GET'])
def get_planets():
"""
Get a list of all planets.
---
responses:
200:
description: A list of planet names.
"""
planets = [i['name'] for i in col.find({},{'_id':0})]
return jsonify(planets)
@app.route('/planet_details', methods=['GET'])
def get_planet_by_name():
"""
Get a planet based on its name.
---
parameters:
- name: name
in: query
type: string
required: true
description: The name of the planet to retrieve.
responses:
200:
description: Planet retrieved successfully.
400:
description: Planet name is required.
404:
description: Planet not found.
"""
planet_name = request.args.get('name')
if not planet_name:
return jsonify({"error": "Planet name is required"}), 400
planet = col.find_one({"name": planet_name}, {'_id': False})
if not planet:
return jsonify({"error": "Planet not found"}), 404
return jsonify(planet), 200
@app.route('/create_planet', methods=['POST'])
def create_planet():
"""
Create a new planet.
---
parameters:
- name: name
in: formData
type: string
required: true
description: The name of the planet to create.
responses:
201:
description: Planet created successfully.
400:
description: Bad request. Planet name is required.
"""
planet_name = request.form.get('name')
if not planet_name:
return jsonify({"error": "Planet name is required"}), 400
new_planet = {"name": planet_name}
col.insert_one(new_planet)
return jsonify({"message": "Planet created successfully"}), 201
@app.route('/update_planet', methods=['PUT'])
def update_planet():
"""
Update a planet.
---
parameters:
- name: name
in: formData
type: string
required: true
description: The name of the planet to update.
responses:
200:
description: Planet updated successfully.
400:
description: Bad request. Planet name is required.
404:
description: Planet not found.
"""
planet_name = request.form.get('name')
if not planet_name:
return jsonify({"error": "Planet name is required"}), 400
# Replace the following placeholder with your actual update logic
# Example:
# col.update_one({"name": planet_name}, {"$set": {"population": 1000000}})
# Check if the planet was updated successfully and return the appropriate response
@app.route('/partially_update_planet', methods=['PATCH'])
def partially_update_planet():
"""
Partially update a planet.
---
parameters:
- name: name
in: formData
type: string
required: true
description: The name of the planet to partially update.
responses:
200:
description: Planet partially updated successfully.
400:
description: Bad request. Planet name is required.
404:
description: Planet not found.
"""
planet_name = request.form.get('name')
if not planet_name:
return jsonify({"error": "Planet name is required"}), 400
# Replace the following placeholder with your actual partial update logic
# Example:
# col.update_one({"name": planet_name}, {"$set": {"population": 1000000}})
# Check if the planet was partially updated successfully and return the appropriate response
@app.route('/delete_planet', methods=['DELETE'])
def delete_planet():
"""
Delete a planet.
---
parameters:
- name: name
in: query
type: string
required: true
description: The name of the planet to delete.
responses:
200:
description: Planet deleted successfully.
400:
description: Bad request. Planet name is required.
404:
description: Planet not found.
"""
planet_name = request.args.get('name')
if not planet_name:
return jsonify({"error": "Planet name is required"}), 400
# Replace the following placeholder with your actual delete logic
# Example:
# col.delete_one({"name": planet_name})
# Check if the planet was deleted successfully and return the appropriate response
if __name__ == '__main__':
app.run(debug=True)