class MessagesApi(remote.Service): @SimpleRequest.method(name='inbox', path='inbox', http_method='GET', request_fields=(), response_message=ThreadApiModel.ProtoCollection()) @user_required def inbox(self, req): """Retrieve the account's inbox.""" try: account = get_current_user() thread_dtos = services.messages.threads(account.id) api_models = [ ThreadApiModel.from_thread_dto(thd) for thd in thread_dtos ] return ThreadApiModel.ProtoCollection()(items=api_models) except exp.ServiceExp as e: handle_exception(e) @SimpleRequest.method(name='thread', path='{thread_id}', http_method='GET', request_fields=('thread_id', ), response_message=MessageApiModel.ProtoCollection()) @user_required def thread(self, req): """Retrieve messages in a thread. :param req.thread_id: (int) ID of the thread. """ thread_id = req.thread_id try: account = get_current_user() msg_dtos = services.messages.messages(account.id, thread_id) api_models = [ MessageApiModel.from_message_dto(msg) for msg in msg_dtos ] return MessageApiModel.ProtoCollection()(items=api_models) except exp.ServiceExp as e: handle_exception(e) @NewThreadRequest.method(name='new', path='new', http_method='POST', response_message=ThreadApiModel.ProtoModel()) @user_required def new_thread(self, req): """Create a new thread.""" recipients, text = req.recipients, req.text try: account = get_current_user() thread_dto = services.messages.create_thread( account.id, recipients, text) api_model = ThreadApiModel.from_thread_dto(thread_dto) return api_model except exp.ServiceExp as e: handle_exception(e) @MessageApiModel.method(name='send', path='send', http_method='POST', request_fields=('thread_id', 'text'), response_message=MessageApiModel.ProtoModel()) @user_required def send(self, req): """Send a reply in a thread. :param req.thread_id: (int) ID of the thread. :param req.text: (str) message to send. """ thread_id, text = req.thread_id, req.text try: account = get_current_user() msg_dto = services.messages.send(account.id, thread_id, text) api_model = MessageApiModel.from_message_dto(msg_dto) return api_model except exp.ServiceExp as e: handle_exception(e) @SimpleRequest.method(name='leave', path='leave', http_method='POST', request_fields=('thread_id', ), response_message=SimpleResponse.ProtoModel()) @user_required def leave(self, req): """Leave a thread. :param req.thread_id: (int) ID of the thread. """ resp = SimpleResponse() thread_id = req.thread_id try: account = get_current_user() services.messages.leave_thread(account.id, thread_id) return resp.ToMessage() except exp.ServiceExp as e: handle_exception(e) @SimpleRequest.method(name='hide', path='hide', http_method='POST', request_fields=('message_id', ), response_message=SimpleResponse.ProtoModel()) @user_required def hide(self, req): """Hide a message in a thread. :param req.message_id: (int) ID of the message to hide. """ resp = SimpleResponse() message_id = req.message_id try: account = get_current_user() services.messages.hide_message(account.id, message_id) return resp.ToMessage() except exp.ServiceExp as e: handle_exception(e)
class AccountsApi(remote.Service): @SimpleRequest.method(name='get', path='{account_id}', http_method='GET', request_fields=('account_id', ), response_message=AccountApiModel.ProtoModel()) @user_required def account_get(self, req): """Retrieve the account's profile information. :param req.account_id: (int) ID of the account to retrieve. """ account_id = req.account_id try: account_dto = services.accounts.account_by_id(account_id) api_model = AccountApiModel.from_account_dto(account_dto) return api_model except exp.ServiceExp as e: handle_exception(e) @AccountApiModel.method(name='update', path='update', http_method='POST', response_message=AccountApiModel.ProtoModel()) @user_required def account_update(self, req): """Update account information.""" try: actor = get_current_user() account_dto = AccountApiModel.to_account_dto(req) account_dto = services.accounts.account_update( actor.id, account_dto) api_model = AccountApiModel.from_account_dto(account_dto) refresh_userdata() return api_model except exp.ServiceExp as e: handle_exception(e) @SimpleRequest.method(name='caregiver', path='caregiver', http_method='GET', request_fields=('account_id', ), response_message=CaregiverApiModel.ProtoModel()) @user_required def caregiver(self, req): """Retrieve the account's caregiver details.""" try: account = get_current_user() caregiver_dto = services.accounts.caregiver_by_account(account.id) api_model = CaregiverApiModel.from_caregiver_dto(caregiver_dto) return api_model except exp.ServiceExp as e: handle_exception(e) @CaregiverApiModel.method(name='caregiver.update', path='caregiver/update', http_method='POST', response_message=CaregiverApiModel.ProtoModel()) @user_required def caregiver_update(self, req): """Update the account's caregiver details.""" try: account = get_current_user() caregiver_dto = CaregiverApiModel.to_caregiver_dto(req) caregiver_dto = services.accounts.caregiver_update( account.id, caregiver_dto) api_model = CaregiverApiModel.from_caregiver_dto(caregiver_dto) return api_model except exp.ServiceExp as e: handle_exception(e) @SimpleRequest.method(name='patients.list', path='patients/list', http_method='GET', request_fields=(), response_message=PatientApiModel.ProtoCollection()) @user_required def patients_list(self, req): """Retrieve the account's patients.""" try: account = get_current_user() patient_dtos = services.accounts.patients_by_account(account.id) api_models = [ PatientApiModel.from_patient_dto(p) for p in patient_dtos ] return PatientApiModel.ProtoCollection()(items=api_models) except exp.ServiceExp as e: handle_exception(e) @PatientApiModel.method(name='patients.update', path='patients/update', http_method='POST', response_message=PatientApiModel.ProtoModel()) @user_required def patients_update(self, req): """Create or update a single patient.""" try: account = get_current_user() patient_dto = PatientApiModel.to_patient_dto(req) patient_dto = services.accounts.patient_update( account.id, patient_dto) api_model = PatientApiModel.from_patient_dto(patient_dto) return api_model except exp.ServiceExp as e: handle_exception(e) @SimpleRequest.method(name='patients.remove', path='patients/remove', http_method='POST', request_fields=('patient_id', ), response_message=SimpleResponse.ProtoModel()) @user_required def patients_remove(self, req): """Remove a single patient. :param req.patient_id: (int) ID of the patient to remove. """ patient_id = req.patient_id resp = SimpleResponse() try: account = get_current_user() services.accounts.patient_remove(account.id, patient_id) return resp.ToMessage() except exp.ServiceExp as e: handle_exception(e)
class ConnectionsApi(remote.Service): @SimpleRequest.method(name='request', path='request', http_method='POST', request_fields=('account_id', ), response_message=SimpleResponse.ProtoModel()) @user_required def request(self, req): """Send a connection request to a user. :param req.account_id: (int) ID of the account receiving the request. """ resp = SimpleResponse() to_id = req.account_id try: account = get_current_user() services.connections.send_request(account.id, to_id) return resp.ToMessage() except exp.ServiceExp as e: handle_exception(e) @SimpleRequest.method(name='accept', path='accept', http_method='POST', request_fields=('account_id', ), response_message=SimpleResponse.ProtoModel()) @user_required def accept(self, req): """Accept a connection request from a user. :param req.account_id: (int) ID of the account that sent the connection request. """ resp = SimpleResponse() from_id = req.account_id try: account = get_current_user() services.connections.accept_request(account.id, from_id) return resp.ToMessage() except exp.ServiceExp as e: handle_exception(e) @SimpleRequest.method(name='decline', path='decline', http_method='POST', request_fields=('account_id', ), response_message=SimpleResponse.ProtoModel()) @user_required def decline(self, req): """Decline a connection request form a user. :param req.account_id: (int) ID of the account that sent the connection request. """ resp = SimpleResponse() from_id = req.account_id try: account = get_current_user() services.connections.decline_request(account.id, from_id) return resp.ToMessage() except exp.ServiceExp as e: handle_exception(e) @SimpleRequest.method(name='remove', path='remove', http_method='POST', request_fields=('account_id', ), response_message=SimpleResponse.ProtoModel()) @user_required def remove(self, req): """Remove an existing connection. :param req.account_id: (int) ID of the account to be removed. """ resp = SimpleResponse() other_id = req.account_id try: account = get_current_user() services.connections.remove_connection(account.id, other_id) return resp.ToMessage() except exp.ServiceExp as e: handle_exception(e) @SimpleRequest.method(name='my', path='my', http_method='GET', request_fields=(), response_message=ConnApiModel.ProtoCollection()) @user_required def my(self, req): """Retrieve the current account's connections.""" try: account = get_current_user() dtos = services.connections.my_connections(account.id) print(dtos) api_models = [ConnApiModel.from_conn_dto(d) for d in dtos] return ConnApiModel.ProtoCollection()(items=api_models) except exp.ServiceExp as e: handle_exception(e) @SimpleRequest.method( name='pending', path='pending', http_method='GET', request_fields=(), response_message=PendingConnApiModel.ProtoCollection()) @user_required def pending(self, req): """Retrieve the current account's pending connections requests.""" try: account = get_current_user() dtos = services.connections.pending_requests(account.id) api_models = [PendingConnApiModel.from_conn_dto(d) for d in dtos] return PendingConnApiModel.ProtoCollection()(items=api_models) except exp.ServiceExp as e: handle_exception(e)