22from collections import OrderedDict
33from typing import Union
44
5+ from marklogic .transactions import Transaction
56from requests import Response , Session
67from requests_toolbelt .multipart .decoder import MultipartDecoder
78from urllib3 .fields import RequestField
89from urllib3 .filepost import encode_multipart_formdata
910
1011"""
1112Defines classes to simplify usage of the documents REST endpoint defined at
12- https://docs.marklogic.com/REST/client/management.
13+ https://docs.marklogic.com/REST/client/management.
1314"""
1415
1516
@@ -147,7 +148,7 @@ def __init__(
147148 @property
148149 def metadata (self ):
149150 """
150- Returns a dict containing the 5 attributes that comprise the metadata of a
151+ Returns a dict containing the 5 attributes that comprise the metadata of a
151152 document in MarkLogic.
152153 """
153154 return {
@@ -344,7 +345,10 @@ def __init__(self, session: Session):
344345 self ._session = session
345346
346347 def write (
347- self , parts : Union [Document , list [Union [DefaultMetadata , Document ]]], ** kwargs
348+ self ,
349+ parts : Union [Document , list [Union [DefaultMetadata , Document ]]],
350+ tx : Transaction = None ,
351+ ** kwargs ,
348352 ) -> Response :
349353 """
350354 Write one or many documents at a time via a POST to the endpoint defined at
@@ -355,6 +359,7 @@ def write(
355359 after it that does not define its own metadata. See
356360 https://docs.marklogic.com/guide/rest-dev/bulk#id_16015 for more information on
357361 how the REST endpoint uses metadata.
362+ :param tx: if set, the request will be associated with the given transaction.
358363 """
359364 fields = []
360365
@@ -374,17 +379,27 @@ def write(
374379
375380 data , content_type = encode_multipart_formdata (fields )
376381
382+ params = kwargs .pop ("params" , {})
383+ if tx :
384+ params ["txid" ] = tx .id
385+
377386 headers = kwargs .pop ("headers" , {})
378387 headers ["Content-Type" ] = "" .join (
379388 ("multipart/mixed" ,) + content_type .partition (";" )[1 :]
380389 )
381390 if not headers .get ("Accept" ):
382391 headers ["Accept" ] = "application/json"
383392
384- return self ._session .post ("/v1/documents" , data = data , headers = headers , ** kwargs )
393+ return self ._session .post (
394+ "/v1/documents" , data = data , headers = headers , params = params , ** kwargs
395+ )
385396
386397 def read (
387- self , uris : Union [str , list [str ]], categories : list [str ] = None , ** kwargs
398+ self ,
399+ uris : Union [str , list [str ]],
400+ categories : list [str ] = None ,
401+ tx : Transaction = None ,
402+ ** kwargs ,
388403 ) -> Union [list [Document ], Response ]:
389404 """
390405 Read one or many documents via a GET to the endpoint defined at
@@ -395,12 +410,15 @@ def read(
395410 :param categories: optional list of the categories of data to return for each
396411 URI. By default, only content will be returned for each URI. See the endpoint
397412 documentation for further information.
413+ :param tx: if set, the request will be associated with the given transaction.
398414 """
399415 params = kwargs .pop ("params" , {})
400416 params ["uri" ] = uris if isinstance (uris , list ) else [uris ]
401417 params ["format" ] = "json" # This refers to the metadata format.
402418 if categories :
403419 params ["category" ] = categories
420+ if tx :
421+ params ["txid" ] = tx .id
404422
405423 headers = kwargs .pop ("headers" , {})
406424 headers ["Accept" ] = "multipart/mixed"
@@ -423,6 +441,7 @@ def search(
423441 page_length : int = None ,
424442 options : str = None ,
425443 collections : list [str ] = None ,
444+ tx : Transaction = None ,
426445 ** kwargs ,
427446 ) -> Union [list [Document ], Response ]:
428447 """
@@ -442,6 +461,7 @@ def search(
442461 :param page_length: maximum number of documents to return.
443462 :param options: name of a query options instance to use.
444463 :param collections: restrict results to documents in these collections.
464+ :param tx: if set, the request will be associated with the given transaction.
445465 """
446466 params = kwargs .pop ("params" , {})
447467 params ["format" ] = "json" # This refers to the metadata format.
@@ -457,6 +477,8 @@ def search(
457477 params ["pageLength" ] = page_length
458478 if options :
459479 params ["options" ] = options
480+ if tx :
481+ params ["txid" ] = tx .id
460482
461483 headers = kwargs .pop ("headers" , {})
462484 headers ["Accept" ] = "multipart/mixed"
0 commit comments