Skip to content

Commit

Permalink
Fix payload for put-file endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
dormant-user committed Oct 27, 2024
1 parent 9e14235 commit dcaa99c
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 78 deletions.
5 changes: 0 additions & 5 deletions doc_gen/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,6 @@ Payloads
.. autoclass:: pyninja.modules.payloads.GetFile(BaseModel)
:exclude-members: _abc_impl, model_config, model_fields, model_computed_fields

====

.. autoclass:: pyninja.modules.payloads.PutFile(BaseModel)
:exclude-members: _abc_impl, model_config, model_fields, model_computed_fields

RateLimit
---------
.. automodule:: pyninja.modules.rate_limit
Expand Down
5 changes: 0 additions & 5 deletions docs/_sources/index.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,6 @@ Payloads
.. autoclass:: pyninja.modules.payloads.GetFile(BaseModel)
:exclude-members: _abc_impl, model_config, model_fields, model_computed_fields

====

.. autoclass:: pyninja.modules.payloads.PutFile(BaseModel)
:exclude-members: _abc_impl, model_config, model_fields, model_computed_fields

RateLimit
---------
.. automodule:: pyninja.modules.rate_limit
Expand Down
17 changes: 0 additions & 17 deletions docs/genindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ <h1 id="index">Index</h1>
| <a href="#L"><strong>L</strong></a>
| <a href="#M"><strong>M</strong></a>
| <a href="#N"><strong>N</strong></a>
| <a href="#O"><strong>O</strong></a>
| <a href="#P"><strong>P</strong></a>
| <a href="#R"><strong>R</strong></a>
| <a href="#S"><strong>S</strong></a>
Expand Down Expand Up @@ -186,11 +185,7 @@ <h2 id="D">D</h2>
<li><a href="index.html#pyninja.modules.models.ServiceStatus.description">description (pyninja.modules.models.ServiceStatus attribute)</a>
</li>
<li><a href="index.html#pyninja.modules.payloads.ListFiles.directory">directory (pyninja.modules.payloads.ListFiles attribute)</a>

<ul>
<li><a href="index.html#pyninja.modules.payloads.PutFile.directory">(pyninja.modules.payloads.PutFile attribute)</a>
</li>
</ul></li>
<li><a href="index.html#pyninja.modules.models.EnvConfig.disk_lib">disk_lib (pyninja.modules.models.EnvConfig attribute)</a>
</li>
<li><a href="index.html#pyninja.modules.models.DiskLib">DiskLib (class in pyninja.modules.models)</a>
Expand Down Expand Up @@ -228,8 +223,6 @@ <h2 id="F">F</h2>
<table style="width: 100%" class="indextable genindextable"><tr>
<td style="width: 33%; vertical-align: top;"><ul>
<li><a href="index.html#pyninja.monitor.authenticator.failed_auth_counter">failed_auth_counter() (in module pyninja.monitor.authenticator)</a>
</li>
<li><a href="index.html#pyninja.modules.payloads.PutFile.file">file (pyninja.modules.payloads.PutFile attribute)</a>
</li>
<li><a href="index.html#pyninja.modules.payloads.GetFile.filepath">filepath (pyninja.modules.payloads.GetFile attribute)</a>
</li>
Expand Down Expand Up @@ -545,14 +538,6 @@ <h2 id="N">N</h2>
</ul></td>
</tr></table>

<h2 id="O">O</h2>
<table style="width: 100%" class="indextable genindextable"><tr>
<td style="width: 33%; vertical-align: top;"><ul>
<li><a href="index.html#pyninja.modules.payloads.PutFile.overwrite">overwrite (pyninja.modules.payloads.PutFile attribute)</a>
</li>
</ul></td>
</tr></table>

