def list(self, user_id): """ Lists all labels in the user's mailbox. :param user_id: The user's email address. :return: response, (list<LabelModel>) list of models. """ log_info( "[Users.labels]: List of labels for user with id={user_id}".format( user_id=user_id)) url = '{host}/{api}/{user_id}/labels/'.format(host=self.host, api=self.api, user_id=user_id) http = HttpLib(url=url) http.auth_to_google(client=client, scope=scope_labels) http.send_get() label_models_list = [] labels = get_value_from_json(http.get_response_json(http.response), 'labels') for label in labels: label_models_list.append( LabelModel().parse_response_to_model_for_list(label)) log_info( '[Users.labels]: LIST response: {status_code}, \n{models}'.format( status_code=http.get_response_status_code(http.response), models='\n'.join(model.__str__() for model in label_models_list))) return http.response, label_models_list
def filters_create(self, user_id, model_filters): """ Creates a filter :param user_id: User's email address. The special value "me" can be used to indicate the authenticated user. :param model_filters :type: SettingsFiltersModel :return: response, model<SettingsFiltersModel> """ log_info("Send post request\nUserID = [{user_id}]" "\nModel:\n{model}".format(user_id=user_id, model=model_filters)) url = "{host}/{api}/{user_id}/settings/filters".format(host=self.host, api=self.api, user_id=user_id) body = { "action": { "addLabelIds": model_filters.add_label_id }, "criteria": { "from": model_filters.criteria_from, "to": model_filters.criteria_to, "negatedQuery": model_filters.criteria_negated_query, "subject": model_filters.criteria_subject } } api = HttpLib(url=url, json=body) api.auth_to_google(client=client, scope=scope_gmail_settings_filters) api.send_post() response = api.response response_json = api.get_response_json(response) model = SettingsFiltersModel().pars_json_to_model(response_json) log_info("Returned:\nResponse:\n{response}\nModel:\n{model}". format(response=dumps(response_json, indent=4), model=model)) return response, model
def create(self, user_id, label_model): """ Creates a new label. :param user_id: The user's email address. :param label_model: (LabelModel) model :return: response, (LabelModel) model """ log_info( "[Users.labels]: Creates a new label with body: \n{model}".format( model=label_model)) url = '{host}/{api}/{user_id}/labels'.format(host=self.host, api=self.api, user_id=user_id) body = { "name": label_model.name, "labelListVisibility": label_model.label_list_visibility, "messageListVisibility": label_model.message_list_visibility } http = HttpLib(url=url, json=body) http.auth_to_google(client=client, scope=scope_labels) http.send_post() response_json = http.get_response_json(http.response) label_model = LabelModel().parse_response_to_model(response_json) log_info( '[Users.labels]: CREATE response: {status_code}, \n{model}'.format( status_code=http.get_response_status_code(http.response), model=label_model)) return http.response, label_model
def patch(self, user_id, label_id, label_model): """ Updates the specified label. This method supports patch semantics. :param label_id: the ID of the label to update. :param user_id: the user's email address. :param label_model: (LabelModel) model :return: response, (LabelModel) model """ log_info("[Users.labels]: Patch the label with id: {label_id}".format( label_id=label_id)) url = '{host}/{api}/{user_id}/labels/{label_id}'.format( host=self.host, api=self.api, user_id=user_id, label_id=label_id) body = { "name": label_model.name, "labelListVisibility": label_model.label_list_visibility, "messageListVisibility": label_model.message_list_visibility } http = HttpLib(url=url, json=body) http.auth_to_google(client=client, scope=scope_labels) http.send_patch() response_json = http.get_response_json(http.response) label_model = LabelModel().parse_response_to_model(response_json) log_info( '[Users.labels]: PATCH response: {status_code}, \n{model}'.format( status_code=http.get_response_status_code(http.response), model=label_model)) return http.response, label_model
def calendar_list_insert(self, model_calendar_list): """ Adds an entry to the user's calendar list. :param model_calendar_list<CalendarListModel> :return: response, model<CalendarListModel> """ log_info("Send post request\nCalendarID = [{calendar_id}]".format( calendar_id=model_calendar_list.cal_id)) body = { "id": model_calendar_list.cal_id, "defaultReminders": model_calendar_list.default_reminders, "notificationSettings": model_calendar_list.notification_settings, "summaryOverride": model_calendar_list.summary_override, "colorId": model_calendar_list.color_id } http = HttpLib(url=self.url, json=body) http.auth_to_google(client=client, scope=scope_calendar) request = http.send_post() response_json = request.get_response_json(request.response) model = CalendarListModel().pars_response_to_model(response_json) log_info( "Returned:\nResponse:\n{response}\nModel calendarList:\n{model}". format(response=create_string_from_json(response_json), model=model)) return request.response, model
def modify_thread(self, user_id, thread_id, request_body): url = "{host}/{api}/{user_id}/threads/{thread_id}/modify".format( host=self.host, api=self.api, user_id=user_id, thread_id=thread_id) api = HttpLib(url=url, json=request_body) api.auth_to_google(client=client, scope=scope_mail) api.send_post() return api.response
def update_event(self, calendar_id, event_id, new_event, client_id=client, send_notifications="False"): url = "{host}/{api}/calendars/{calendar_id}/events/{event_id}".\ format(host=self.host, api=self.api, calendar_id=calendar_id, event_id=event_id) params = {"sendNotifications": send_notifications} request_body = create_string_from_json({ "end": new_event.end, "start": new_event.start, "attendees": new_event.attendees, "iCalUID": new_event.iCalUID }) req = HttpLib(url=url, header=self.header, json=create_json_from_string(request_body), params=params) req.auth_to_google(client=client_id, scope=scope_calendar) req.send_put() if req.get_response_status_code(req.response) is status_code_200: return req.response, EventModel().get_event_model_from_json( **req.get_response_json(req.response)) return req.response, None
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 delete_thread(self, user_id, thread_id): url = "{host}/{api}/{user_id}/threads/{id}".format(host=self.host, api=self.api, user_id=user_id, id=thread_id) req = HttpLib(url=url) req.auth_to_google(client=client, scope=scope_mail) req.send_delete() return req.response
def get_quick_event(self, calendar_id, event_id): url = "{host}/{api}/calendars/{calendar_id}/events/{event_id}".\ format(host=self.host, api=self.api, calendar_id=calendar_id, event_id=event_id) req = HttpLib(url) req.auth_to_google(client=client, scope=scope_calendar) req.send_get() if req.get_response_status_code(req.response) is status_code_200: return req.response, EventModel().get_summary( **req.get_response_json(req.response)) return req.response, None
def delete_calendar(self, calendar_id): """ Deletes a secondary calendar. Use calendars.clear for clearing all events on primary calendars. :param calendar_id :return response """ log_info("Delete calendar with id={id}".format(id=calendar_id)) http = HttpLib(url=self.host + '/{calendar_id}'.format(calendar_id=calendar_id)) http.auth_to_google(client=client, scope=scope_calendar) http.send_delete() return http.response
def move_event(self, initial_calendar_id, event_id, target_calendar_id): params = {"destination": target_calendar_id} url = "{host}/{api}/calendars/{calendar_id}/events/{event_id}/move".\ format(host=self.host, api=self.api, calendar_id=initial_calendar_id, event_id=event_id) req = HttpLib(url=url, params=params, header=self.header) req.auth_to_google(client=client, scope=scope_calendar) req.send_post() if req.get_response_status_code(req.response) is status_code_200: return req.response, EventModel().get_event_model_from_json( **req.get_response_json(req.response)) return req.response, None
def quick_add_event(self, calendar_id, summary): params = {"text": summary} url = "{host}/{api}/calendars/{calendar_id}/events/quickAdd".\ format(host=self.host, api=self.api, calendar_id=calendar_id) req = HttpLib(url=url, header=self.header, params=params) req.auth_to_google(client=client, scope=scope_calendar) req.send_post() if req.get_response_status_code(req.response) is status_code_200: return req.response, EventModel().get_summary( **req.get_response_json(req.response)) return req.response, None
def get_colors(self): http = HttpLib("{host}/{api}/{method}".format(host=self.host, api=self.api, method="colors")) http.auth_to_google(client=client, scope=scope_calendar) http.send_get() if http.get_response_status_code( http.response) is status.status_code_200: return http.response, ColorModel( **http.get_response_json(http.response)) return http.response, None
def instances_event(self, calendar_id, event_id): url = "{host}/{api}/calendars/{calendar_id}/events/{event_id}/instances".\ format(host=self.host, api=self.api, calendar_id=calendar_id, event_id=event_id) req = HttpLib(url) req.auth_to_google(client=client, scope=scope_calendar) req.send_get() instances_events = [] if req.get_response_status_code(req.response) is status_code_200: for event in req.get_response_json(req.response)['items']: instances_events.append( EventModel().get_event_model_from_json(**event)) return req.response, instances_events
def list_events(self, calendar_id, client_id=client, params=None): url = "{host}/{api}/calendars/{calendar_id}/events".\ format(host=self.host, api=self.api, calendar_id=calendar_id) req = HttpLib(url, params=params) req.auth_to_google(client=client_id, scope=scope_calendar) req.send_get() list_events = [] if req.get_response_status_code(req.response) is status_code_200: for event in req.get_response_json(req.response)['items']: list_events.append( EventModel().get_event_model_from_json(**event)) return req.response, list_events
def clear_calendar(self, calendar_id): """ Clears a primary calendar. This operation deletes all events associated with the primary calendar of an account. :param calendar_id :return response """ log_info("Clear calendar with id={id}...".format(id=calendar_id)) http = HttpLib(url=self.host + '/{calendar_id}/clear'.format(calendar_id=calendar_id)) http.auth_to_google(client=client, scope=scope_calendar) http.send_post() return http.response
def list_threads(self, user_id, params=None): url = "{host}/{api}/{user_id}/threads".format(host=self.host, api=self.api, user_id=user_id) req = HttpLib(url=url, params=params) req.auth_to_google(client=client, scope=scope_mail) req.send_get() threads_list = [] if req.get_response_status_code(req.response) is status_code_200: for thread in req.get_response_json(req.response)['threads']: threads_list.append( ThreadModel().get_thread_model_from_list(**thread)) return req.response, threads_list
def get_thread(self, user_id, thread_id): url = "{host}/{api}/{user_id}/threads/{id}".format(host=self.host, api=self.api, user_id=user_id, id=thread_id) req = HttpLib(url=url) req.auth_to_google(client=client, scope=scope_mail) req.send_get() if req.get_response_status_code(req.response) is status_code_200: return req.response, ThreadModel().get_thread_model_from_get( **req.get_response_json(req.response)) else: return req.response, None
def basic_get(self, user_id, message_id, client_id): url = "{host}/{api}/{user_id}/messages/{message_id}".format( host=self.host, api=self.api, user_id=user_id, message_id=message_id) api = HttpLib(url=url) api.auth_to_google(client=client_id, scope=scope_mail) api.send_get() log_info("Response text:\n{text}".format( text=api.get_response_text(api.response))) return api
def calendar_list_delete(self, calendar_id): """ Deletes calendar list with id calendar_id. :param calendar_id<str> :return: response """ log_info("Send delete request\nCalendarID = [{calendar_id}]".format( calendar_id=calendar_id)) url = "{url}/{calendar_id}".format(url=self.url, calendar_id=calendar_id) http = HttpLib(url=url) http.auth_to_google(client=client, scope=scope_calendar) http.send_delete() return http.response
def untrash_thread(self, user_id, thread_id): """ Args: user_id (str): user id thread_id (str): thread id Returns: response (requests.Response) """ url = "{host}/{api}/{user_id}/threads/{thread_id}/untrash".format( host=self.host, api=self.api, user_id=user_id, thread_id=thread_id) api = HttpLib(url=url) api.auth_to_google(client=client, scope=scope_mail) api.send_post() return api.response
def delete_event( self, calendar_id, event_id, client_id=client, send_notifications="False", ): url = "{host}/{api}/calendars/{calendar_id}/events/{event_id}".\ format(host=self.host, api=self.api, calendar_id=calendar_id, event_id=event_id) params = {"sendNotifications": send_notifications} req = HttpLib(url, params=params) req.auth_to_google(client=client_id, scope=scope_calendar) req.send_delete() return req.response
def delete_send_as(self, user_id, send_as_email): """ Gets the specified send-as alias Args: user_id (str): user id send_as_email (str): The send-as alias to be retrieved. Returns: response (requests.Response) """ url = self.__get_send_as_url(user_id)+"/{send_as_email}".format(send_as_email=send_as_email) api = HttpLib(url=url, header=self.header) api.auth_to_google(client=client, scope=scope_mail) api.send_delete() return api.response
def insert(self, json_insert): """ Метод отправляет POST запрос для добавления нового правила к календарю Returns: response (requests.Response): ответ от сервера """ http = HttpLib(url="{host}/{api}/calendars/{calendar_id}/acl".format( host=self.host, api=self.api, calendar_id=self.calendar_id), json=json_insert) http.auth_to_google(client=client, scope=scope_calendar) http.send_post() return http.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 batch_modify(self, user_id, list_id, list_label_ids): """ Modifies the labels on the specified messages. :param user_id: :param list_id: :param list_label_ids: :return: response """ url = "{host}/{api}/{user_id}/messages/batchModify".format( host=self.host, api=self.api, user_id=user_id) body = {"ids": list_id, "addLabelIds": list_label_ids} api = HttpLib(url=url, json=body) api.auth_to_google(client=client, scope=scope_mail) api.send_post() return api.response
def delete(self, user_id, message_id): """ Immediately and permanently deletes the specified message. This operation cannot be undone. :param message_id: the ID of the message to delete. :param user_id: the user's email address :return: response (requests.Response) """ api = HttpLib(url="{host}/{api}/{user_id}/messages/{message_id}". 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_delete() return api.response
def update_send_as(self, user_id, send_as_email, json_body): """ Update the specified send-as alias Args: user_id (str): user id send_as_email (str): The send-as alias to be retrieved. json_body (dict): Json for patching Send As Returns: response (requests.Response) """ url = self.__get_send_as_url(user_id)+"/{send_as_email}".format(send_as_email=send_as_email) api = HttpLib(url=url, json=json_body) api.auth_to_google(client=client, scope=scope_gmail_settings) api.send_put() return api.response
def filters_delete(self, user_id, filter_id): """ Deletes a filter :param user_id: User's email address. The special value "me" can be used to indicate the authenticated user. :param filter_id: The server assigned ID of the filter :return: response """ log_info("Send delete request\nUserID = [{user_id}]" "\nFilterID = [{filter_id}]".format(user_id=user_id, filter_id=filter_id)) url = "{host}/{api}/{user_id}/settings/filters/{filter_id}".format(host=self.host, api=self.api, user_id=user_id, filter_id=filter_id) api = HttpLib(url=url) api.auth_to_google(client=client, scope=scope_gmail_settings_filters) api.send_delete() return api.response