OneDrive

Note

It’s required to install email from Optional dependencies section.

Note

For onedrive you need to specify authorization, you need to create & configure new application with the Microsoft identity platform. By the following steps:
2. (Optional) In case of using Public(Delegated) Authorization option, you have to enable Allow public client flows in Authentication section
3. Add credentials, we currently support only Client’s Secret
4. Add corresponding permissions for OneDrive, in section Permissions.
Application permissions - App runs as a background service or daemon without a signed-in user. But has usually access to all resources of which some can’t be limited.
Delegated permissions - App needs to access the API as the signed-in user. Thus can’t access resources that the user doesn’t have access to.
Onedrive permissions list - one should be sufficient.

Permission

Application

Delegated

Can be limited

Files.Read.All
Read access only

Files.ReadWrite.All
Read and write access

Sites.Read.All
Access Sharepoint - Read only

Sites.ReadWrite.All
Access Sharepoint - Read and Write

Sites.Selected
Access Sharepoint - Configurable Read / Write

User.Read
If you need to get user drive id

Warning

Important, Application permissions allow access to all drives or sites.

If limiting access is a priority we suggest only using Application permissions with Sites.Selected which enables limitations to Sharepoint drives.

Otherwise you need to use Delegated permissions for a user with access to the resources needed. But this is not suitable for business critical applications as the user token can expire and an user must approve the application.

Manager

class aiviro.modules.onedrive.OneDriveManager(auth: BaseAuthentication)

Manages OneDrive API requests.

Parameters:

auth – Authentication object to use for requests

Example:

>>> from aiviro.modules.onedrive import OneDriveManager, OneDriveScope
>>> manager = OneDriveManager.oauth_public(
...     "username",
...     "client_id",
...     "authority_url",
...     main_scope=OneDriveScope.FILES_READWRITE
... )
>>> r_folder = manager.root
>>> print(r_folder.name)
>>> for child in r_folder.children:
...     print(child.name)
classmethod oauth_public(username: str, client_id: str, authority_url: str, alias: str | None = None, main_scope: ~aiviro.modules.onedrive.common.OneDriveScope = <OneDriveScope.FILES_READWRITE: 2>, additional_scopes: ~typing.Tuple[str, ...] = ()) OneDriveManager

Creates a OneDriveManager instance using a public authentication.

Parameters:
  • username – Username to use for authentication

  • client_id – Client ID to use for authentication

  • authority_url – Authority URL to use for authentication

  • alias – Alias to use for authentication

  • main_scope – Main scope to use for authentication

  • additional_scopes – Additional scopes to use for authentication

Returns:

OneDriveManager instance

classmethod oauth_confidential(username: str, client_id: str, authority_url: str, secret: str, main_scope: ~aiviro.modules.onedrive.common.OneDriveScope = <OneDriveScope.FILES_READWRITE: 2>, additional_scopes: ~typing.Tuple[str, ...] = ()) OneDriveManager

Creates a OneDriveManager instance using a confidential authentication.

Parameters:
  • username – Username to use for authentication

  • client_id – Client ID to use for authentication

  • authority_url – Authority URL to use for authentication

  • secret – Secret to use for authentication

  • main_scope – Main scope to use for authentication

  • additional_scopes – Additional scopes to use for authentication

Returns:

OneDriveManager instance

property default_drive: BaseDrive

Default drive to use for requests.

property request: MSALRequests

Request handler to use for requests.

property root: OneDriveFolder

Returns the root folder of the default drive.

get_item_by_id(idx: str, drive: BaseDrive | None = None) OneDriveFile | OneDriveFolder

Returns the file or folder with the given ID.

Parameters:
  • idx – ID of the file or folder

  • drive – Drive to use for the request

list_by_id(idx: str, drive: BaseDrive | None = None) Iterable[OneDriveFile | OneDriveFolder]

Lists all items in file or folder with the given ID.

Parameters:
  • idx – ID of the file or folder

  • drive – Drive to use for the request

get_content_of_id(idx: str, drive: BaseDrive | None = None) bytes

