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 delete(self, id): with grpc.insecure_channel(HOST + ':' + PORT) as channel: stub = contacts_pb2_grpc.ContactServiceStub(channel) try: deleteContactRequest = contacts_pb2.DeleteContactRequest(id=id) contact = stub.Delete(deleteContactRequest) except Exception as e: print(e) raise return ''
def get(self, id): """Get the details of a single contact.""" with grpc.insecure_channel(HOST + ':' + PORT) as channel: stub = contacts_pb2_grpc.ContactServiceStub(channel) try: contact = stub.GetByID( contacts_pb2.GetByIDContactsRequest(id=id)) except Exception as e: print( "Invalid username or password? Or perphaps we couldn't connect?" ) print(e) return False return jsonify(json.loads(MessageToJson(contact)))
def get(self): """List out all of the contacts.""" with grpc.insecure_channel(HOST + ':' + PORT) as channel: stub = contacts_pb2_grpc.ContactServiceStub(channel) try: contacts = stub.List( contacts_pb2.ListContactsRequest(page_size=1, page_token="1")) except Exception as e: print( "Invalid username or password? Or perphaps we couldn't connect?" ) print(e) return False return jsonify(json.loads(MessageToJson(contacts)))
def run(): # NOTE(gRPC Python Team): .close() is possible on a channel and should be # used in circumstances in which the with statement does not fit the needs # of the code. with grpc.insecure_channel('0.0.0.0:22222') as channel: stub = contacts_pb2_grpc.ContactServiceStub(channel) print('-' * 20) print("LIST:") # Test the list endpoint. response = stub.List( contacts_pb2.ListContactsRequest(page_size=1, page_token="1")) for contact in response.contacts: print(contact) print('-' * 20)