urschrei / pyzotero

Pyzotero: a Python client for the Zotero API
https://pyzotero.readthedocs.org
Other
908 stars 99 forks source link

Can't attach note to item #144

Closed skvrnami closed 3 years ago

skvrnami commented 3 years ago

Problem Description

I was trying to create a note and attach it to an item, but the function create_items raises 404 Client Error. The problem is in attaching the note to the parent item as the note is successfully created in the main Zotero folder, but the attachment of note to the parent item fails.

Minimal reproducible example is here:

from config import API_KEY, LIBRARY_ID
from pyzotero import zotero as pyzotero

LIBRARY_TYPE = 'user'
zotero = pyzotero.Zotero(LIBRARY_ID, LIBRARY_TYPE, API_KEY)

new_item = zotero.item_template("book")
new_item['title'] = 'Hobbit'
resp = zotero.create_items([new_item])
new_item_id = [v for v in resp['success'].values()][0]

note = zotero.item_template("note")
note['note'] = 'Bilbo Baggins'

note_resp = zotero.create_items([note], parentid=new_item_id)

Platform: macOS Python version: Python 3.8.2 Pyzotero version: 1.4.24

More Details

Traceback ``` Traceback (most recent call last): File "/Users/skvrnami/github/zoteroRemarkable/.env/lib/python3.8/site-packages/pyzotero/zotero.py", line 1295, in create_items presp.raise_for_status() File "/Users/skvrnami/github/zoteroRemarkable/.env/lib/python3.8/site-packages/requests/models.py", line 943, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://api.zotero.org/users/2422340/items/2D8VZ86W During handling of the above exception, another exception occurred: Traceback (most recent call last): File "", line 1, in File "/Users/skvrnami/github/zoteroRemarkable/.env/lib/python3.8/site-packages/pyzotero/zotero.py", line 1297, in create_items error_handler(self, presp) File "/Users/skvrnami/github/zoteroRemarkable/.env/lib/python3.8/site-packages/pyzotero/zotero.py", line 1651, in error_handler raise error_codes.get(req.status_code)(err_msg(req)) pyzotero.zotero_errors.ResourceNotFound: Code: 404 URL: https://api.zotero.org/users/2422340/items/2D8VZ86W Method: PATCH Response: Not found ```
urschrei commented 3 years ago

I can't reproduce this using 1.4.24: your example works perfectly across multiple tries. Can you confirm that this wasn't a transient error, and that "simple" item creation works correctly; i.e. that a created item becomes available in the web interface / client?

skvrnami commented 3 years ago

Hi, thanks for your time. I checked and the item creation works ok (see screenshot from the web interface). The code returns error after running the last line.

Snímek obrazovky 2021-08-16 v 14 22 04
urschrei commented 3 years ago

As you can see from the traceback, there's a 404 error occurring which means that the API call which attempts to create the note can't find the parent item which is being created first. Unless there's an error when creating the parent item, I don't see how this can be possible in a single-threaded program.

Are you sure that the example you posted is representative of what you're trying to do? This is the sort of problem that can occur in an async program or something using sub-processes – as I said, when I run your example program it works.