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 insert_acl_rule(role, scope_type, scope_value): """ Метод создает правило в календаре template Create Acl role | ${role} | ${scope_value} | ${scope_type} Args: role (str): тип роли scope_value (str): email к кому применить правило scope_type (str): тип пользователя Return: expected_model(ACLModel): модель правила """ json_insert = create_json_role(role, scope_type, scope_value) log_info("Create model from json (expected model)") expected_model = ACLModel() expected_model.init_acl_from_json(json_data=json_insert) actual_response = AclApi().insert(json_insert) actual_status_code = HttpLib.get_response_status_code(actual_response) actual_json = HttpLib.get_response_json(actual_response) log_pretty_json(actual_json, "This is response json from server") assert (actual_status_code == status_code_200),\ "Insert ACL rule Failed. The response status code not equal 200, current status code: {actual_code}".\ format(actual_code=actual_status_code) return expected_model
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): """ 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 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 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 update_acl_rule(role, scope_type, scope_value): """ Метод обновляет правило в календаре Args: role (str): тип роли scope_type (str): тип пользователя scope_value (str): email к кому применияется правило Return: expected_model(ACLModel): модель правила """ json_update = create_json_role(role, scope_type, scope_value) log_info("Create model from json (expected model)") expected_model = ACLModel() expected_model.init_acl_from_json(json_data=json_update) actual_response = AclApi().update(json_data=json_update, rule_id="user:{scope_value}". format(scope_value=scope_value)) actual_status_code = HttpLib.get_response_status_code(actual_response) actual_json = HttpLib.get_response_json(actual_response) log_pretty_json(actual_json, "This is response json from server") assert (actual_status_code == status_code_200), \ "Updating ACL rule Failed. The response status code not equal 200, current status code: {actual_code}". \ format(actual_code=actual_status_code) return expected_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 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 get_pop(self, user_id="me", oauth_client=client): """ Send get request to Users.settings: getPop :param user_id: user email address for request :param oauth_client: client that should be authorized with oauth 2.0 :return: response and status code """ url = "{host}/{api}/{service}".format( host=self.host, api=self.api, service=url_getPop.format(userId=user_id)) log_info("Get pop url is: {url}".format(url=url)) http = HttpLib(url) http.auth_to_google(client=oauth_client, scope=scope_gmail_setting) http.send_get() log_pretty_json(http.get_response_json(http.response), "Get pop response") return http.get_response_json( http.response), http.get_response_status_code(http.response)
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 delete_calendar_list(calendar_id): """ Delete calendar list :param calendar_id: ID created calendar """ response = CalendarListApi().calendar_list_delete(calendar_id) response_status_code = HttpLib.get_response_status_code(response) assert (response_status_code == status_code_204), \ "Delete calendar list failed: Status code isn't '204'." \ "\nResponse:\n{text}Status code = {status_code}".format(text=HttpLib.get_response_json(response), status_code=response_status_code)
def list_steps(self, case_id): url = "{host}/{api}/get_case/{case_id}".format(host=self.host, api=self.api, case_id=case_id) req = HttpLib(url=url, auth=(self.username, self.password), header=self.content_type) req.send_get() steps_list = [] if req.response.status_code == 200: for step in req.get_response_json(req.response)['custom_steps_separated']: steps_list.append(CaseStepModel().get_step_model_from_json(content=step['content'], expected=step['expected'])) return steps_list else: return None
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 update_imap(self, imap_model, user_id="me", oauth_client=client): """ Send put request to Users.settings: getVacation :param imap_model: Imap Model object :param user_id: user email address for request :param oauth_client: client that should be authorized with oauth 2.0 :return: response and status code """ url = "{host}/{api}/{service}".format( host=self.host, api=self.api, service=url_getImap.format(userId=user_id)) log_info("Update imap url is: {url}".format(url=url)) body = imap_model.create_json_from_model() http = HttpLib(url, json=body) http.auth_to_google(client=oauth_client, scope=scope_gmail_setting) http.send_put() log_pretty_json(http.get_response_json(http.response), "Update imap response") return http.get_response_json( http.response), http.get_response_status_code(http.response)
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 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 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 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 untrash_thread(user_id, thread_id): """ Args: user_id (str): user id thread_id (str): thread id """ response = ThreadsApi().untrash_thread(user_id, thread_id) response_code = HttpLib.get_response_status_code(response) response_json = HttpLib.get_response_json(response) log_info("Move thread with id {thread_id} from trash".format( thread_id=thread_id)) log_pretty_json(response_json, "This is json response") assert response_code == status_code_200, "Error while moving thread with {id} from trash".format( id=thread_id)
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 get_history_list(self, user_id, start_history_id): """ Gets the history list Args: user_id (str): user id start_history_id (str): specified startHistoryId for request Returns: response (requests.Response) model (HistoryModel): the model is getting from the server """ url = "{host}/{api}/{user_id}/history".format(host=self.host, api=self.api, user_id=user_id) params = {'startHistoryId': start_history_id} api = HttpLib(url=url, header=self.header, params=params) api.auth_to_google(client=client, scope=scope_mail) api.send_get() response = api.response log_pretty_json(api.get_response_json(response), "This is response json from server") model = HistoryModel().get_history_model_from_json( api.get_response_json(response)) log_info(model) return response, model
def get_calendar(self, calendar_model_output): """ Get calendar. This operation get calendar. :param calendar_model_output: :return: response calendar_output_actual """ log_info("Get calendar...") url = self.host + '/{calendar_id}'.format(calendar_id=calendar_model_output.id) http = HttpLib(url=url) http.auth_to_google(client=client, scope=scope_calendar) response = http.send_get().response response_json = http.get_response_json(http.response) calendar_model_actual = CalendarsModel().get_calendar_model_actual(response_json) return response, calendar_model_actual
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 get_profile(self, user_id): """ Args: user_id (str): user id Returns: api.response (requests.Response): response from the server model (UsersModel): the model is getting from the server """ url = self.__get_message_url(user_id=user_id) api = HttpLib(url=url, header=self.header) api.auth_to_google(client=client, scope=scope_mail) api.send_get() model = UsersModel().get_users_model_from_json( api.get_response_json(api.response)) return api.response, model
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 get_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) model (SendAsModel): the model is getting from the server """ 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_get() model = SendAsModel().get_send_as_model_from_json(api.get_response_json(api.response)) return api.response, model
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 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