0

I’m trying to create a simple app where I can insert data into a Nextcloud app called “Tables”. This app has an API, so I can insert the data through that.

Everything was working well until I changed the type of 3 columns to type “link”, (before they were type “text”). These columns are “Backup Link”, “Restore Link” and “Evidence Link”. Each column is meant to contain the link to a file inside of my Nextcloud. I changed the type because I wanted to just click in the text and It will get me to my browser and show me the cotnent of the file.

The code is mainly made by AI. I know isn’t the best way to code (mainly because I’m no really coding, just copying and pasting things), but I don’t have much time to learn how to do this in Python right now.

To summarize, my problem is that if my URL is https://nextcloud.example.com/1/asjklDHBkauBS, the column only shows https://nextcloud.example.c and nothing else. I added some traces to check the content of the URL being sent, and it appears correct, but when I look at it in Nextcloud, the URL gets cut off.

Any help would be great. Below I’ve added a simplified version of my code

Thank you!!

Simplified version of the code:

import requests
import json
import datetime
import logging

NC_BASE_URL = "https://nextcloud.example.com"
TABLES_API_BASE = f"{NC_BASE_URL}/apps/tables/api/1/tables"

def get_column_mapping(table_id, auth_config):
    """Retrieve the mapping between column names and their IDs."""
    try:
        auth = auth_config['auth']
        headers = auth_config['headers']
        response = requests.get(f"{TABLES_API_BASE}/{table_id}/columns", headers=headers, auth=auth,
                                verify=auth_config['verify'], timeout=auth_config['timeout'])
        if response.status_code == 200:
            columns = response.json()
            mapping = {col['title'].strip(): str(col['id']) for col in columns}
            return mapping
        else:
            print(f"Error retrieving columns: {response.status_code} - {response.text}")
            return None
    except Exception as e:
        logging.error(f"Error connecting to Nextcloud: {e}")
        return None

def register_backup(nc_user, nc_credential, auth_config, table_id, link_backup, link_restore, link_evidence, file_name):
    """Insert a new row into a Nextcloud Tables table."""
    mapping = get_column_mapping(table_id, auth_config)
    if not mapping:
        return False

    current_date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    payload_data = {
        mapping.get("Date"): current_date,
        mapping.get("Backup Link"): link_backup,
        mapping.get("Restore Link"): link_restore,
        mapping.get("Evidence Link"): link_evidence,
        mapping.get("FileName"): file_name,
    }

    payload_data_clean = {k: v for k, v in payload_data.items() if k and v is not None and v != ""}
    payload = {"data": payload_data_clean}

    print(json.dumps(payload, indent=2, ensure_ascii=False))

    try:
        response = requests.post(f"{TABLES_API_BASE}/{table_id}/rows", json=payload, **auth_config)
        if response.status_code in [200, 201]:
            logging.info(f"Backup successfully registered: {file_name}")
            return True
        else:
            logging.error(f"Error registering backup ({response.status_code}): {response.text}")
            return False
    except Exception as e:
        logging.error(f"Connection error: {e}")
        return False

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.