Exemplo n.º 1
0
def customers_with_id(customer_id):
    if request.method == 'GET':
        data={"error":"Something went wrong. Please try again later."}
        app.logger.error('GET /customers with id method called')

        responseStatusCode = 500
        try:
            customer = collection_customers.find_one({"guid":customer_id})
            app.logger.info('customer object: ' + str(customer))
            customer_obj = Customer.fromDict(customer)
            data = customer_obj
            responseStatusCode = 200
        except KeyError as e:
            data = {"error":"Invalid id for the recording."}
            responseStatusCode = 400
        except Exception as e:
            data = {"error":"Something went wrong. Please try again later. "+str(e)}
            responseStatusCode = 500    
        (data,mime) = (jsonParser.DateTimeJSONEncoder().encode(data),'application/json')
        return Response(data, status=responseStatusCode, mimetype=mime)
    
    if request.method == 'PUT':
        data={"error":"Something went wrong. Please try again later."}
        app.logger.error('PUT /customers with id method called')
        responseStatusCode = 500
        try:
            customer = collection_customers.find_one({"guid":customer_id})
            customer_obj = Customer.fromJson(request.data, request.method, customer_id)
            app.logger.info('customer object 2: ' + str(customer_obj))
            #store the recording in the recording collection
            collection_customers.update({'guid':customer_id},{"$set": customer_obj.toDict()},upsert=True) #upsert creates the object if not existing
            data = customer_obj
            app.logger.info('customer object 3: ' + str(customer_obj))
            responseStatusCode = 200
        except TypeError as e:
            data = {"error":"Customer could not be created because the data in the body of the request was bad." + str(e)}
            responseStatusCode = 400
        except Exception as e:
            data = {"error":"Something went wrong. Please try again later. "+ str(e)}
            responseStatusCode = 500
        (data,mime) = (jsonParser.DateTimeJSONEncoder().encode(data),'application/json')
        return Response(data, status=responseStatusCode, mimetype=mime) 

    if request.method == 'DELETE':
        data={"error":"Something went wrong. Please try again later."}
        responseStatusCode = 500
        try:
            collection_customers.update({'guid':customer_id},{"$set": {"status": "deleted"}})
            data = {"success":"Customer with guid: "+customer_id+" deleted successfully."}
            responseStatusCode = 200
        except KeyError as e:
            data = {"error":"Invalid id for the customer. "+str(e)}
            responseStatusCode = 400
        except Exception as e:
            data = data={"error":"Something went wrong. Please try again later. "+str(e)}
            responseStatusCode = 401
        (data,mime) = (jsonParser.DateTimeJSONEncoder().encode(data),'application/json')
        return Response(data, status=responseStatusCode, mimetype=mime)
Exemplo n.º 2
0
def customers():
    if request.method == 'GET':
        app.logger.error('GET /customers method called')
        data={"error":"Something went wrong. Please try again later."}
        responseStatusCode = 500
        try:
            customers = collection_customers.find()
            response = []
            for customer_dict in customers:
                app.logger.error("sander" + str(customer_dict))
                customer_obj = Customer.fromDict(customer_dict)
                response.append(customer_obj)
            data = response
            app.logger.error('Data sent as part of the GET /customers: ' + str(data))
            responseStatusCode = 200
        except Exception as e:
            data={"error":"Something went wrong. Please try again later. "+str(e)}  
            app.logger.error('Error in GET /customers: '+repr(e)) 
            responseStatusCode = 500
        (data,mime) = (jsonParser.DateTimeJSONEncoder().encode(data),'application/json')
        return Response(data, status=responseStatusCode, mimetype=mime)
    
    if request.method == 'POST':
        app.logger.error('POST /customers method called')
        data={"error":"Something went wrong. Please try again later."}
        responseStatusCode = 500
        try:     
            customer_obj = Customer.fromJson(request.data)
            collection_customers.insert(customer_obj.toDict())
            data = customer_obj
            #app.logger.error('Data sent as part of the POST /customers: ' + str(data))
            (data,mime) = (jsonParser.DateTimeJSONEncoder().encode(data),'application/json')
            responseStatusCode = 200

        except TypeError as e:
            data = {"error":"Customer could not be created because the data in the body of the request was bad." + str(e)}
            responseStatusCode = 400
        except Exception as e:
            data = {"error":"Something went wrong. Please try again later."+ str(e)}
            app.logger.error('Error in POST /customers: '+str(e)) 
            responseStatusCode = 500
        (data,mime) = (jsonParser.DateTimeJSONEncoder().encode(data),'application/json')
        return Response(data, status=responseStatusCode, mimetype=mime)