How to get employee detail by userId in Python/Falcon

1.1k views Asked by At

I am new to Python/Falcon. How to get employee details by userId in Python?

falcontry.py

Falcon example:

import falcon
import json 

class ThingsResource: 

    def on_get(self, req, resp): 
        resp.status = falcon.HTTP_200 

        fp = open("jsonparser.json","r") 
        json_str = fp.read() 

        print json_str json_content = json.loads(json_str)
        json_content = json.dumps(json_content) 

        print json_content resp.body = (json_content)

api = falcon.API() 
things = ThingsResource() 
api.add_route('/Employees/userId/{id}', things)

what i am doing wrong here?

jsonparser.json Employees:

{ "Employees" : [{
        "userId":"RUPAK",
        "jobTitleName":"Developer",
        "firstName":"ABC",
        "lastName":"Irani",
        "preferredFullName":"Romin Irani",
        "employeeCode":"E1",
        "region":"CA",
        "phoneNumber":"408-1234567",
        "emailAddress":"[email protected]"
    }]

}
2

There are 2 answers

2
iamdanchiv On

Your JSON/Dict:

t =  { "Employees" : [ {
            "userId":"RUPAK",
            "jobTitleName":"Developer",
            "firstName":"ABC",
            "lastName":"Irani",
            "preferredFullName":"Romin Irani",
            "employeeCode":"E1",
            "region":"CA",
            "phoneNumber":"408-1234567",
            "emailAddress":"[email protected]" }
            ]
    }

Employees Class:

class Employees():

    def on_get(self, json, userID):
        for x in json.get('Employees'):
            if x.get('userId') == userID: 
                print x


empl = Employees()
empl.on_get(t, 'RUPAK')

Output:

$ python pytest.py {'emailAddress': '[email protected]', 'phoneNumber': '408-1234567', 'jobTitleName': 'Developer', 'firstName': 'ABC', 'lastName': 'Irani', 'preferredFullName': 'Romin Irani', 'employeeCode': 'E1', 'userId': 'RUPAK', 'region': 'CA'}

Let me know if you need more details on the approach. As it stands, it is pretty hard to understand what you are looking for, because of the lack of information/formatting in your question.

Hope it helps you!

0
Muhammad Abdullah On

As per your most recent comment, I'm assuming that you have a database of Employee with a table named Detail and you want the user to pass the userId. That will be achieved by requesting the URL http://www.yourdomain.com/Employees?userId=RUPAK

If that is the case, you can read userId from req object by using req.get_params() and query your database using a SELECT statement with WHERE clause. Your modified code is as followed:

import falcon
import json 
import MySQLdb

class ThingsResource: 
    def on_get(self, req, resp): 
        resp.status = falcon.HTTP_200 
        db = MySQLdb.connect(host = 'localhost', user = 'root', passwd = '***', db = 'Employee' ) 
        cursor = db.cursor() 

        userId=req.get_params('userId')
        cursor.execute('SELECT * FROM Detail WHERE userId='+userId) 
        result = cursor.fetchone() 
        print result
        resp.body = json.dumps(result) 

api = falcon.API() 
things = ThingsResource() 
api.add_route('/Employees', things)

If this resolves your problem then please marks "Answered" otherwise let me know if you need further assistance.