Skip to content

Commit

Permalink
Allow content_handler to return a content artifact
Browse files Browse the repository at this point in the history
fixes #4635
  • Loading branch information
daviddavis authored and dkliban committed Nov 2, 2023
1 parent 60b07a9 commit c72929e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGES/plugin_api/4635.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Added content app feature to accept a ``ContentArtifact`` from ``content_handler`` so that the
plugin writer no longer has to generate a response when dealing with ``ContentArtifacts``.
5 changes: 4 additions & 1 deletion docs/plugin_dev/api-reference/content-app.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ Making a custom Handler is a two-step process:
If content needs to be served from within the :term:`Distribution`'s base_path,
overriding the :meth:`~pulpcore.plugin.models.Distribution.content_handler` and
:meth:`~pulpcore.plugin.models.Distribution.content_handler_directory_listing`
methods in your Distribution is an easier way to serve this content.
methods in your Distribution is an easier way to serve this content. The
:meth:`~pulpcore.plugin.models.Distribution.content_handler` method should
return an instance of `aiohttp.web_response.Response` or a
`pulpcore.plugin.models.ContentArtifact`.

Creating your Handler
---------------------
Expand Down
17 changes: 14 additions & 3 deletions pulpcore/content/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,11 +573,22 @@ async def _match_and_stream(self, path, request):
if not ends_in_slash:
rel_path = f"{rel_path}/"

headers = self.response_headers(original_rel_path, distro)

content_handler_result = await sync_to_async(distro.content_handler)(original_rel_path)
if content_handler_result is not None:
return content_handler_result

headers = self.response_headers(original_rel_path, distro)
if isinstance(content_handler_result, ContentArtifact):
if content_handler_result.artifact:
return await self._serve_content_artifact(
content_handler_result, headers, request
)
else:
return await self._stream_content_artifact(
request, StreamResponse(headers=headers), content_handler_result
)
else:
# the result is a response so just return it
return content_handler_result

repository = distro.repository
publication = distro.publication
Expand Down

0 comments on commit c72929e

Please sign in to comment.