|
| 1 | +.. _first-steps: |
| 2 | + |
| 3 | +First steps |
| 4 | +=========== |
| 5 | + |
| 6 | +For this part, you will need an environment with **nc_py_api** installed and Nextcloud version 26 or higher. |
| 7 | + |
| 8 | +Full support is only available from version ``27.1`` of Nextcloud. |
| 9 | + |
| 10 | +Nextcloud client |
| 11 | +^^^^^^^^^^^^^^^^ |
| 12 | + |
| 13 | +.. note:: In many cases, even if you want to develop an application, |
| 14 | + it's a good idea to first debug and develop part of it as a client. |
| 15 | + |
| 16 | +Creation |
| 17 | +"""""""" |
| 18 | + |
| 19 | +.. code-block:: python |
| 20 | +
|
| 21 | + from nc_py_api import Nextcloud |
| 22 | +
|
| 23 | +
|
| 24 | + nc = Nextcloud(nextcloud_url="http://nextcloud.local", nc_auth_user="admin", nc_auth_pass="admin") |
| 25 | +
|
| 26 | +Where ``nc_auth_pass`` can be usual Nextcloud application password. |
| 27 | + |
| 28 | +To test if this works, let's print the capabilities of the Nextcloud instance: |
| 29 | + |
| 30 | +.. code-block:: python |
| 31 | +
|
| 32 | + from json import dumps |
| 33 | +
|
| 34 | + from nc_py_api import Nextcloud |
| 35 | +
|
| 36 | +
|
| 37 | + nc = Nextcloud(nextcloud_url="http://nextcloud.local", nc_auth_user="admin", nc_auth_pass="admin") |
| 38 | + pretty_capabilities = dumps(nc.capabilities, indent=4, sort_keys=True) |
| 39 | + print(pretty_capabilities) |
| 40 | +
|
| 41 | +Getting list of files of User |
| 42 | +""""""""""""""""""""""""""""" |
| 43 | + |
| 44 | +This is a hard way to get list of all files recursively: |
| 45 | + |
| 46 | +.. literalinclude:: ../examples/as_client/files_listing.py |
| 47 | + |
| 48 | +This code do the same in one DAV call, but prints **directories** in addition to files: |
| 49 | + |
| 50 | +.. code-block:: python |
| 51 | +
|
| 52 | + from nc_py_api import Nextcloud |
| 53 | +
|
| 54 | +
|
| 55 | + nc = Nextcloud(nextcloud_url="http://nextcloud.local", nc_auth_user="admin", nc_auth_pass="admin") |
| 56 | + print("Files & folders on the instance for the selected user:") |
| 57 | + all_files_folders = nc.files.listdir(depth=-1) |
| 58 | + for obj in all_files_folders: |
| 59 | + print(obj.user_path) |
| 60 | +
|
| 61 | +To print only files, you can use list comprehension: |
| 62 | + |
| 63 | +.. code-block:: python |
| 64 | +
|
| 65 | + print("Files on the instance for the selected user:") |
| 66 | + all_files = [i for i in nc.files.listdir(depth=-1) if not i.is_dir] |
| 67 | + for obj in all_files: |
| 68 | + print(obj.user_path) |
| 69 | +
|
| 70 | +Uploading a single file |
| 71 | +""""""""""""""""""""""" |
| 72 | + |
| 73 | +It is always better to use ``upload_stream`` instead of ``upload`` as it works |
| 74 | +with chunks and ``in future`` will support **multi threaded** upload. |
| 75 | + |
| 76 | +.. literalinclude:: ../examples/as_client/files_upload.py |
| 77 | + |
| 78 | +Downloading a single file |
| 79 | +""""""""""""""""""""""""" |
| 80 | + |
| 81 | +A very simple example of downloading an image as one piece of data to memory and displaying it. |
| 82 | + |
| 83 | +.. note:: For big files, it is always better to use ``download2stream`` method, as it uses chunks. |
| 84 | + |
| 85 | +.. literalinclude:: ../examples/as_client/files_download.py |
0 commit comments