@@ -9,66 +9,85 @@ permalink: /documents/writing
9
9
The [ POST /v1/documents] ( https://docs.marklogic.com/REST/POST/v1/documents ) endpoint in the MarkLogic REST API supports
10
10
writing multiple documents with metadata via a multipart HTTP request. The MarkLogic Python client
11
11
simplifies the use of this endpoint via the ` client.documents.write ` method and the ` Document `
12
- class. The examples below all assume that you have constructed a ` Client ` instance already as described in the
13
- [ setup guide] ( /setup ) .
12
+ class.
13
+
14
+ The examples below all assume that you have created a new MarkLogic user named "python-user" as described in the
15
+ [ setup guide] ( /setup ) . In addition, each of the examples below requires the following ` Client ` instance to be created
16
+ first:
17
+
18
+ ```
19
+ from marklogic import Client
20
+ client = Client('http://localhost:8000', digest=('python-user', 'pyth0n'))
21
+ ```
14
22
15
23
## Writing documents with metadata
16
24
17
25
Writing a document requires specifying a URI, the document content, and at least one update permission (a user with the
18
26
"admin" role does not need to specify an update permission, but using such a user in an application is not encouraged).
19
27
The example below demonstrates this; the ` default_perms ` variable is used in subsequent examples.
20
28
21
- from marklogic.documents import Document
22
- default_perms = {"rest-reader": ["read", "update"]}
23
- response = client.documents.write(Document("/doc1.json", {"doc": 1}, permissions=default_perms))
29
+ ```
30
+ from marklogic.documents import Document
31
+ default_perms = {"rest-reader": ["read", "update"]}
32
+ response = client.documents.write(Document("/doc1.json", {"doc": 1}, permissions=default_perms))
33
+ ```
24
34
25
35
The ` write ` method returns a [ Response instance] ( https://requests.readthedocs.io/en/latest/api/#requests.Response )
26
36
from the ` requests ` API, giving you access to everything returned by the MarkLogic REST API.
27
37
28
38
The ` Document ` class supports all of the types of metadata that can be assigned to a document:
29
39
30
- doc = Document(
31
- "/doc1.json",
32
- {"doc": 1},
33
- permissions=default_perms,
34
- collections=["c1", "c2"],
35
- quality=10,
36
- metadata_values={"key1": "value1", "key2": "value2"},
37
- properties={"prop1": "value1", "prop2": 2}
38
- )
39
- client.documents.write(doc)
40
+ ```
41
+ doc = Document(
42
+ "/doc1.json",
43
+ {"doc": 1},
44
+ permissions=default_perms,
45
+ collections=["c1", "c2"],
46
+ quality=10,
47
+ metadata_values={"key1": "value1", "key2": "value2"},
48
+ properties={"prop1": "value1", "prop2": 2}
49
+ )
50
+ client.documents.write(doc)
51
+ ```
40
52
41
53
Multiple documents can be written in a single call, each with their own metadata:
42
54
43
- client.documents.write([
44
- Document("/doc1.json", {"doc": 1}, permissions=default_perms, collections=["example"])
45
- Document("/doc2.json", {"doc": 2}, permissions=default_perms, quality=5),
46
- ])
55
+ ```
56
+ client.documents.write([
57
+ Document("/doc1.json", {"doc": 1}, permissions=default_perms, collections=["example"]),
58
+ Document("/doc2.json", {"doc": 2}, permissions=default_perms, quality=5),
59
+ ])
60
+ ```
47
61
48
62
## Writing documents with different content types
49
63
50
64
The examples above create documents with a dictionary as content, resulting in JSON documents in MarkLogic. An XML
51
65
document can be created with content defined via a string, including in the same request that creates a JSON document:
52
66
53
- client.documents.write([
54
- Document("/doc1.json", {"doc": 1}, permissions=default_perms)
55
- Document("/doc2.xml", "<doc>2</doc>", permissions=default_perms),
56
- ])
67
+ ```
68
+ client.documents.write([
69
+ Document("/doc1.json", {"doc": 1}, permissions=default_perms),
70
+ Document("/doc2.xml", "<doc>2</doc>", permissions=default_perms),
71
+ ])
72
+ ```
57
73
58
74
Binary documents can be created by passing in binary content:
59
75
60
- client.documents.write([Document("/doc1.bin", b"example content", permissions=default_perms)])
76
+ ```
77
+ client.documents.write([Document("/doc1.bin", b"example content", permissions=default_perms)])
78
+ ```
61
79
62
80
A ` Document ` has a ` content_type ` attribute that allows for explicitly defining the
63
81
mimetype of a document. This feature is useful in a scenario where MarkLogic does not
64
82
have a mimetype registered for the URI extension, or there is no extension:
65
83
66
- client.documents.write([Document(
67
- "/doc1", "some text",
68
- permissions=default_perms,
69
- content_type="text/plain
70
- )])
71
-
84
+ ```
85
+ client.documents.write([Document(
86
+ "/doc1", "some text",
87
+ permissions=default_perms,
88
+ content_type="text/plain"
89
+ )])
90
+ ```
72
91
73
92
## Specifying default metadata
74
93
@@ -79,12 +98,14 @@ instance, unless it specifies its own metadata.
79
98
80
99
Consider the following example:
81
100
82
- from marklogic.documents import Document, DefaultMetadata
83
- client.documents.write([
84
- DefaultMetadata(perms=default_perms, collections=["example"]),
85
- Document("/doc1.json", {"doc": 1}),
86
- Document("/doc2.json", {"doc": 2", perms=default_perms, quality=10})
87
- ])
101
+ ```
102
+ from marklogic.documents import Document, DefaultMetadata
103
+ client.documents.write([
104
+ DefaultMetadata(permissions=default_perms, collections=["example"]),
105
+ Document("/doc1.json", {"doc": 1}),
106
+ Document("/doc2.json", {"doc": 2}, permissions=default_perms, quality=10)
107
+ ])
108
+ ```
88
109
89
110
The first document will be written with the metadata specified in the first ` DefaultMetadata ` instance. The second
90
111
document will not use the default metadata since it specifies its own metadata. It will have the same permissions, but
@@ -113,7 +134,9 @@ The following shows an example of writing a document without specifying a URI, w
113
134
URI beginning with "/example/" and ending with ".json", with MarkLogic adding a random identifier in between to
114
135
construct the URI:
115
136
116
- client.documents.write([Document(null, {"doc": 1}, extension=".json", directory="/example/")])
137
+ ```
138
+ client.documents.write([Document(None, {"doc": 1}, extension=".json", directory="/example/")])
139
+ ```
117
140
118
141
## Error handling
119
142
0 commit comments