<h2 id="P">P</h2>
<table style="width: 100%" class="indextable genindextable"><tr>
<td style="width: 33%; vertical-align: top;"><ul>
Expand All @@ -577,8 +562,6 @@ <h2 id="P">P</h2>
<li><a href="index.html#pyninja.routes.fullaccess.put_file">put_file() (in module pyninja.routes.fullaccess)</a>
</li>
<li><a href="index.html#pyninja.executors.database.put_record">put_record() (in module pyninja.executors.database)</a>
</li>
<li><a href="index.html#pyninja.modules.payloads.PutFile">PutFile (class in pyninja.modules.payloads)</a>
</li>
<li>
pyninja.executors.auth
Expand Down
28 changes: 2 additions & 26 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,13 @@ <h1>PyNinja - Executors<a class="headerlink" href="#pyninja-executors" title="Pe

<dl class="py function">
<dt class="sig sig-object py" id="pyninja.routes.fullaccess.put_file">
<em class="property"><span class="k"><span class="pre">async</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">pyninja.routes.fullaccess.</span></span><span class="sig-name descname"><span class="pre">put_file</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">request</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">Request</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">payload</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference internal" href="#pyninja.modules.payloads.PutFile" title="pyninja.modules.payloads.PutFile"><span class="pre">PutFile</span></a></span></em>, <em class="sig-param"><span class="n"><span class="pre">apikey</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">HTTPAuthorizationCredentials</span></span><span class="w"> </span><span class="o"><span class="pre">=</span></span><span class="w"> </span><span class="default_value"><span class="pre">Depends(HTTPBearer)</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">token</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">Optional</span><span class="p"><span class="pre">[</span></span><span class="pre">str</span><span class="p"><span class="pre">]</span></span></span><span class="w"> </span><span class="o"><span class="pre">=</span></span><span class="w"> </span><span class="default_value"><span class="pre">Header(None)</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pyninja.routes.fullaccess.put_file" title="Permalink to this definition"></a></dt>
<em class="property"><span class="k"><span class="pre">async</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">pyninja.routes.fullaccess.</span></span><span class="sig-name descname"><span class="pre">put_file</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">request</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">Request</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">file</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">UploadFile</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">directory</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">Path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">overwrite</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">bool</span></span><span class="w"> </span><span class="o"><span class="pre">=</span></span><span class="w"> </span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">apikey</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">HTTPAuthorizationCredentials</span></span><span class="w"> </span><span class="o"><span class="pre">=</span></span><span class="w"> </span><span class="default_value"><span class="pre">Depends(HTTPBearer)</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">token</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">Optional</span><span class="p"><span class="pre">[</span></span><span class="pre">str</span><span class="p"><span class="pre">]</span></span></span><span class="w"> </span><span class="o"><span class="pre">=</span></span><span class="w"> </span><span class="default_value"><span class="pre">Header(None)</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pyninja.routes.fullaccess.put_file" title="Permalink to this definition"></a></dt>
<dd><p><strong>Upload a file to th.</strong></p>
<p><strong>Args:</strong></p>
<blockquote>
<div><ul class="simple">
<li><p>request: Reference to the FastAPI request object.</p></li>
<li><p>file: Upload object for the file param.</p></li>
<li><p>payload: Payload received as request body.</p></li>
<li><p>apikey: API Key to authenticate the request.</p></li>
<li><p>token: API secret to authenticate the request.</p></li>
Expand Down Expand Up @@ -2051,31 +2052,6 @@ <h2>Payloads<a class="headerlink" href="#payloads" title="Permalink to this head

</dd></dl>

<hr class="docutils" />
<dl class="py class">
<dt class="sig sig-object py" id="pyninja.modules.payloads.PutFile">
<em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">pyninja.modules.payloads.</span></span><span class="sig-name descname"><span class="pre">PutFile</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">BaseModel</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#pyninja.modules.payloads.PutFile" title="Permalink to this definition"></a></dt>
<dd><p>Payload for put-file endpoint.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">PutFile</span>
</pre></div>
</div>
<dl class="py attribute">
<dt class="sig sig-object py" id="pyninja.modules.payloads.PutFile.file">
<span class="sig-name descname"><span class="pre">file</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="pre">UploadFile</span></em><a class="headerlink" href="#pyninja.modules.payloads.PutFile.file" title="Permalink to this definition"></a></dt>
<dd></dd></dl>

