Пример #1
0
class GmailRestHandler(object):
    """Class abstracts working with the Gmail Api Client"""

    # The class that is used to load configuration
    config_class = Config

    root_path = os.getcwd()

    def __init__(self, config_file):
        self.config = self.config_class(self.root_path)
        self.config.from_pyfile(config_file)

        client_secret_file = self.config.get("CLIENT_SECRET_FILE")
        if not client_secret_file:
            raise MissingConfigVariableError

        scopes = self.config.get("SCOPES")
        if not scopes:
            raise MissingConfigVariableError

        application_name = self.config.get("APPLICATION_NAME")
        if not application_name:
            raise MissingConfigVariableError

        self.authentication = Authentication(client_secret_file, application_name, scopes)
        self.service = self._build_service()

    def _build_service(self):
        """Build Gmail Api Client discovery service"""
        # Build credentials
        credentials = self.authentication.get_credentials()
        http = credentials.authorize(httplib2.Http())
        # Build service
        service = discovery.build("gmail", "v1", http=http)

        return service

    def get_message(self, user_id, msg_id):
        """Get a Message with given ID.

        Args:
        service: Authorized Gmail API service instance.
        user_id: User's email address. The special value "me"
        can be used to indicate the authenticated user.
        msg_id: The ID of the Message required.

        Returns:
        A Message.
        """
        try:
            message = self.service.users().messages().get(userId=user_id, id=msg_id).execute()

        except errors.HttpError, error:
            raise GmailApiHTTPError
        else:
Пример #2
0
    def __init__(self, config_file):
        self.config = self.config_class(self.root_path)
        self.config.from_pyfile(config_file)

        client_secret_file = self.config.get("CLIENT_SECRET_FILE")
        if not client_secret_file:
            raise MissingConfigVariableError

        scopes = self.config.get("SCOPES")
        if not scopes:
            raise MissingConfigVariableError

        application_name = self.config.get("APPLICATION_NAME")
        if not application_name:
            raise MissingConfigVariableError

        self.authentication = Authentication(client_secret_file, application_name, scopes)
        self.service = self._build_service()