Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion office365/onedrive/driveitems/driveItem.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ def get_content(self, format_name: str | None = None) -> ClientResult[AnyStr]:
action_name = "content"
if format_name is not None:
action_name = action_name + rf"?format={format_name}"
qry = FunctionQuery(self, action_name, None, return_type)
qry = FunctionQuery(self, action_name, None, return_type, return_raw_content=True)
self.context.add_query(qry)
return return_type

Expand Down
9 changes: 8 additions & 1 deletion office365/runtime/odata/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ def process_response(self, response: Response, query: ClientQuery) -> None:
if isinstance(return_type, ClientObject):
return_type.clear_state()

if response.headers.get("Content-Type", "").lower().split(";")[0] != "application/json":
content_type = response.headers.get("Content-Type", "").lower().split(";")[0]

if content_type != "application/json" or self._is_raw_content_query(query):
if isinstance(return_type, ClientResult):
return_type.set_property("__value", response.content)
else:
Expand All @@ -87,6 +89,11 @@ def process_response(self, response: Response, query: ClientQuery) -> None:

self.map_json(response.json(), return_type, json_format)

@staticmethod
def _is_raw_content_query(query: ClientQuery) -> bool:
"""Check if the query represents a raw content retrieval (e.g. file download)."""
return isinstance(query, FunctionQuery) and query.return_raw_content

def map_json(
self,
json: Any,
Expand Down
9 changes: 9 additions & 0 deletions office365/runtime/queries/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def __init__(
method_name: str | None = None,
method_params: list | dict | ClientValue | None = None,
return_type: ReturnT | None = None,
return_raw_content: bool = False,
) -> None:
"""Initialize a function query.

Expand All @@ -23,9 +24,17 @@ def __init__(
method_name: The name of the method to call
method_params: Parameters for the method call
return_type: The expected return type
return_raw_content: When True, the response body is treated as raw content
(e.g. file download) rather than parsed as OData JSON.
"""
super().__init__(binding_type.context, binding_type, None, None, return_type)
self._path = ServiceOperationPath(method_name or "", method_params, binding_type.resource_path)
self._return_raw_content = return_raw_content

@property
def return_raw_content(self) -> bool:
"""Whether the response should be treated as raw content, not OData JSON."""
return self._return_raw_content

def __repr__(self) -> str:
return f"FunctionQuery(name={self.path.name})"
Expand Down
2 changes: 1 addition & 1 deletion office365/sharepoint/files/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def _file_loaded():
def get_content(self) -> ClientResult[bytes]:
"""Downloads a file content"""
return_type = ClientResult(self.context, bytes())
qry = FunctionQuery(self, "$value", return_type=return_type)
qry = FunctionQuery(self, "$value", return_type=return_type, return_raw_content=True)
self.context.add_query(qry)
return return_type

Expand Down
Loading