Skip to content

Instantly share code, notes, and snippets.

@rjrudin
Created September 29, 2022 11:14
Show Gist options
  • Save rjrudin/46011bd6daee06b1de39b633e741e0d4 to your computer and use it in GitHub Desktop.
Save rjrudin/46011bd6daee06b1de39b633e741e0d4 to your computer and use it in GitHub Desktop.
batch = DocumentBatch()
# Document with every type of metadata supplied
batch.add(
Document(
uri="/example1.json",
content={"some": "data"},
content_type="application/json",
metadata=Metadata(
quality=1.2,
collections=["coll1", "coll2"],
permissions={
"rest-reader": ["read", "execute"],
"rest-writer": "update",
},
properties={
"prop1": "simple value",
"prop2": "<nested><xml>will work</xml></nested>",
},
metadataValues={"key1": "value1", "key2": "value2"},
),
)
)
# Minimal example of creating a document
batch.add(
Document(
uri="/example2.xml",
content="<some>xml</some>",
content_type="application/xml",
),
)
data, headers = batch.data_and_headers()
response = requests.post(
"http://localhost:8000/v1/documents",
auth=HTTPDigestAuth("admin", "admin"),
data=data,
headers=headers,
)
@rjrudin
Copy link
Author

rjrudin commented Sep 29, 2022

Implementing this can be done by copying the excellent work at marklogic/python_api - specifically in https://github.com/marklogic/python_api/blob/master/marklogic/client/bulkloader.py and https://github.com/marklogic/python_api/blob/master/marklogic/client/documents.py

Note that that project has a much larger scope, covering parts of the Manage API and Client API. Additionally, the Documents class serves two purposes - one as a facade on /v1/documents, and also for holding bits about a single document.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment