A package to interact with the Microsoft Graph API for personal OneDrives.
Project description
OneDrive Personal SDK
OneDrive Personal SDK is a Python library for interacting with a personal OneDrive through the Graph API.
Usage
Getting an authentication token
The library is built to support different token providers. To define a token provider you need to inherit from TokenProvider and implement the async_get_access_token method. The method should return a valid token string.
To use msal as a token provider you can create a class like the following:
from msal import PublicClientApplication
from onedrive_personal_sdk import TokenProvider
class MsalTokenProvider(PublicClientApplication, TokenProvider):
def __init__(self, client_id: str, authority: str, scopes: list[str]):
super().__init__(client_id, authority=authority)
self._scopes = scopes
async def async_get_access_token(self) -> str:
result = self.acquire_token_interactive(scopes=self._scopes)
return result["access_token"]
Creating a client
To create a client you need to provide a token provider.
from onedrive_personal_sdk import OneDriveClient
client = OneDriveClient(auth_provider)
# can also be created with a custom aiohttp session
client = OneDriveClient(auth_provider, session=session)
Calling the API
The client provides methods to interact with the OneDrive API. The methods are async and return the response from the API. Most methods accept item ids or paths as arguments.
root = await client.get_drive_item("root") # Get the root folder
item = await client.get_drive_item("root:/path/to/item:") # Get an item by path
item = await client.get_drive_item(item.id) # Get an item by id
folder = await client.get_drive_item("root:/path/to/folder:") # Get a folder
# List children of a folder
children = await client.list_children("root:/path/to/folder:")
children = await client.list_children(folder.id)
Uploading files
Smaller files (<250MB) can be uploaded with the upload method. All files need to be provided as AsyncIterators. Such a generator can be created from aiofiles.
import aiofiles
from collections.abc import AsyncIterator
async def file_reader(file_path: str) -> AsyncIterator[bytes]:
async with aiofiles.open(file_path, "rb") as f:
while True:
chunk = await f.read(1024) # Read in chunks of 1024 bytes
if not chunk:
break
yield chunk
await client.upload_file("root:/TestFolder", "test.txt", file_reader("test.txt"))
Larger files (and small files as well) can be uploaded with a LargeFileUploadClient. The client will handle the upload in chunks.
import os
from onedrive_personal_sdk import LargeFileUploadClient
from onedrive_personal_sdk.models.upload FileInfo
filename = "testfile.txt"
size = os.path.getsize(filename)
file = FileInfo(
name=filename,
size=size,
folder_path_id="root",
content_stream=file_reader(filename),
)
file = await LargeFileUploadClient.upload(auth_provider, file)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file onedrive_personal_sdk-0.0.1a5.tar.gz.
File metadata
- Download URL: onedrive_personal_sdk-0.0.1a5.tar.gz
- Upload date:
- Size: 11.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e6da8ce4fb3933bce088734535a6494a67acd8b84314c1108ac324943d7da121
|
|
| MD5 |
f973eb2d0cb30988985d71d53c7b991c
|
|
| BLAKE2b-256 |
80bfdfa898025a77dc5a1115932705d4d5d1250b36f59b0c62be53459654d441
|
Provenance
The following attestation bundles were made for onedrive_personal_sdk-0.0.1a5.tar.gz:
Publisher:
pypi.yaml on zweckj/onedrive-personal-sdk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
onedrive_personal_sdk-0.0.1a5.tar.gz -
Subject digest:
e6da8ce4fb3933bce088734535a6494a67acd8b84314c1108ac324943d7da121 - Sigstore transparency entry: 167848881
- Sigstore integration time:
-
Permalink:
zweckj/onedrive-personal-sdk@eca4803bcaea50e791cb8f85fbf60c1e5b58bb07 -
Branch / Tag:
refs/tags/v0.0.1-alpha.5 - Owner: https://github.com/zweckj
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi.yaml@eca4803bcaea50e791cb8f85fbf60c1e5b58bb07 -
Trigger Event:
release
-
Statement type:
File details
Details for the file onedrive_personal_sdk-0.0.1a5-py3-none-any.whl.
File metadata
- Download URL: onedrive_personal_sdk-0.0.1a5-py3-none-any.whl
- Upload date:
- Size: 13.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7316ab3780117ffe81f5a6024732adacda2bdcc05237ff553308e7e8f564da43
|
|
| MD5 |
f457e2b573608afe3817aa3234ad5fed
|
|
| BLAKE2b-256 |
978265a7a2085b7ef24a768351fe9bada47896b0a2741c8dae1f8a0f1271715b
|
Provenance
The following attestation bundles were made for onedrive_personal_sdk-0.0.1a5-py3-none-any.whl:
Publisher:
pypi.yaml on zweckj/onedrive-personal-sdk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
onedrive_personal_sdk-0.0.1a5-py3-none-any.whl -
Subject digest:
7316ab3780117ffe81f5a6024732adacda2bdcc05237ff553308e7e8f564da43 - Sigstore transparency entry: 167848882
- Sigstore integration time:
-
Permalink:
zweckj/onedrive-personal-sdk@eca4803bcaea50e791cb8f85fbf60c1e5b58bb07 -
Branch / Tag:
refs/tags/v0.0.1-alpha.5 - Owner: https://github.com/zweckj
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi.yaml@eca4803bcaea50e791cb8f85fbf60c1e5b58bb07 -
Trigger Event:
release
-
Statement type: