Home > OS >  How to combine these 2 simple python files without using class?
How to combine these 2 simple python files without using class?

Time:06-20

So I have 2 python files. I wanted to know if I can do it without the use of class.

from bson import json_util
from pymongo import MongoClient
import json

class DBClient:
    def __init__(self):
        self.client = MongoClient('localhost', 27017)
        self.db = self.client.requests.requests_data

    def get_data(self, request_id, email):
        request = self.db.find_one({"requestId": request_id, "email": email})
        if request:
            request = json.loads(json_util.dumps(request))
            for k, v in list(request.items()):
                if v == 'Failed':
                    del request[k]
        return request

    def update_status(self, request_id, tc, old_status, new_status):
        self.db.update_one({"requestId": request_id, tc: old_status}, {"$set": {tc:new_status}})

from flask import Flask, request, jsonify
from modules import db

app = Flask(__name__)

db = db.DBClient()

@app.route('/fetch_data', methods=['GET'])
def fetch_data():
    request_id = request.args.get('requestId')
    email = request.args.get('email')
    if request_id is None or email is None:
        return jsonify({"status": 400, "data": [], "info": "Invalid email or requestId"})
    request_data = db.get_data(request_id, email)
    return jsonify({"status": 200, "data": request_data, "info": "OK"})

if __name__ == '__main__':
    app.run()

Also I wanted to know if it is possible to make this into one single python file instead of 2 separate ones.

CodePudding user response:

While having your modules separate improves readability and is more in line with software engineering practices. You can combine the two files into a single one without issue.

from bson import json_util
from pymongo import MongoClient
from flask import Flask, request, jsonify
import json
app = Flask(__name__)
class DBClient:
    def __init__(self):
        self.client = MongoClient('localhost', 27017)
        self.db = self.client.requests.requests_data

    def get_data(self, request_id, email):
        request = self.db.find_one({"requestId": request_id, "email": email})
        if request:
            request = json.loads(json_util.dumps(request))
            for k, v in list(request.items()):
                if v == 'Failed':
                    del request[k]
        return request

    def update_status(self, request_id, tc, old_status, new_status):
        self.db.update_one({"requestId": request_id, tc: old_status}, {"$set": {tc:new_status}})

db = DBClient()

@app.route('/fetch_data', methods=['GET'])
def fetch_data():
    request_id = request.args.get('requestId')
    email = request.args.get('email')
    if request_id is None or email is None:
        return jsonify({"status": 400, "data": [], "info": "Invalid email or requestId"})
    request_data = db.get_data(request_id, email)
    return jsonify({"status": 200, "data": request_data, "info": "OK"})

if __name__ == '__main__':
    app.run()

Non Class Approach

from pymongo import MongoClient
from flask import Flask, request, jsonify
import json

app = Flask(__name__)

client = MongoClient("localhost", 27017)
db = client.requests.requests_data


def get_data(db, request_id, email):
    request = db.find_one({"requestId": request_id, "email": email})
    if request:
        request = json.loads(json_util.dumps(request))
        for k, v in list(request.items()):
            if v == "Failed":
                del request[k]
    return request


def update_status(db, request_id, tc, old_status, new_status):
    db.update_one(
        {"requestId": request_id, tc: old_status}, {"$set": {tc: new_status}}
    )


@app.route("/fetch_data", methods=["GET"])
def fetch_data():
    request_id = request.args.get("requestId")
    email = request.args.get("email")
    if request_id is None or email is None:
        return jsonify(
            {"status": 400, "data": [], "info": "Invalid email or requestId"}
        )
    request_data = get_data(db, request_id, email)
    return jsonify({"status": 200, "data": request_data, "info": "OK"})


if __name__ == "__main__":
    app.run()


  • Related