def list(self, user_id, client_id=client): """ Args: user_id (str): id пользователя client_id(dict): клиент под которым выполняется авторизация Returns: response (requests.Response) model_array (list) """ api = HttpLib(url="{host}/{api}/{user_id}/messages".format( host=self.host, api=self.api, user_id=user_id)) api.auth_to_google(client=client_id, scope=scope_mail) api.send_get() response = api.response model_array = [] log_info(api.response.text) if api.get_response_json(response)["resultSizeEstimate"] == 0: model_array.append(MessageModel().get_empty_list_from_json( api.get_response_json(response))) return response, model_array else: json_array = api.get_response_json(response)["messages"] for item in json_array: model = MessageModel().get_basic_message_from_json(item) model_array.append(model) return response, model_array
def get_thread_model_from_get(self, **kwargs): self.thread_id = kwargs['id'] self.history_id = kwargs['historyId'] for message in kwargs['messages']: self.messages.append( MessageModel().get_basic_message_from_json(message)) return self
def untrash(self, user_id, message_id): """ Removes the specified message from the trash. :param user_id: the user's email address :param message_id: the ID of the message to delete. :return: response (requests.Response) model (MessageModel) """ api = HttpLib( url="{host}/{api}/{user_id}/messages/{message_id}/untrash".format( host=self.host, api=self.api, user_id=user_id, message_id=message_id)) api.auth_to_google(client=client, scope=scope_mail) api.send_post() model = MessageModel() model.get_basic_message_from_json(api.get_response_json(api.response)) return api.response, model
def get(self, user_id, message_id, client_id=client): """ Args: user_id (str): id пользователя message_id (str): id сообщения client_id(dict): данные аутентицикации пользователя Returns: response (requests.Response) model (MessageModel) модель с общими полями """ api = self.basic_get(user_id, message_id, client_id) return api.response, MessageModel().get_basic_message_from_json( api.get_response_json(api.response))
def get_iis(self, user_id, message_id, client_id=client): """ IIS - model for methods import, insert, send Args: user_id (str): id пользователя message_id (str): id сообщения client_id(dict): клиент под которым выполняется авторизация Returns: response (requests.Response) model (MessageModel) модель с общими полями + header and data """ api = self.basic_get(user_id, message_id, client_id) return api.response, MessageModel().get_iis_message_from_json( api.get_response_json(api.response))
def send_message(self, user_id, raw): """ :param user_id: The user's email address :param raw: The entire email message in an RFC 2822 formatted and base64url encoded string :return: response and MessageModel """ url = "{host}/{api}/{user_id}/messages/send".format(host=self.host, api=self.api, user_id=user_id) request_body = {"raw": raw} api = HttpLib(url=url, json=request_body, header=self.header) api.auth_to_google(client=client, scope=scope_mail) api.send_post() return api.response, MessageModel().get_basic_message_from_json( api.get_response_json(api.response))
def list_draft(self, user_id): url = "{host}/{api}/{user_id}/drafts".format(host=self.host, api=self.api, user_id=user_id) api = HttpLib(url=url, header=self.header) api.auth_to_google(client=client, scope=scope_mail) api.send_get() drafts_json = api.get_response_json(api.response)["drafts"] model_array = [] for item in drafts_json: model = MessageModel().get_draft_message_model_from_json(item) model_array.append(model) return api.response, model_array
def create_draft(self, user_id, raw): """ Args: user_id (str): user id raw (str): string in the base64 format Returns: api.response (requests.Response): response from the server model (MessageModel): the model is getting from the server """ url = "{host}/{api}/{user_id}/drafts".format(host=self.host, api=self.api, user_id=user_id) request_body = {"id": "", "message": {"raw": raw}} api = HttpLib(url=url, json=request_body, header=self.header) api.auth_to_google(client=client, scope=scope_mail) api.send_post() return api.response, MessageModel().get_draft_message_model_from_json( api.get_response_json(api.response))
def modify_messages(self, user_id, message_id, add_labels_list=None, remove_label_list=None): """ Modifies the labels on the specified message Args: user_id (str): The user's email address message_id(str): Message id add_labels_list(lust): List of added labels remove_label_list(list): List of remove labels Returns: response (requests.Response) model_array (массив MessageModel) """ if remove_label_list is None: remove_label_list = [] if add_labels_list is None: add_labels_list = [] request_body = { "addLabelIds": add_labels_list, "removeLabelIds": remove_label_list } api = HttpLib( url="{host}/{api}/{user_id}/messages/{message_id}/modify".format( host=self.host, api=self.api, user_id=user_id, message_id=message_id), json=request_body) log_info("Modify messages URL: {url}, request: {json}".format( url=api.url, json=api.json)) api.auth_to_google(client=client, scope=scope_mail) api.send_post() log_pretty_json(api.response.text, "Modify messages response") model = MessageModel().get_basic_message_from_json( api.get_response_json(api.response)) return api.response, model
def get_draft(self, user_id, draft_message_id): """ Args: user_id (str): user id draft_message_id (str): draft message id in the draft Returns: api.response (requests.Response): response from the server model (MessageModel): the model is getting from the server """ url = self.__get_message_url(user_id=user_id, draft_message_id=draft_message_id) api = HttpLib(url=url, header=self.header) api.auth_to_google(client=client, scope=scope_mail) api.send_get() model = MessageModel().get_draft_message_model_from_json( api.get_response_json(api.response)) return api.response, model
def import_text_message(self, user_id, raw): """ Imports text message to user's inbox. Args: user_id (str): The user's email address text_message (obj): Text message as object :return: response (requests.response), model (MessageModel) """ url = "{host}/{api}/{user_id}/messages/import".format(host=self.host, api=self.api, user_id=user_id) json = {"raw": raw} api = HttpLib(url=url, json=json, header=self.header) api.auth_to_google(client=client, scope=scope_mail) api.send_post() log_info(api.response.text) model = MessageModel().get_basic_message_from_json( api.get_response_json(api.response)) return api.response, model
def send_draft_message(self, user_id, draft_message_id, message_raw): """ Args: user_id (str): user id message_raw (str): message string in the base64 format draft_message_id (str): draft message id in the draft """ url = "{host}/{api}/{user_id}/drafts/send".format(host=self.host, api=self.api, user_id=user_id) send_json = {"id": draft_message_id, "message": {"raw": message_raw}} api = HttpLib(url=url, header=self.header, json=send_json) api.auth_to_google(client=client, scope=scope_mail) api.send_post() model = MessageModel().get_basic_message_from_json( api.get_response_json(api.response)) return api.response, model
def update_draft(self, user_id, draft_message_id, raw_txt): """ Args: user_id (str): user id draft_message_id (str): draft message id in the draft raw_txt (str): string in the base64 format Returns: api.response (requests.Response): response from the server model (MessageModel): the model is getting from the server """ url = self.__get_message_url(user_id=user_id, draft_message_id=draft_message_id) json_update = {"id": draft_message_id, "message": {"raw": raw_txt}} api = HttpLib(url=url, header=self.header, json=json_update) api.auth_to_google(client=client, scope=scope_mail) api.send_put() model = MessageModel().get_draft_message_model_from_json( api.get_response_json(api.response)) return api.response, model
def insert_message(self, user_id, text): """ Send request to inserts a message into user's mailbox Args: user_id (str): The user's email address text (str): message text Returns: response (requests.Response) model (MessageModel) """ json = {"raw": text} api = HttpLib(url="{host}/{api}/{user_id}/messages".format( host=self.host, api=self.api, user_id=user_id), header=self.header, json=json) api.auth_to_google(client=client, scope=scope_mail) api.send_post() log_info(api.response.text) return api.response, MessageModel().get_basic_message_from_json( api.get_response_json(api.response))