1
- from O365 .connection import Connection , Protocol , MSGraphProtocol , oauth_authentication_flow
1
+ from O365 .address_book import AddressBook , GlobalAddressList
2
+ from O365 .calendar import Schedule
3
+ from O365 .connection import Connection , Protocol , MSGraphProtocol
4
+ from O365 .connection import oauth_authentication_flow
2
5
from O365 .drive import Storage
6
+ from O365 .mailbox import MailBox
7
+ from O365 .message import Message
3
8
from O365 .utils import ME_RESOURCE
4
9
from O365 .message import Message
5
10
from O365 .mailbox import MailBox
6
11
from O365 .address_book import AddressBook , GlobalAddressList
7
12
from O365 .calendar import Schedule
13
+ from O365 .sharepoint import Sharepoint
8
14
9
15
10
16
class Account (object ):
11
- """ Class helper to integrate all components into a single object """
12
17
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
18
+ def __init__ (self , credentials , * , protocol = None , main_resource = ME_RESOURCE ,
19
+ ** kwargs ):
20
+ """ Creates an object which is used to access resources related to the
21
+ specified credentials
22
+
23
+ :param tuple credentials: a tuple containing the client_id
24
+ and client_secret
25
+ :param Protocol protocol: the protocol to be used in this account
26
+ :param str main_resource: the resource to be used by this account
27
+ ('me' or 'users')
19
28
:param kwargs: any extra args to be passed to the Connection instance
29
+ :raises ValueError: if an invalid protocol is passed
20
30
"""
21
31
22
- protocol = protocol or MSGraphProtocol # defaults to Graph protocol
23
- self .protocol = protocol (default_resource = main_resource , ** kwargs ) if isinstance (protocol , type ) else protocol
32
+ protocol = protocol or MSGraphProtocol # Defaults to Graph protocol
33
+ self .protocol = protocol (default_resource = main_resource ,
34
+ ** kwargs ) if isinstance (protocol ,
35
+ type ) else protocol
24
36
25
37
if not isinstance (self .protocol , Protocol ):
26
38
raise ValueError ("'protocol' must be a subclass of Protocol" )
@@ -35,62 +47,107 @@ def __repr__(self):
35
47
return 'Unidentified Account'
36
48
37
49
def authenticate (self , * , scopes , ** kwargs ):
38
- """
39
- Performs the oauth authentication flow resulting in a stored token.
50
+ """ Performs the oauth authentication flow resulting in a stored token
40
51
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
52
+
53
+ :param list[str] scopes: list of protocol user scopes to be converted
54
+ by the protocol
55
+ :param kwargs: other configurations to be passed to the
56
+ Connection instance
57
+ :return: Success / Failure
58
+ :rtype: bool
43
59
"""
44
60
kwargs .setdefault ('token_file_name' , self .con .token_path .name )
45
61
46
- return oauth_authentication_flow (* self .con .auth , scopes = scopes , protocol = self .protocol , ** kwargs )
62
+ return oauth_authentication_flow (* self .con .auth , scopes = scopes ,
63
+ protocol = self .protocol , ** kwargs )
47
64
48
65
@property
49
66
def connection (self ):
50
- """ Alias for self.con """
67
+ """ Alias for self.con
68
+
69
+ :rtype: Connection
70
+ """
51
71
return self .con
52
72
53
73
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.
74
+ """ Creates a new message to be sent or stored
75
+
76
+ :param str resource: Custom resource to be used in this message
77
+ (Defaults to parent main_resource)
78
+ :return: New empty message
79
+ :rtype: Message
57
80
"""
58
81
return Message (parent = self , main_resource = resource , is_draft = True )
59
82
60
83
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.
84
+ """ Get an instance to the mailbox for the specified account resource
85
+
86
+ :param str resource: Custom resource to be used in this mailbox
87
+ (Defaults to parent main_resource)
88
+ :return: a representation of account mailbox
89
+ :rtype: MailBox
64
90
"""
65
91
return MailBox (parent = self , main_resource = resource , name = 'MailBox' )
66
92
67
93
def address_book (self , * , resource = None , address_book = 'personal' ):
94
+ """ Get an instance to the specified address book for the
95
+ specified account resource
96
+
97
+ :param str resource: Custom resource to be used in this address book
98
+ (Defaults to parent main_resource)
99
+ :param str address_book: Choose from 'Personal' or
100
+ 'GAL' (Global Address List)
101
+ :return: a representation of the specified address book
102
+ :rtype: AddressBook or GlobalAddressList
103
+ :raises RuntimeError: if invalid address_book is specified
68
104
"""
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' :
105
+ if address_book .lower () == 'personal' :
106
+ return AddressBook (parent = self , main_resource = resource ,
107
+ name = 'Personal Address Book' )
108
+ elif address_book .lower () == 'gal' :
76
109
return GlobalAddressList (parent = self )
77
110
else :
78
- raise RuntimeError ('Addres_book must be either "personal" (resource address book) or "gal" (Global Address List)' )
111
+ raise RuntimeError (
112
+ 'address_book must be either "personal" '
113
+ '(resource address book) or "gal" (Global Address List)' )
79
114
80
115
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.
116
+ """ Get an instance to work with calendar events for the
117
+ specified account resource
118
+
119
+ :param str resource: Custom resource to be used in this schedule object
120
+ (Defaults to parent main_resource)
121
+ :return: a representation of calendar events
122
+ :rtype: Schedule
84
123
"""
85
124
return Schedule (parent = self , main_resource = resource )
86
125
87
126
def storage (self , * , resource = None ):
127
+ """ Get an instance to handle file storage like OneDrive or
128
+ Sharepoint document libraries for the specified account resource
129
+
130
+ :param str resource: Custom resource to be used in this drive object
131
+ (Defaults to parent main_resource)
132
+ :return: a representation of File Storage
133
+ :rtype: Storage
134
+ :raises RuntimeError: if protocol doesn't support the feature
135
+ """
136
+ if not isinstance (self .protocol , MSGraphProtocol ):
137
+ # TODO: Custom protocol accessing OneDrive/Sharepoint Api fails here
138
+ raise RuntimeError (
139
+ 'Drive options only works on Microsoft Graph API' )
140
+
141
+ return Storage (parent = self , main_resource = resource )
142
+
143
+ def sharepoint (self , * , resource = '' ):
88
144
"""
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 .
145
+ Creates a new Sharepoint instance
146
+ :param resource: Custom resource to be used in this sharepoint object. Defaults to blank .
91
147
"""
148
+
92
149
if not isinstance (self .protocol , MSGraphProtocol ):
93
150
# TODO: a custom protocol accessing OneDrive or Sharepoint Api will fail here.
94
- raise RuntimeError ('Drive options only works on Microsoft Graph API' )
151
+ raise RuntimeError ('Sharepoint api only works on Microsoft Graph API' )
95
152
96
- return Storage (parent = self , main_resource = resource )
153
+ return Sharepoint (parent = self , main_resource = resource )
0 commit comments