|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: Executing code |
| 4 | +nav_order: 5 |
| 5 | +--- |
| 6 | + |
| 7 | +The [MarkLogic REST service extension](https://docs.marklogic.com/REST/client/service-extension) supports the |
| 8 | +execution of custom code, whether via an inline script or an existing module in your application's modules database. |
| 9 | +The MarkLogic Python client simplifies execution of custom code both by managing some of the complexity of submitting |
| 10 | +and custom code and also converting the multipart response into more useful Python data types. |
| 11 | + |
| 12 | +## Setup |
| 13 | + |
| 14 | +The examples below all depend on the instructions in the [setup guide](example-setup.md) having already been performed. |
| 15 | + |
| 16 | +To try out the examples, start a Python shell and first run the following: |
| 17 | + |
| 18 | +``` |
| 19 | +from marklogic import Client |
| 20 | +client = Client('http://localhost:8000', digest=('python-user', 'pyth0n')) |
| 21 | +``` |
| 22 | + |
| 23 | +## Executing ad-hoc queries |
| 24 | + |
| 25 | +The [v1/eval REST endpoint](https://docs.marklogic.com/REST/POST/v1/eval) supports the execution of ad-hoc JavaScript |
| 26 | +and XQuery queries. Each type of query can be easily submitted via the client: |
| 27 | + |
| 28 | +``` |
| 29 | +client.eval.javascript("fn.currentDateTime()") |
| 30 | +client.eval.xquery("fn:current-dateTime()") |
| 31 | +``` |
| 32 | + |
| 33 | +Variables can optionally be provided via a `dict`: |
| 34 | + |
| 35 | +``` |
| 36 | +results = client.eval.javascript('Sequence.from([{"hello": myValue}])', vars={"myValue": "world"}) |
| 37 | +assert "world" == results[0]["hello"] |
| 38 | +``` |
| 39 | + |
| 40 | +Because the REST endpoint returns a sequence of items, the client will always return a list of values. See the section |
| 41 | +below on how data types are converted to understand how the client will convert each value into an appropriate Python |
| 42 | +data type. |
| 43 | + |
| 44 | +## Invoking modules |
| 45 | + |
| 46 | +The [v1/invoke REST endpoint](https://docs.marklogic.com/REST/POST/v1/invoke) supports the execution of JavaScript |
| 47 | +and XQuery main modules that have been deployed to your application's modules database. A module can be invoked via |
| 48 | +the client in the following manner: |
| 49 | + |
| 50 | +``` |
| 51 | +client.invoke("/path/to/module.sjs") |
| 52 | +``` |
| 53 | + |
| 54 | + |
| 55 | +## Conversion of data types |
| 56 | + |
| 57 | +The REST endpoints for evaluating ad-hoc code and for invoking a module both return a sequence of values, with each |
| 58 | +value having MarkLogic-specific type information. The client will use this type information to convert each value into |
| 59 | +an appropriate Python data type. For example, each JSON object into the example below is converted into a `dict`: |
| 60 | + |
| 61 | +``` |
| 62 | +results = client.eval.javascript('Sequence.from([{"doc": 1}, {"doc": 2}])') |
| 63 | +assert len(results) == 2 |
| 64 | +assert results[0]["doc"] == 1 |
| 65 | +assert results[1]["doc"] == 2 |
| 66 | +``` |
| 67 | + |
| 68 | +The following table describes how each MarkLogic type is associated with a Python data type. For any |
| 69 | +MarkLogic type not listed in the table, the value is not converted and will be of type `bytes`. |
| 70 | + |
| 71 | +| MarkLogic type | Python type | |
| 72 | +| --- | --- | |
| 73 | +| string | str | |
| 74 | +| integer | int | |
| 75 | +| boolean | bool | |
| 76 | +| decimal | [Decimal](https://docs.python.org/3/library/decimal.html) | |
| 77 | +| map | dict | |
| 78 | +| element() | str | |
| 79 | +| array | list | |
| 80 | +| array-node() | list | |
| 81 | +| object-node() | dict or marklogic.documents.Document | |
| 82 | +| document-node() | str or marklogic.documents.Document | |
| 83 | +| binary() | marklogic.documents.Document | |
| 84 | + |
| 85 | +For the `object-node()` and `document-node()` entries in the above table, a `marklogic.documents.Document` instance |
| 86 | +will be returned if the MarkLogic value is associated with a URI via the multipart `X-URI` header. Otherwise, a `dict` |
| 87 | +or `str` is returned respectively. |
| 88 | + |
| 89 | +## Returning the original HTTP response |
| 90 | + |
| 91 | +Each `client.eval` method and `client.invoke` accept a `return_response` argument. When that |
| 92 | +argument is set to `True`, the original response is returned. This can be useful for custom |
| 93 | +processing of the response or debugging requests. |
0 commit comments