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 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 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 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 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 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 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 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 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 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_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 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 settings_list(self): """ Method to build setings list request Returns: list of SettingsModel objects: list of settings from response int: response status code """ settings = [] url = "{host}/{api}/settings".format(host=self.host, api=self.api) http = HttpLib(url=url) http.auth_to_google(client=client, scope=scope_calendar) http.send_get() settings_items = get_value_from_json( http.get_response_json(http.response), 'items') for item in settings_items: settings.append(SettingsModel().create_model_from_json(item)) return settings, http.get_response_status_code(http.response)
def settings_get(self, setting_id): """ Method to build setings get request Args: setting_id(str): settings id Returns: requests.response: request response int: response status code """ url = "{host}/{api}/settings/{setting}".format(host=self.host, api=self.api, setting=setting_id) http = HttpLib(url) http.auth_to_google(client=client, scope=scope_calendar) http.send_get() return http.get_response_json( http.response), http.get_response_status_code(http.response)
def forwarding_list(self, user_id="me", client_id=client): """ Build forwarding list request :param user_id: user email :param client_id: auth client :return: response, ForwardingModel object """ forwarding = [] url = "{host}/{api}/{user_id}/settings/forwardingAddresses".format(host=self.host, api=self.api, user_id=user_id) log_info(url) http = HttpLib(url=url) http.auth_to_google(client=client_id, scope=scope_settings_forwarding) http.send_get() log_pretty_json(http.response.text, "Forwarding list response") forwarding_items = get_value_from_json(http.get_response_json(http.response), 'forwardingAddresses') for item in forwarding_items: forwarding.append(ForwardingModel().create_model_from_json(item)) return http.response, forwarding
def filters_get(self, user_id, filter_id): """ Gets 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, model<SettingsFiltersModel> """ log_info("Send get 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_least_one) api.send_get() 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 forwarding_get(self, user_id, email, client_id=client): """ Build forwarding get request :param user_id: user email :param email: forwarding email :param client_id: auth client :return: response, ForwardingModel object """ url = "{host}/{api}/{user_id}/settings/forwardingAddresses/{forward_email}".format(host=self.host, api=self.api, user_id=user_id, forward_email=email) log_info(url) http = HttpLib(url) http.auth_to_google(client=client_id, scope=scope_settings_forwarding) http.send_get() forwarding_model = ForwardingModel().create_model_from_json(http.get_response_json(http.response)) log_info("Get forwarding model. Model is: {forwarding_model}".format(forwarding_model=forwarding_model)) return http.response, forwarding_model
def get_send_as_list(self, user_id): """ Gets the specified send-as alias list Args: user_id (str): user id Returns: response (requests.Response) model (SendAsModel): the list of models are getting from the server """ url = self.__get_send_as_url(user_id=user_id) api = HttpLib(url=url, header=self.header) api.auth_to_google(client=client, scope=scope_mail) api.send_get() response = api.response json_array = api.get_response_json(response)['sendAs'] model_array = [] for item in json_array: model = SendAsModel().get_send_as_model_from_json(item) model_array.append(model) return response, model_array
def get_attachment(self, user_id, message_id, attachment_id): """ :param user_id: Email owner :param message_id: Id message :param attachment_id: Id attachment :return: Attachment model """ log_info("Get attachment in API by user_id = {user_id}, message_id = {message_id} and attachment_id = " "{attachment_id}".format(user_id=user_id, message_id=message_id, attachment_id=attachment_id)) url = "{host}/{api}/{user_id}/messages/{message_id}/attachments/{attachment_id}".\ format(host=self.host, api=self.api, user_id=user_id, message_id=message_id, attachment_id=attachment_id) http = HttpLib(url=url) http.auth_to_google(client=client, scope=scope_gmail_drafts) http.send_get() response_json = http.get_response_json(http.response) return http.response, AttachmentModel().create_model_from_json(response_json)
def get(self, label_id, user_id): """ Gets the specified label. :param label_id: The ID of the label to update. :param user_id: The user's email address. :return: response, (LabelModel) model """ log_info("[Users.labels]: Get 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) http = HttpLib(url=url) http.auth_to_google(client=client, scope=scope_labels) http.send_get() response_json = http.get_response_json(http.response) label_model = LabelModel().parse_response_to_model(response_json) log_info( '[Users.labels]: GET 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_list(self, user_id): """ Lists the message filters of a Gmail user. :param user_id: User's email address. The special value "me" can be used to indicate the authenticated user. :returns: response, list(<SettingsFiltersModel>) """ log_info("Send get request\nUserID = [{user_id}]".format(user_id=user_id)) url = "{host}/{api}/{user_id}/settings/filters".format(host=self.host, api=self.api, user_id=user_id) api = HttpLib(url=url) api.auth_to_google(client=client, scope=scope_gmail_settings_filters_least_one) api.send_get() model_filter_list = [] response = api.response response_json = api.get_response_json(response) for filter_json in response_json['filter']: model_filter_list.append(SettingsFiltersModel().pars_json_to_model(filter_json)) log_info("Returned:\nResponse:\n{response}\nModel filter list:\n{model_list}". format(response=dumps(response_json, indent=4), model_list='\n'.join(str(item.__dict__) for item in model_filter_list))) return api.response, model_filter_list
def get(self, rule_id): """ Метод отпрвляет GET запрос для получения правила применяемого к календарю Returns: response (requests.Response): ответ от сервера model (ACL): модель полученная из ответа сервера """ http = HttpLib( url="{host}/{api}/calendars/{calendar_id}/acl/{rule_id}".format( host=self.host, api=self.api, calendar_id=self.calendar_id, rule_id=rule_id)) http.auth_to_google(client=client, scope=scope_calendar) http.send_get() model = ACLModel() model.init_acl_from_json(HttpLib.get_response_json(http.response)) return http.response, model
def list(self): """ Метод отпрвляет GET запрос для получения правил применяемых к календарю Returns: response (requests.Response): ответ от сервера model_array (ACL): массив моделей полученная из ответа сервер """ http = HttpLib(url="{host}/{api}/calendars/{calendar_id}/acl".format( host=self.host, api=self.api, calendar_id=self.calendar_id)) http.auth_to_google(client=client, scope=scope_calendar) http.send_get() json_array = HttpLib.get_response_json(http.response)["items"] model_array = [] for item in json_array: it = ACLModel() it.init_acl_from_json(item) model_array.append(it) return http.response, model_array
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 calendar_list_list(self): """ Returns entries on the user's calendar list. :return: response, list<CalendarListModel> """ log_info("Send get request") http = HttpLib(url=self.url) http.auth_to_google(client=client, scope=scope_calendar) request = http.send_get() calendar_lists = [] lists = get_value_from_json( request.get_response_json(request.response), 'items') for calendar_list in lists: calendar_lists.append( CalendarListModel().pars_response_to_model(calendar_list)) log_info("Returned:\nModel calendarList list:\n{model_list}".format( model_list='\n'.join( str(item.__dict__) for item in calendar_lists))) return request.response, calendar_lists
def calendar_list_get(self, calendar_id): """ Returns an entry on the user's calendar list. :param calendar_id<str> :return: response, model<CalendarListModel> """ log_info("Send get 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) request = http.send_get() 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