@@ -9,73 +9,100 @@ MarkLogic Python client into a Python virtual environment.
99
1010## Connecting to MarkLogic
1111
12- (TODO This will almost certainly be reorganized before the 1.0 release.)
13-
1412The ` Client ` class is the primary API to interact with in the MarkLogic Python client. The
1513` Client ` class extends the ` requests `
1614[ ` Session ` class] ( https://docs.python-requests.org/en/latest/user/advanced/#session-objects ) , thus exposing all methods
1715found in both the ` Session ` class and the ` requests ` API. You can therefore use a ` Client ` object in the same manner
1816as you'd use either the ` Session ` class or the ` requests ` API.
1917
18+ To try out any of the examples below or in the rest of this guide, you will first need to create a new MarkLogic user.
19+ To do so, please go to the Admin application for your MarkLogic instance - e.g. if you are running MarkLogic locally,
20+ this will be at < http://localhost:8001 > , and you will authenticate as your "admin" user. Then perform the following
21+ steps to create a new user:
22+
23+ 1 . Click on "Users" in the "Security" box.
24+ 2 . Click on "Create".
25+ 3 . In the form, enter "python-user" for "User Name" and "pyth0n" as the password.
26+ 4 . Scroll down until you see the "Roles" section. Click on the "rest-reader" and "rest-writer" checkboxes.
27+ 5 . Scroll to the top or bottom and click on "OK" to create the user.
28+
2029A ` Client ` instance can be constructed either by providing a base URL for all requests along with authentication:
2130
22- from marklogic import Client
23- client = Client('http://localhost:8030', digest=('username', 'password'))
31+ ```
32+ from marklogic import Client
33+ client = Client('http://localhost:8000', digest=('python-user', 'pyth0n'))
34+ ```
2435
2536Or via separate arguments for each of the parts of a base URL:
2637
27- from marklogic import Client
28- client = Client(host='localhost', port='8030', digest=('username', 'password'))
38+ ```
39+ from marklogic import Client
40+ client = Client(host='localhost', port='8000', digest=('python-user', 'pyth0n'))
41+ ```
2942
3043After constructing a ` Client ` instance, each of the methods in the ` requests ` API for sending an HTTP request can be
3144used without needing to specify the base URL nor the authentication again. For example:
3245
33- response = client.post('/v1/search')
34- response = client.get('/v1/documents', params={'uri': '/my-doc.json'})
46+ ```
47+ response = client.post('/v1/search')
48+ response = client.get('/v1/documents', params={'uri': '/my-doc.json'})
49+ ```
3550
3651Because the ` Client ` class extends the ` Sessions ` class, it can be used as a context manager:
3752
38- with Client('http://localhost:8030', digest=('username', 'password')) as client:
39- response = client.post('/v1/search')
40- response = client.get('/v1/documents', params={'uri': '/my-doc.json'})
53+ ```
54+ with Client('http://localhost:8000', digest=('python-user', 'pyth0n')) as client:
55+ response = client.post('/v1/search')
56+ response = client.get('/v1/documents', params={'uri': '/my-doc.json'})
57+ ```
4158
4259## Authentication
4360
4461The ` Client ` constructor includes a ` digest ` argument as a convenience for using digest authentication:
4562
46- from marklogic import Client
47- client = Client('http://localhost:8030', digest=('username', 'password'))
63+ ```
64+ from marklogic import Client
65+ client = Client('http://localhost:8000', digest=('python-user', 'pyth0n'))
66+ ```
4867
4968An ` auth ` argument is also available for using any authentication strategy that can be configured
5069[ via the requests ` auth ` argument] ( https://requests.readthedocs.io/en/latest/user/advanced/#custom-authentication ) . For
5170example, just like with ` requests ` , a tuple can be passed to the ` auth ` argument to use basic authentication:
5271
53- from marklogic import Client
54- client = Client('http://localhost:8030', auth=('username', 'password'))
72+ ```
73+ from marklogic import Client
74+ client = Client('http://localhost:8000', auth=('python-user', 'pyth0n'))
75+ ```
5576
5677### MarkLogic Cloud Authentication
5778
5879When connecting to a [ MarkLogic Cloud instance] ( https://developer.marklogic.com/products/cloud/ ) , you will need to set
5980the ` cloud_api_key ` and ` base_path ` arguments. You only need to specify a ` host ` as well, as port 443 and HTTPS will be
6081used by default. For example:
6182
62- from marklogic import Client
63- client = Client(host='example.marklogic.cloud', cloud_api_key='some-key-value', base_path='/ml/example/manage')
83+ ```
84+ from marklogic import Client
85+ client = Client(host='example.marklogic.cloud', cloud_api_key='some-key-value', base_path='/ml/example/manage')
86+ ```
6487
6588You may still use a full base URL if you wish:
6689
67- from marklogic import Client
68- client = Client('https://example.marklogic.cloud', cloud_api_key='some-key-value', base_path='/ml/example/manage')
90+ ```
91+ from marklogic import Client
92+ client = Client('https://example.marklogic.cloud', cloud_api_key='some-key-value', base_path='/ml/example/manage')
93+ ```
6994
7095MarkLogic Cloud uses an access token for authentication; the access token is generated using the API key value. In some
7196scenarios, you may wish to set the token expiration time to a value other than the default used by MarkLogic Cloud. To
7297do so, set the ` cloud_token_duration ` argument to a number greater than zero that defines the token duration in
7398minutes:
7499
75- from marklogic import Client
76- # Sets a token duration of 10 minutes.
77- client = Client(host='example.marklogic.cloud', cloud_api_key='some-key-value', base_path='/ml/example/manage',
78- cloud_token_duration=10)
100+ ```
101+ from marklogic import Client
102+ # Sets a token duration of 10 minutes.
103+ client = Client(host='example.marklogic.cloud', cloud_api_key='some-key-value', base_path='/ml/example/manage',
104+ cloud_token_duration=10)
105+ ```
79106
80107## SSL
81108
@@ -84,10 +111,14 @@ Configuring SSL connections is the same as
84111As a convience, the ` Client ` constructor includes a ` verify ` argument so that it does not need to be configured on the
85112` Client ` instance after it's been constructed nor on every request:
86113
87- from marklogic import Client
88- client = Client('https://localhost:8030', digest=('username', 'password'), verify='/path/to/cert.pem')
114+ ```
115+ from marklogic import Client
116+ client = Client('https://localhost:8000', digest=('python-user', 'pyth0n'), verify='/path/to/cert.pem')
117+ ```
89118
90119When specifying the base URL via separate arguments, the ` scheme ` argument can be set for HTTPS connections:
91120
92- from marklogic import Client
93- client = Client(host='localhost', port='8030', scheme='https', digest=('username', 'password'), verify=False)
121+ ```
122+ from marklogic import Client
123+ client = Client(host='localhost', port='8000', scheme='https', digest=('python-user', 'pyth0n'), verify=False)
124+ ```
0 commit comments