<dl class="py attribute">
<dt class="sig sig-object py" id="pyninja.modules.payloads.PutFile.directory">
<span class="sig-name descname"><span class="pre">directory</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="pre">Path</span></em><a class="headerlink" href="#pyninja.modules.payloads.PutFile.directory" title="Permalink to this definition"></a></dt>
<dd></dd></dl>

<dl class="py attribute">
<dt class="sig sig-object py" id="pyninja.modules.payloads.PutFile.overwrite">
<span class="sig-name descname"><span class="pre">overwrite</span></span><em class="property"><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="pre">bool</span></em><a class="headerlink" href="#pyninja.modules.payloads.PutFile.overwrite" title="Permalink to this definition"></a></dt>
<dd></dd></dl>

</dd></dl>

</section>
<section id="module-pyninja.modules.rate_limit">
<span id="ratelimit"></span><h2>RateLimit<a class="headerlink" href="#module-pyninja.modules.rate_limit" title="Permalink to this heading"></a></h2>
Expand Down
Binary file modified docs/objects.inv
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/searchindex.js

Large diffs are not rendered by default.

13 changes: 0 additions & 13 deletions pyninja/modules/payloads.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from fastapi import UploadFile
from pydantic import BaseModel, DirectoryPath, FilePath, PositiveFloat, PositiveInt


Expand Down Expand Up @@ -34,15 +33,3 @@ class GetFile(BaseModel):
"""

filepath: FilePath


class PutFile(BaseModel):
"""Payload for put-file endpoint.
>>> PutFile
"""

file: UploadFile
directory: DirectoryPath
overwrite: bool = False
24 changes: 13 additions & 11 deletions pyninja/routes/fullaccess.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
from http import HTTPStatus
from typing import Optional

from fastapi import APIRouter, Depends, Header, Request
from fastapi import APIRouter, Depends, Header, Request, UploadFile
from fastapi.responses import FileResponse
from fastapi.security import HTTPAuthorizationCredentials, HTTPBasic, HTTPBearer
from pydantic import DirectoryPath

from pyninja.executors import auth, squire
from pyninja.modules import exceptions, payloads, tree
Expand Down Expand Up @@ -139,7 +140,9 @@ async def get_file(

async def put_file(
request: Request,
payload: payloads.PutFile,
file: UploadFile,
directory: DirectoryPath,
overwrite: bool = False,
apikey: HTTPAuthorizationCredentials = Depends(BEARER_AUTH),
token: Optional[str] = Header(None),
):
Expand All @@ -148,28 +151,27 @@ async def put_file(
**Args:**
- request: Reference to the FastAPI request object.
- file: Upload object for the file param.
- payload: Payload received as request body.
- apikey: API Key to authenticate the request.
- token: API secret to authenticate the request.
"""
await auth.level_2(request, apikey, token)
LOGGER.info(
"Requested file: '%s' for upload at %s",
payload.file.filename,
payload.directory,
file.filename,
directory,
)
content = await payload.file.read()
if not payload.overwrite and os.path.isfile(
os.path.join(payload.directory, payload.file.filename)
):
content = await file.read()
if not overwrite and os.path.isfile(os.path.join(directory, file.filename)):
raise exceptions.APIResponse(
status_code=HTTPStatus.BAD_REQUEST.real,
detail=f"File {payload.file.filename!r} exists at {str(payload.directory)!r} already, "
detail=f"File {file.filename!r} exists at {str(directory)!r} already, "
"set 'overwrite' flag to True to overwrite.",
)
with open(os.path.join(payload.directory, payload.file.filename), "wb") as f_stream:
with open(os.path.join(directory, file.filename), "wb") as f_stream:
f_stream.write(content)
raise exceptions.APIResponse(
status_code=HTTPStatus.OK.real,
detail=f"{payload.file.filename!r} was uploaded to {payload.directory}.",
detail=f"{file.filename!r} was uploaded to {directory}.",
)

0 comments on commit dcaa99c

Please sign in to comment.