|
| 1 | +from O365.connection import Connection, Protocol, MSGraphProtocol, oauth_authentication_flow |
| 2 | +from O365.drive import Storage |
| 3 | +from O365.utils import ME_RESOURCE |
| 4 | +from O365.message import Message |
| 5 | +from O365.mailbox import MailBox |
| 6 | +from O365.address_book import AddressBook, GlobalAddressList |
| 7 | +from O365.calendar import Schedule |
| 8 | + |
| 9 | + |
| 10 | +class Account(object): |
| 11 | + """ Class helper to integrate all components into a single object """ |
| 12 | + |
| 13 | + def __init__(self, credentials, *, protocol=None, main_resource=ME_RESOURCE, **kwargs): |
| 14 | + """ |
| 15 | + Account constructor. |
| 16 | + :param credentials: a tuple containing the client_id and client_secret |
| 17 | + :param protocol: the protocol to be used in this account instance |
| 18 | + :param main_resource: the resource to be used by this account |
| 19 | + :param kwargs: any extra args to be passed to the Connection instance |
| 20 | + """ |
| 21 | + |
| 22 | + protocol = protocol or MSGraphProtocol # defaults to Graph protocol |
| 23 | + self.protocol = protocol(default_resource=main_resource, **kwargs) if isinstance(protocol, type) else protocol |
| 24 | + |
| 25 | + if not isinstance(self.protocol, Protocol): |
| 26 | + raise ValueError("'protocol' must be a subclass of Protocol") |
| 27 | + |
| 28 | + self.con = Connection(credentials, **kwargs) |
| 29 | + self.main_resource = main_resource |
| 30 | + |
| 31 | + def __repr__(self): |
| 32 | + if self.con.auth: |
| 33 | + return 'Account Client Id: {}'.format(self.con.auth[0]) |
| 34 | + else: |
| 35 | + return 'Unidentified Account' |
| 36 | + |
| 37 | + def authenticate(self, *, scopes, **kwargs): |
| 38 | + """ |
| 39 | + Performs the oauth authentication flow resulting in a stored token. |
| 40 | + It uses the credentials passed on instantiation |
| 41 | + :param scopes: a list of protocol user scopes to be converted by the protocol |
| 42 | + :param kwargs: other configuration to be passed to the Connection instance |
| 43 | + """ |
| 44 | + kwargs.setdefault('token_file_name', self.con.token_path.name) |
| 45 | + |
| 46 | + return oauth_authentication_flow(*self.con.auth, scopes=scopes, protocol=self.protocol, **kwargs) |
| 47 | + |
| 48 | + @property |
| 49 | + def connection(self): |
| 50 | + """ Alias for self.con """ |
| 51 | + return self.con |
| 52 | + |
| 53 | + def new_message(self, resource=None): |
| 54 | + """ |
| 55 | + Creates a new message to be send or stored |
| 56 | + :param resource: Custom resource to be used in this message. Defaults to parent main_resource. |
| 57 | + """ |
| 58 | + return Message(parent=self, main_resource=resource, is_draft=True) |
| 59 | + |
| 60 | + def mailbox(self, resource=None): |
| 61 | + """ |
| 62 | + Creates MailBox Folder instance |
| 63 | + :param resource: Custom resource to be used in this mailbox. Defaults to parent main_resource. |
| 64 | + """ |
| 65 | + return MailBox(parent=self, main_resource=resource, name='MailBox') |
| 66 | + |
| 67 | + def address_book(self, *, resource=None, address_book='personal'): |
| 68 | + """ |
| 69 | + Creates Address Book instance |
| 70 | + :param resource: Custom resource to be used in this address book. Defaults to parent main_resource. |
| 71 | + :param address_book: Choose from Personal or Gal (Global Address List) |
| 72 | + """ |
| 73 | + if address_book == 'personal': |
| 74 | + return AddressBook(parent=self, main_resource=resource, name='Personal Address Book') |
| 75 | + elif address_book == 'gal': |
| 76 | + return GlobalAddressList(parent=self) |
| 77 | + else: |
| 78 | + raise RuntimeError('Addres_book must be either "personal" (resource address book) or "gal" (Global Address List)') |
| 79 | + |
| 80 | + def schedule(self, *, resource=None): |
| 81 | + """ |
| 82 | + Creates Schedule instance to handle calendars |
| 83 | + :param resource: Custom resource to be used in this schedule object. Defaults to parent main_resource. |
| 84 | + """ |
| 85 | + return Schedule(parent=self, main_resource=resource) |
| 86 | + |
| 87 | + def storage(self, *, resource=None): |
| 88 | + """ |
| 89 | + Creates a Storage instance to handle file storage like OneDrive or Sharepoint document libraries |
| 90 | + :param resource: Custom resource to be used in this drive object. Defaults to parent main_resource. |
| 91 | + """ |
| 92 | + if not isinstance(self.protocol, MSGraphProtocol): |
| 93 | + # TODO: a custom protocol accessing OneDrive or Sharepoint Api will fail here. |
| 94 | + raise RuntimeError('Drive options only works on Microsoft Graph API') |
| 95 | + |
| 96 | + return Storage(parent=self, main_resource=resource) |
0 commit comments