Skip to content

Commit 0a88cbd

Browse files
authored
Merge pull request #18 from marklogic/feature/doc-fixes
Fixes for the docs on writing
2 parents 03e6582 + ba36807 commit 0a88cbd

File tree

1 file changed

+60
-37
lines changed

1 file changed

+60
-37
lines changed

docs/managing-documents/writing.md

Lines changed: 60 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,66 +9,85 @@ permalink: /documents/writing
99
The [POST /v1/documents](https://docs.marklogic.com/REST/POST/v1/documents) endpoint in the MarkLogic REST API supports
1010
writing multiple documents with metadata via a multipart HTTP request. The MarkLogic Python client
1111
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+
```
1422

1523
## Writing documents with metadata
1624

1725
Writing a document requires specifying a URI, the document content, and at least one update permission (a user with the
1826
"admin" role does not need to specify an update permission, but using such a user in an application is not encouraged).
1927
The example below demonstrates this; the `default_perms` variable is used in subsequent examples.
2028

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+
```
2434

2535
The `write` method returns a [Response instance](https://requests.readthedocs.io/en/latest/api/#requests.Response)
2636
from the `requests` API, giving you access to everything returned by the MarkLogic REST API.
2737

2838
The `Document` class supports all of the types of metadata that can be assigned to a document:
2939

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+
```
4052

4153
Multiple documents can be written in a single call, each with their own metadata:
4254

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+
```
4761

4862
## Writing documents with different content types
4963

5064
The examples above create documents with a dictionary as content, resulting in JSON documents in MarkLogic. An XML
5165
document can be created with content defined via a string, including in the same request that creates a JSON document:
5266

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+
```
5773

5874
Binary documents can be created by passing in binary content:
5975

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+
```
6179

6280
A `Document` has a `content_type` attribute that allows for explicitly defining the
6381
mimetype of a document. This feature is useful in a scenario where MarkLogic does not
6482
have a mimetype registered for the URI extension, or there is no extension:
6583

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+
```
7291

7392
## Specifying default metadata
7493

@@ -79,12 +98,14 @@ instance, unless it specifies its own metadata.
7998

8099
Consider the following example:
81100

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+
```
88109

89110
The first document will be written with the metadata specified in the first `DefaultMetadata` instance. The second
90111
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
113134
URI beginning with "/example/" and ending with ".json", with MarkLogic adding a random identifier in between to
114135
construct the URI:
115136

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+
```
117140

118141
## Error handling
119142

0 commit comments

Comments
 (0)