🔍 Code Extractor

class OnenoteResource

Maturity: 47

Represents an image or other file resource on a OneNote page, providing access to the resource's binary content and metadata.

File:
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/onenote/resources/resource.py
Lines:
8 - 23
Complexity:
moderate

Purpose

This class models a OneNote resource (such as images, files, or other binary content) attached to a OneNote page. It extends OnenoteEntityBaseModel to provide specialized functionality for retrieving the binary content of resources and accessing their download URLs. The class is part of the Office 365 OneNote API integration and handles the interaction with the underlying API through a context-based query system.

Source Code

class OnenoteResource(OnenoteEntityBaseModel):
    """An image or other file resource on a OneNote page."""

    def get_content(self):
        # type: () -> ClientResult[AnyStr]
        """Retrieve the binary data of a file or image resource object."""
        return_type = ClientResult(self.context)
        qry = FunctionQuery(self, "content", None, return_type)
        self.context.add_query(qry)
        return return_type

    @property
    def content_url(self):
        # type: () -> Optional[str]
        """The URL for downloading the content"""
        return self.properties.get("contentUrl", None)

Parameters

Name Type Default Kind
bases OnenoteEntityBaseModel -

Parameter Details

context: Inherited from OnenoteEntityBaseModel. The execution context that manages API communication and query execution for this resource entity.

properties: Inherited from OnenoteEntityBaseModel. A dictionary containing the resource's properties including contentUrl and other metadata returned from the OneNote API.

Return Value

Instantiation returns an OnenoteResource object that represents a specific resource on a OneNote page. The get_content() method returns a ClientResult[AnyStr] object that will contain the binary data of the resource once the query is executed. The content_url property returns an Optional[str] containing the URL for downloading the content, or None if not available.

Class Interface

Methods

get_content() -> ClientResult[AnyStr]

Purpose: Retrieves the binary data of the file or image resource object by creating and queuing a function query

Returns: A ClientResult object that will contain the binary content (AnyStr) of the resource after the query is executed via context.execute_query()

content_url -> Optional[str] property

Purpose: Provides read-only access to the URL for downloading the resource content

Returns: A string containing the download URL for the resource content, or None if the URL is not available in the properties

Attributes

Name Type Description Scope
context ClientContext Inherited from base class. The execution context that manages API communication and query execution instance
properties dict Inherited from base class. Dictionary containing the resource's properties including contentUrl and other metadata instance

Dependencies

  • typing
  • office365.onenote.entity_base_model
  • office365.runtime.client_result
  • office365.runtime.queries.function

Required Imports

from typing import AnyStr, Optional
from office365.onenote.entity_base_model import OnenoteEntityBaseModel
from office365.runtime.client_result import ClientResult
from office365.runtime.queries.function import FunctionQuery

Usage Example

# Assuming you have a OneNote page object with resources
from office365.onenote.resources.onenote_resource import OnenoteResource

# Get a resource from a OneNote page (typically obtained from page.resources)
resource = page.resources[0]  # OnenoteResource instance

# Get the download URL for the resource
download_url = resource.content_url
print(f"Resource URL: {download_url}")

# Retrieve the binary content of the resource
content_result = resource.get_content()
context.execute_query()  # Execute the query to fetch the content

# Access the binary data
binary_data = content_result.value
with open('downloaded_resource.bin', 'wb') as f:
    f.write(binary_data)

Best Practices

  • Always call context.execute_query() after calling get_content() to actually fetch the binary data from the API
  • Check if content_url is not None before attempting to use it for downloads
  • The get_content() method returns a ClientResult object that acts as a promise - the actual value is populated after query execution
  • This class is typically not instantiated directly but obtained through navigation properties from OneNote page objects
  • Handle binary data appropriately based on the resource type (image, file, etc.)
  • Be aware that retrieving content may involve network calls and should be done efficiently to avoid unnecessary API requests
  • The context object must be properly authenticated and configured before using any methods

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class Onenote 69.2% similar

    The Onenote class serves as the entry point for accessing Microsoft OneNote resources through the Microsoft Graph API, providing access to notebooks, pages, sections, and other OneNote entities.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/onenote/onenote.py
  • class OnenoteEntityBaseModel 65.5% similar

    OnenoteEntityBaseModel is a base class for OneNote entities that inherits from Entity. It serves as a foundational type for all OneNote-related entity models in the Office365 SDK.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/onenote/entity_base_model.py
  • class OnenoteEntityHierarchyModel 62.9% similar

    A model class representing OneNote entities with hierarchical properties such as notebooks, sections, and pages, providing access to display name, creation/modification metadata, and identity information.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/onenote/entity_hierarchy_model.py
  • class Notebook 62.0% similar

    Represents a OneNote notebook entity with access to its sections and section groups, providing a hierarchical structure for organizing OneNote content.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/onenote/notebooks/notebook.py
  • class OnenoteEntitySchemaObjectModel 58.9% similar

    A base class for OneNote entity objects that provides schema-based property access with a focus on creation datetime tracking.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/onenote/entity_schema_object_model.py
← Back to Browse