Returns the content of file or folder with the given ID.

Parameters:
  • idx – ID of the file or folder

  • drive – Drive to use for the request

Returns:

Content of the file or folder

update_content_of_id(idx: str, content: BinaryIO, drive: BaseDrive | None = None) None

Updates the content of file or folder with the given ID.

Parameters:
  • idx – ID of the file or folder

  • content – New content of the file or folder

  • drive – Drive to use for the request

upload_new_content(parent_idx: str, filename: str, content: BinaryIO, drive: BaseDrive | None = None) None

Uploads a new file to the given folder.

Parameters:
  • parent_idx – ID of the parent folder

  • filename – Name of the file

  • content – Content of the file

  • drive – Drive to use for the request

move_item(item_id: str, new_parent_id: str, drive: BaseDrive | None = None) None

Moves an item to a new parent folder.

Parameters:
  • item_id – ID of the item to move

  • new_parent_id – ID of the new parent folder

  • drive – Drive to use for the request

rename_item(item_id: str, new_name: str, drive: BaseDrive | None = None) None

Renames a file or folder.

Parameters:
  • item_id – ID of the item to rename

  • new_name – New name of the item

  • drive – Drive to use for the request

create_folder(parent_id: str, folder_name: str, drive: BaseDrive | None = None) None

Creates a new folder in the given parent folder.

Parameters:
  • parent_id – ID of the parent folder

  • folder_name – Name of the new folder

  • drive – Drive to use for the request

class aiviro.modules.onedrive.OneDriveScope(value)

An enumeration.

FILES_READ = 1

Files read permission

FILES_READWRITE = 2

Files read and write permission

SITES_READ = 4

Sites read permission

SITES_READWRITE = 8

Sites read and write permission

SITES_SELECTED = 16

Sites selected permission

Files and Folders

class aiviro.modules.onedrive.manager.OneDriveFile(_manager: OneDriveManager, _model: FileItem, _drive: BaseDrive)

Class for representing file in OneDrive.

property name: str

Get name of file.

property id: str

Get id of file.

property content: bytes

Get content of file as bytes.

download(destination: str | Path = '', **kwargs: Any) None

Download file to destination.

Parameters:

destination – Destination path. If not specified, file will be downloaded to current directory.

update(new_data: bytes) None

Update content of file.

Parameters:

new_data – New content of file.

update_from_file(path: Path | str) None

Update content of file from file.

Parameters:

path – Path to file.

move(new_parent_id: str) None

Move file to new parent folder.

Parameters:

new_parent_id – Id of new parent folder.

rename(new_name: str) None

Rename file.

Parameters:

new_name – New name of file.

class aiviro.modules.onedrive.manager.OneDriveFolder(_manager: OneDriveManager, _model: FolderItem, _drive: BaseDrive)

Class for representing folder in OneDrive.

property id: str

Get id of folder.

property name: str

Get name of folder.

property children: Iterable[OneDriveFile | OneDriveFolder]

Get children of folder.

download(destination: str | Path = '', include_source_dir: bool = False, max_depth: int | None = None, *, depth: int = 0, **kwargs: Any) None

Download folder to destination.

Parameters:
  • destination – Destination path. If not specified, folder will be downloaded to current directory.

  • include_source_dir – If True, source directory will be included in destination path.

  • max_depth – Maximum depth of download. If None, all files will be downloaded.

  • depth – Current depth of download.

upload(file_name: str, new_file_content: bytes) None

Upload new file to folder.

Parameters:
  • file_name – Name of new file.

  • new_file_content – Content of new file.

upload_file(file_path: Path | str) None

Upload new file to folder from file.

Parameters:

file_path – Path to file.

move(new_parent_id: str) None

Move folder to new parent folder.

Parameters:

new_parent_id – Id of new parent folder.

rename(new_name: str) None

Rename folder.

Parameters:

new_name – New name of folder.

mkdir(folder_name: str) None

Create new folder in folder.

Parameters:

folder_name – Name of new folder.