onedrive-personal-sdk


Nameonedrive-personal-sdk JSON
Version 0.0.8 PyPI version JSON
download
home_pageNone
SummaryA package to interact with the Microsoft Graph API for personal OneDrives.
upload_time2025-02-05 11:49:43
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseMIT
keywords onedrive graph api async client
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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:

```python
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.

```python
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.

```python


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`.

```python
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
```

```python
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.

```python
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)
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "onedrive-personal-sdk",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": "Josef Zweck <josef@zweck.dev>",
    "keywords": "OneDrive, Graph, api, async, client",
    "author": null,
    "author_email": "Josef Zweck <josef@zweck.dev>",
    "download_url": "https://files.pythonhosted.org/packages/1b/88/2c5abdca5f84c32c5f094a383aeac0cac518d92e06246f8bbaaab50acbb9/onedrive_personal_sdk-0.0.8.tar.gz",
    "platform": null,
    "description": "# OneDrive Personal SDK\n\nOneDrive Personal SDK is a Python library for interacting with a personal OneDrive through the Graph API.\n\n# Usage\n\n## Getting an authentication token\n\nThe 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.\n\nTo use `msal` as a token provider you can create a class like the following:\n\n```python\nfrom msal import PublicClientApplication\nfrom onedrive_personal_sdk import TokenProvider\n\nclass MsalTokenProvider(PublicClientApplication, TokenProvider):\n    def __init__(self, client_id: str, authority: str, scopes: list[str]):\n        super().__init__(client_id, authority=authority)\n        self._scopes = scopes\n\n    async def async_get_access_token(self) -> str:\n        result = self.acquire_token_interactive(scopes=self._scopes)\n        return result[\"access_token\"]\n```\n\n## Creating a client\n\nTo create a client you need to provide a token provider.\n\n```python\nfrom onedrive_personal_sdk import OneDriveClient\n\nclient = OneDriveClient(auth_provider)\n\n# can also be created with a custom aiohttp session\nclient = OneDriveClient(auth_provider, session=session)\n```\n\n# Calling the API\n\nThe 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.\n\n```python\n\n\nroot = await client.get_drive_item(\"root\") # Get the root folder\nitem = await client.get_drive_item(\"root:/path/to/item:\") # Get an item by path\nitem = await client.get_drive_item(item.id) # Get an item by id\n\nfolder = await client.get_drive_item(\"root:/path/to/folder:\") # Get a folder\n\n# List children of a folder\nchildren = await client.list_children(\"root:/path/to/folder:\")\nchildren = await client.list_children(folder.id)\n\n```\n\n# Uploading files\n\nSmaller 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`.\n\n```python\nimport aiofiles\nfrom collections.abc import AsyncIterator\n\nasync def file_reader(file_path: str) -> AsyncIterator[bytes]:\n    async with aiofiles.open(file_path, \"rb\") as f:\n        while True:\n            chunk = await f.read(1024)  # Read in chunks of 1024 bytes\n            if not chunk:\n                break\n            yield chunk\n```\n\n```python\nawait client.upload_file(\"root:/TestFolder\", \"test.txt\", file_reader(\"test.txt\"))\n```\n\nLarger files (and small files as well) can be uploaded with a `LargeFileUploadClient`. The client will handle the upload in chunks.\n\n```python\nimport os\nfrom onedrive_personal_sdk import LargeFileUploadClient\nfrom onedrive_personal_sdk.models.upload FileInfo\n\nfilename = \"testfile.txt\"\nsize = os.path.getsize(filename)\n\nfile = FileInfo(\n    name=filename,\n    size=size,\n    folder_path_id=\"root\",\n    content_stream=file_reader(filename),\n)\nfile = await LargeFileUploadClient.upload(auth_provider, file)\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A package to interact with the Microsoft Graph API for personal OneDrives.",
    "version": "0.0.8",
    "project_urls": {
        "Documentation": "https://github.com/zweckj/onedrive-personal-sdk",
        "Homepage": "https://github.com/zweckj/onedrive-personal-sdk",
        "Repository": "https://github.com/zweckj/onedrive-personal-sdk"
    },
    "split_keywords": [
        "onedrive",
        " graph",
        " api",
        " async",
        " client"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "31a6cdf2e29eb5e6c737ed799f456591f8e783fa3e2336b03b10b09a9712f531",
                "md5": "db4d62669e219ab5e496562091768aea",
                "sha256": "fb59fa4e93556f8e1199f931f0debab2a190806bc5a433355f538766425f8ac5"
            },
            "downloads": -1,
            "filename": "onedrive_personal_sdk-0.0.8-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "db4d62669e219ab5e496562091768aea",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 13666,
            "upload_time": "2025-02-05T11:49:40",
            "upload_time_iso_8601": "2025-02-05T11:49:40.909836Z",
            "url": "https://files.pythonhosted.org/packages/31/a6/cdf2e29eb5e6c737ed799f456591f8e783fa3e2336b03b10b09a9712f531/onedrive_personal_sdk-0.0.8-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "1b882c5abdca5f84c32c5f094a383aeac0cac518d92e06246f8bbaaab50acbb9",
                "md5": "d10a6d48666c6f41f7e6614fe22ab948",
                "sha256": "91301ab4acd788b12cb183d70fb992dd1be7cd3c19011b8b982254d7b6544855"
            },
            "downloads": -1,
            "filename": "onedrive_personal_sdk-0.0.8.tar.gz",
            "has_sig": false,
            "md5_digest": "d10a6d48666c6f41f7e6614fe22ab948",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 11902,
            "upload_time": "2025-02-05T11:49:43",
            "upload_time_iso_8601": "2025-02-05T11:49:43.546754Z",
            "url": "https://files.pythonhosted.org/packages/1b/88/2c5abdca5f84c32c5f094a383aeac0cac518d92e06246f8bbaaab50acbb9/onedrive_personal_sdk-0.0.8.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-02-05 11:49:43",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "zweckj",
    "github_project": "onedrive-personal-sdk",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "onedrive-personal-sdk"
}
        
Elapsed time: 0.41660s