@@ -9,73 +9,100 @@ MarkLogic Python client into a Python virtual environment.
9
9
10
10
## Connecting to MarkLogic
11
11
12
- (TODO This will almost certainly be reorganized before the 1.0 release.)
13
-
14
12
The ` Client ` class is the primary API to interact with in the MarkLogic Python client. The
15
13
` Client ` class extends the ` requests `
16
14
[ ` Session ` class] ( https://docs.python-requests.org/en/latest/user/advanced/#session-objects ) , thus exposing all methods
17
15
found in both the ` Session ` class and the ` requests ` API. You can therefore use a ` Client ` object in the same manner
18
16
as you'd use either the ` Session ` class or the ` requests ` API.
19
17
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
+
20
29
A ` Client ` instance can be constructed either by providing a base URL for all requests along with authentication:
21
30
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
+ ```
24
35
25
36
Or via separate arguments for each of the parts of a base URL:
26
37
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
+ ```
29
42
30
43
After constructing a ` Client ` instance, each of the methods in the ` requests ` API for sending an HTTP request can be
31
44
used without needing to specify the base URL nor the authentication again. For example:
32
45
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
+ ```
35
50
36
51
Because the ` Client ` class extends the ` Sessions ` class, it can be used as a context manager:
37
52
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
+ ```
41
58
42
59
## Authentication
43
60
44
61
The ` Client ` constructor includes a ` digest ` argument as a convenience for using digest authentication:
45
62
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
+ ```
48
67
49
68
An ` auth ` argument is also available for using any authentication strategy that can be configured
50
69
[ via the requests ` auth ` argument] ( https://requests.readthedocs.io/en/latest/user/advanced/#custom-authentication ) . For
51
70
example, just like with ` requests ` , a tuple can be passed to the ` auth ` argument to use basic authentication:
52
71
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
+ ```
55
76
56
77
### MarkLogic Cloud Authentication
57
78
58
79
When connecting to a [ MarkLogic Cloud instance] ( https://developer.marklogic.com/products/cloud/ ) , you will need to set
59
80
the ` cloud_api_key ` and ` base_path ` arguments. You only need to specify a ` host ` as well, as port 443 and HTTPS will be
60
81
used by default. For example:
61
82
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
+ ```
64
87
65
88
You may still use a full base URL if you wish:
66
89
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
+ ```
69
94
70
95
MarkLogic Cloud uses an access token for authentication; the access token is generated using the API key value. In some
71
96
scenarios, you may wish to set the token expiration time to a value other than the default used by MarkLogic Cloud. To
72
97
do so, set the ` cloud_token_duration ` argument to a number greater than zero that defines the token duration in
73
98
minutes:
74
99
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
+ ```
79
106
80
107
## SSL
81
108
@@ -84,10 +111,14 @@ Configuring SSL connections is the same as
84
111
As a convience, the ` Client ` constructor includes a ` verify ` argument so that it does not need to be configured on the
85
112
` Client ` instance after it's been constructed nor on every request:
86
113
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
+ ```
89
118
90
119
When specifying the base URL via separate arguments, the ` scheme ` argument can be set for HTTPS connections:
91
120
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