def add_event( self, calendar_id, event, client_id=client, send_notifications="False", ): url = "{host}/{api}/calendars/{calendar_id}/events".\ format(host=self.host, api=self.api, calendar_id=calendar_id) request_body = create_string_from_json({ "end": event.end, "start": event.start, "attendees": event.attendees, "iCalUID": event.iCalUID }) params = {"sendNotifications": send_notifications} 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_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 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 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 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 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 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 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 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 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 trash_message(self, user_id, message_id): """ Moves the specified message to the trash. :param user_id: :param message_id: :return: response """ url = "{host}/{api}/{user_id}/messages/{message_id}/trash".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, scope=scope_mail) api.send_post() return api.response
def import_event(self, calendar_id, recurrent_event): request_body = { "end": recurrent_event.end, "start": recurrent_event.start, "attendees": recurrent_event.attendees, "iCalUID": recurrent_event.iCalUID, "recurrence": recurrent_event.recurrence } url = "{host}/{api}/calendars/{calendar_id}/events/import".\ format(host=self.host, api=self.api, calendar_id=calendar_id) req = HttpLib(url, json=request_body) 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 query(self, json_query): """ Метод отправляет Post запрос на Freebusy :param json_query: json для запроса :returns: ответ от сервера :returns: модель полученная из ответа """ http = HttpLib(url="{host}/{api}/freeBusy".format(host=self.host, api=self.api), json=json_query) http.auth_to_google(client=client, scope=scope_calendar) http.send_post() freebusy = FreeBusyModel() freebusy.init_freebusy_from_json(HttpLib.get_response_json(http.response)) return http.response, freebusy
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 batch_delete(self, user_id, request_body): """ Deletes many messages by message ID Args: user_id (str): The user's email address request_body(str): Body for request Returns: response (requests.Response) model (MessageModel) """ api = HttpLib(url="{host}/{api}/{user_id}/messages/batchDelete".format( host=self.host, api=self.api, user_id=user_id), json=create_json_from_string(request_body)) api.auth_to_google(client=client, scope=scope_mail) api.send_post() response = api.response return response
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 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 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 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))
def insert_calendar(self, calendar_model_initial): """ Insert calendar. This operation insert new calendar in the primary calendar of an account. :param calendar_model_initial: :return: response calendar_output_model """ log_info("Insert calendar...") json_insert = CalendarsModel().create_json_for_request(calendar_model_initial.summary, calendar_model_initial.description, calendar_model_initial.location) http = HttpLib(url=self.host, json=json_insert) http.auth_to_google(client=client, scope=scope_calendar) response = http.send_post().response calendar_model_output = CalendarsModel().get_calendar_model_actual(http.get_response_json(http.response)) return response, calendar_model_output
def add_result(self, test_id, req_body): url = "{host}/{api}/add_result/{test_id}".format(host=self.host, api=self.api, test_id=test_id) req = HttpLib(url=url, auth=(self.username, self.password), json=req_body, header=self.content_type) req.send_post() assert req.get_response_status_code(req.response) == 200, req.get_response_text(req.response)