def put(self, id): """Update a specific contact.""" params = request.get_json(force=True) group = params.get('group', None) firstname = params.get('firstname', None) lastname = params.get('lastname', None) perfname = params.get('perfname', None) email = params.get('email', None) phone = params.get('phone', None) date_updated = params.get('date_updated', None) date_created = params.get('date_created', None) author = params.get('author', None) with grpc.insecure_channel(HOST + ':' + PORT) as channel: stub = contacts_pb2_grpc.ContactServiceStub(channel) try: contact = contacts_pb2.Contact( id=str(id), group=group, firstname=firstname, lastname=lastname, perfname=perfname, email=email, phone=phone, date_updated=date_updated, date_created=date_created, author=author, ) contact_result = stub.Update(contact) except Exception as e: print(e) raise return jsonify(json.loads(MessageToJson(contact_result)))
def GetByID(self, request, context): contact = select_contact_by_id(self.dbConnect(), request.id) if contact: return contacts_pb2.Contact(id=str(contact[0]), firstname=str(contact[1]), lastname=str(contact[2]), perfname=str(contact[3]), email=str(contact[4]), phone=str(contact[5]), date_updated=str(contact[6]), date_created=str(contact[7]), author=str(contact[8])) context.set_code(grpc.StatusCode.NOT_FOUND)
def Create(self, request, context): contact_id = create_contact(self.dbConnect(), request.firstname, request.lastname, request.perfname, request.email, request.phone, request.author) print("Contact Created: {}".format(contact_id)) return contacts_pb2.Contact(id=str(contact_id), firstname=str(request.firstname), lastname=str(request.lastname), perfname=str(request.perfname), email=str(request.email), phone=str(request.phone), date_updated="", date_created="", author=str(request.author))
def List(self, request, context): contacts = select_all_contacts(self.dbConnect()) serialized_contacts = [] for contact in contacts: serialized_contacts.append( contacts_pb2.Contact(id=str(contact[0]), firstname=str(contact[1]), lastname=str(contact[2]), perfname=str(contact[3]), email=str(contact[4]), phone=str(contact[5]), date_updated=str(contact[6]), date_created=str(contact[7]), author=str(contact[8]))) print("List Action") return contacts_pb2.ListContactsResponse(contacts=serialized_contacts)
def Update(self, request, context): # Check to see if they already exist: if not select_contact_by_id(self.dbConnect(), request.id): context.set_code(grpc.StatusCode.NOT_FOUND) return update_contact(self.dbConnect(), request.id, request.firstname, request.lastname, request.perfname, request.email, request.phone, request.author) contact = select_contact_by_id(self.dbConnect(), request.id) print("Contact Updated: {}".format(request.id)) return contacts_pb2.Contact(id=str(contact[0]), firstname=str(contact[1]), lastname=str(contact[2]), perfname=str(contact[3]), email=str(contact[4]), phone=str(contact[5]), date_updated=str(contact[6]), date_created=str(contact[7]), author=str(contact[8]))