|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: Getting Started |
| 4 | +nav_order: 2 |
| 5 | +--- |
| 6 | + |
| 7 | +**Until the 1.0 release is available**, please follow the instructions in the CONTRIBUTING.md file for installing the |
| 8 | +MarkLogic Python client into a Python virtual environment. |
| 9 | + |
| 10 | +## Connecting to MarkLogic |
| 11 | + |
| 12 | +(TODO This will almost certainly be reorganized before the 1.0 release.) |
| 13 | + |
| 14 | +The `Client` class is the primary API to interact with in the MarkLogic Python client. The |
| 15 | +`Client` class extends the `requests` |
| 16 | +[`Session` class](https://docs.python-requests.org/en/latest/user/advanced/#session-objects), thus exposing all methods |
| 17 | +found in both the `Session` class and the `requests` API. You can therefore use a `Client` object in the same manner |
| 18 | +as you'd use either the `Session` class or the `requests` API. |
| 19 | + |
| 20 | +A `Client` instance can be constructed either by providing a base URL for all requests along with authentication: |
| 21 | + |
| 22 | + from marklogic.client import Client |
| 23 | + client = Client('http://localhost:8030', digest=('username', 'password')) |
| 24 | + |
| 25 | +Or via separate arguments for each of the parts of a base URL: |
| 26 | + |
| 27 | + from marklogic.client import Client |
| 28 | + client = Client(host='localhost', port='8030', digest=('username', 'password')) |
| 29 | + |
| 30 | +After constructing a `Client` instance, each of the methods in the `requests` API for sending an HTTP request can be |
| 31 | +used without needing to specify the base URL nor the authentication again. For example: |
| 32 | + |
| 33 | + response = client.post('/v1/search') |
| 34 | + response = client.get('/v1/documents', params={'uri': '/my-doc.json'}) |
| 35 | + |
| 36 | +Because the `Client` class extends the `Sessions` class, it can be used as a context manager: |
| 37 | + |
| 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'}) |
| 41 | + |
| 42 | +## Authentication |
| 43 | + |
| 44 | +The `Client` constructor includes a `digest` argument as a convenience for using digest authentication: |
| 45 | + |
| 46 | + from marklogic.client import Client |
| 47 | + client = Client('http://localhost:8030', digest=('username', 'password')) |
| 48 | + |
| 49 | +An `auth` argument is also available for using any authentication strategy that can be configured |
| 50 | +[via the requests `auth` argument](https://requests.readthedocs.io/en/latest/user/advanced/#custom-authentication). For |
| 51 | +example, just like with `requests`, a tuple can be passed to the `auth` argument to use basic authentication: |
| 52 | + |
| 53 | + from marklogic.client import Client |
| 54 | + client = Client('http://localhost:8030', auth=('username', 'password')) |
| 55 | + |
| 56 | +## SSL |
| 57 | + |
| 58 | +Configuring SSL connections is the same as |
| 59 | +[documented for the `requests` library](https://requests.readthedocs.io/en/latest/user/advanced/#ssl-cert-verification). |
| 60 | +As a convience, the `Client` constructor includes a `verify` argument so that it does not need to be configured on the |
| 61 | +`Client` instance after it's been constructed nor on every request: |
| 62 | + |
| 63 | + from marklogic.client import Client |
| 64 | + client = Client('https://localhost:8030', digest=('username', 'password'), verify='/path/to/cert.pem') |
| 65 | + |
| 66 | +When specifying the base URL via separate arguments, the `scheme` argument can be set for HTTPS connections: |
| 67 | + |
| 68 | + from marklogic.client import Client |
| 69 | + client = Client(host='localhost', port='8030', scheme='https', digest=('username', 'password'), verify=False) |
0 commit comments