Skip to content

Commit d2c1e8c

Browse files
committed
sdsd
1 parent 3dd7a73 commit d2c1e8c

File tree

2 files changed

+57
-46
lines changed

2 files changed

+57
-46
lines changed

api/binaryConverter.py

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,64 @@
1-
from flask import Flask, jsonify, Blueprint, request
2-
from flask_cors import CORS
3-
from flask_restful import Api, Resource
4-
from __init__ import app, db
5-
from sqlalchemy.exc import SQLAlchemyError
6-
from api.jwt_authorize import token_required
1+
from flask import Blueprint, request, jsonify, current_app, Response, g
2+
from flask_restful import Api, Resource # Used for REST API building
3+
from __init__ import app # Ensure __init__.py initializes your Flask app
74
from model.binaryConverter import BinaryConverter
8-
# Initialize Flask app
9-
app = Flask(__name__)
105

11-
# Enable CORS for cross-origin access
12-
CORS(app)
6+
# Blueprint for the API
7+
binary_converter_api = Blueprint('binary_converter_api', __name__, url_prefix='/api')
138

14-
# Create the blueprint
15-
binaryConverter_api = Blueprint('BinaryConverter_api', __name__, url_prefix='/api')
9+
api = Api(binary_converter_api) # Attach Flask-RESTful API to the Blueprint
1610

17-
# Sample binary data
18-
BINARY_CONVERTER = [
19-
{"binary": "1111001100001", "decimal": 7777},
20-
{"binary": "100100010011", "decimal": 2323},
21-
{"binary": "11100011010001001101", "decimal": 932237},
22-
]
11+
class BinaryConverterAPI:
12+
"""
13+
Define the API CRUD endpoints for the Post model.
14+
There are four operations that correspond to common HTTP methods:
15+
- post: create a new post
16+
- get: read posts
17+
- put: update a post
18+
- delete: delete a post
19+
"""
20+
class _CRUD(Resource):
21+
def post(self):
22+
# Obtain the request data sent by the RESTful client API
23+
data = request.get_json()
24+
# Create a new post object using the data from the request
25+
post = BinaryConverter(data['binary'], data['decimal'])
26+
# Save the post object using the Object Relational Mapper (ORM) method defined in the model
27+
post.create()
28+
# Return response to the client in JSON format, converting Python dictionaries to JSON format
29+
return jsonify(post.read())
2330

24-
# Move the route to the blueprint
25-
@binaryConverter_api.route('/binaryConverter', methods=['GET', 'POST'])
26-
def binary_converter():
27-
if request.method == 'GET':
28-
"""
29-
Endpoint to retrieve all binary history events (from static data).
30-
"""
31-
return jsonify(BINARY_CONVERTER), 200
31+
32+
def put(self):
33+
# Obtain the request data
34+
data = request.get_json()
35+
# Find the current post from the database table(s)
36+
post = BinaryConverter.query.get(data['id'])
37+
# Update the post
38+
post._decimal = data['decimal']
39+
post._binary = data['binary']
40+
# Save the post
41+
post.update()
42+
# Return response
43+
return jsonify(post.read())
3244

33-
if request.method == 'POST':
34-
"""
35-
Endpoint to add a new binary conversion to the history.
36-
"""
37-
data = request.json # Parse incoming JSON
38-
binary_input = data.get('binary')
45+
46+
def delete(self):
47+
# Obtain the request data
48+
data = request.get_json()
49+
# Find the current post from the database table(s)
50+
post = BinaryConverter.query.get(data['id'])
51+
# Delete the post using the ORM method defined in the model
52+
post.delete()
53+
# Return response
54+
return jsonify({"message": "Post deleted"})
3955

40-
# Validate binary input
41-
if binary_input and all(char in '01' for char in binary_input):
42-
decimal_value = int(binary_input, 2) # Convert binary to decimal
43-
new_entry = {"binary": binary_input, "decimal": decimal_value}
44-
BINARY_CONVERTER.append(new_entry) # Add to history
45-
return jsonify(new_entry), 201 # Return the newly added entry
46-
else:
47-
return {"error": "Invalid binary input. Only 0s and 1s are allowed."}, 400
48-
49-
# Register the blueprint with the app
50-
app.register_blueprint(binaryConverter_api)
51-
52-
# Start the app on the desired host and port
56+
"""
57+
Map the _CRUD class to the API endpoints for /post.
58+
- The API resource class inherits from flask_restful.Resource.
59+
- The _CRUD class defines the HTTP methods for the API.
60+
"""
61+
api.add_resource(_CRUD, '/binary-converter')
62+
5363
if __name__ == '__main__':
54-
app.run(debug=True, host="0.0.0.0", port=8887)
64+
app.run(debug=True)

model/binaryConverter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ def initBinaryConverter():
8686
quizzes = [
8787
BinaryConverter(decimal="7777", binary="1111001100001"),
8888
BinaryConverter(decimal="2323", binary="100100010011"),
89+
8990
]
9091

9192
for quiz in quizzes:

0 commit comments

Comments
 (0)