Skip to content

Commit 9b50297

Browse files
authored
+ two chapters in docs for NcApp (#104)
Still missing chapters about logs and notifications. And chapter about files is incomplete. Signed-off-by: Alexander Piskun <bigcat88@icloud.com>
1 parent de74b47 commit 9b50297

File tree

3 files changed

+113
-5
lines changed

3 files changed

+113
-5
lines changed

docs/NextcloudApp.rst

Lines changed: 110 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,117 @@ Before reading this chapter, please review the basic information about deploymen
9191
and the currently supported types of
9292
`deployments configurations <https://cloud-py-api.github.io/app_ecosystem_v2/DeployConfigurations.html>`_ in the AppEcosystem documentation.
9393

94-
to-do
94+
Docker Deploy Daemon
95+
""""""""""""""""""""
96+
97+
Docker images with the application can be deployed both on Docker Hub or on GitHub.
98+
All examples in this repository use GitHub for deployment.
99+
100+
To build the application locally, if you do not have a Mac with Apple Silicon, you will need to install QEMU, to be able
101+
to build image for both **aarch64** and **x64** architectures. Of course it is always your choice and you can support only one type
102+
of CPU and not both, but it is **highly recommended to support both** of them.
103+
104+
First login to preferred docker registry:
105+
106+
.. code-block:: shell
107+
108+
docker login ghcr.io
109+
110+
After that build and push images to it:
111+
112+
.. code-block:: shell
113+
114+
docker buildx build --push --platform linux/arm64/v8,linux/amd64 --tag ghcr.io/REPOSITORY_OWNER/APP_ID:N_VERSION .
115+
116+
Where APP_ID can be repository name, and it is up to you to decide.
117+
118+
.. note:: It is not recommended to use only the ``latest`` tag for the application's image, as increasing the version
119+
of your application will overwrite the previous version, in this case, use several tags to leave the possibility
120+
of installing previous versions of your application.
95121

96122
From skeleton to ToGif
97123
----------------------
98124

99-
to-do
125+
Now it's time to move on to something more complex than just the application skeleton.
126+
127+
Let's consider an example of an application that performs an action with a file when
128+
you click on the drop-down context menu and reports on the work done using notification.
129+
130+
First of all, we modernize info.ixml, add the API groups we need for this to work with **Files** and **Notifications**:
131+
132+
.. code-block:: xml
133+
134+
<scopes>
135+
<required>
136+
<value>FILES</value>
137+
<value>NOTIFICATIONS</value>
138+
</required>
139+
<optional>
140+
</optional>
141+
</scopes>
142+
143+
.. note:: Full list of avalaible API scopes can be found `here <https://cloud-py-api.github.io/app_ecosystem_v2/tech_details/ApiScopes.html>`_.
144+
145+
After that we extend the **enabled** handler and include there registration of the drop-down list element:
146+
147+
.. code-block:: python
148+
149+
def enabled_handler(enabled: bool, nc: NextcloudApp) -> str:
150+
try:
151+
if enabled:
152+
nc.ui.files_dropdown_menu.register("to_gif", "TO GIF", "/video_to_gif", mime="video")
153+
else:
154+
nc.ui.files_dropdown_menu.unregister("to_gif")
155+
except Exception as e:
156+
return str(e)
157+
return ""
158+
159+
After that, let's define the **"/video_to_gif"** endpoint that we had registered in previous step:
160+
161+
.. code-block:: python
162+
163+
@APP.post("/video_to_gif")
164+
async def video_to_gif(
165+
file: UiFileActionHandlerInfo,
166+
nc: Annotated[NextcloudApp, Depends(nc_app)],
167+
background_tasks: BackgroundTasks,
168+
):
169+
background_tasks.add_task(convert_video_to_gif, file.actionFile.to_fs_node(), nc)
170+
return Response()
171+
172+
And this step should be discussed in more detail, since it demonstrates most of the process of working External applications.
173+
174+
Here we see: **nc: Annotated[NextcloudApp, Depends(nc_app)]**
175+
176+
For those who already know how FastAPI works, everything should be clear by now,
177+
and for those who have not, it is very important to understand that:
178+
179+
It is a declaration of FastAPI `dependency <https://fastapi.tiangolo.com/tutorial/dependencies/#dependencies>`_ to be executed
180+
before the code of **video_to_gif** starts execution.
181+
182+
And this required dependency handles authentication and returns an instance of the :py:class:`~nc_py_api.nextcloud.NextcloudApp`
183+
class that allows you to make requests to Nextcloud.
184+
185+
.. note:: Every endpoint in your application should be protected with such method, this will ensure that only Nextcloud instance
186+
will be able to perform requests to the application.
187+
188+
Finally, we are left with two much less interesting parameters, let's start with the last one, with **BackgroundTasks**:
189+
190+
FastAPI `BackgroundTasks <https://fastapi.tiangolo.com/tutorial/background-tasks/?h=backgroundtasks#background-tasks>`_ documentation.
191+
192+
Since in most cases, the tasks that the application will perform will depend either on additional network calls or
193+
heavy calculations and we cannot guarantee a fast completion time, it is recommended to always try to return
194+
an empty response (which will be a status of 200) and in the background already slowly perform operations.
195+
196+
The last parameter is a structure describing the action and the file on which it needs to be performed,
197+
which is passed by the Appecosystem when clicking on the drop-down context menu of the file.
198+
199+
We use the built method :py:meth:`~nc_py_api.ex_app.ui.files.UiActionFileInfo.to_fs_node` into the structure to convert it
200+
into a standard :py:class:`~nc_py_api.files.FsNode` class that describes the file and pass the FsNode class instance to the background task.
201+
202+
In the **convert_video_to_gif** function, a standard conversion using ``OpenCV`` from a video file to a GIF image occurs,
203+
and since this is not directly related to working with NextCloud, we will skip this for now.
204+
205+
**ToGif** example `full source <https://github.com/cloud-py-api/nc_py_api/blob/main/examples/as_app/to_gif/src/main.py>`_ code.
206+
207+
This chapter ends here, but the next topics are even more intriguing.

examples/as_app/to_gif/appinfo/info.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
<scopes>
2828
<required>
2929
<value>FILES</value>
30+
<value>NOTIFICATIONS</value>
3031
</required>
3132
<optional>
32-
<value>NOTIFICATIONS</value>
3333
</optional>
3434
</scopes>
3535
<protocol>http</protocol>

nc_py_api/files/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ class FsNodeInfo:
1212
"""Extra FS object attributes from Nextcloud."""
1313

1414
size: int
15-
"""Length of file in bytes, zero for directories.."""
16-
content_length: int
1715
"""For directories it is size of all content in it, for files it is equal to ``size``."""
16+
content_length: int
17+
"""Length of file in bytes, zero for directories."""
1818
permissions: str
1919
"""Permissions for the object."""
2020
favorite: bool

0 commit comments

Comments
 (0)