-
Notifications
You must be signed in to change notification settings - Fork 4
DEVEXP-496 Can now write a batch of documents #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import json | ||
| from requests import Session | ||
| from urllib3.fields import RequestField | ||
| from urllib3.filepost import encode_multipart_formdata | ||
|
|
||
|
|
||
| class Document: | ||
| """ | ||
| :param uri: the URI of the document; can be None when relying on MarkLogic to | ||
| generate a URI. | ||
| :param content: the content of the document. | ||
| :param content_type: the MIME type of the document; use when MarkLogic cannot | ||
| determine the MIME type based on the URI. | ||
| :param extension: specifies a suffix for a URI generated by MarkLogic. | ||
| :param directory: specifies a prefix for a URI generated by MarkLogic. | ||
| :param repair: for an XML document, the level of XML repair to perform; can be | ||
| "full" or "none", with "none" being the default. | ||
| :param version_id: affects updates when optimistic locking is enabled; see | ||
| https://docs.marklogic.com/REST/POST/v1/documents for more information. | ||
| :param temporal_document: the logical document URI for a document written to a | ||
| temporal collection; requires that a "temporal-collection" parameter be included in | ||
| the request. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| uri: str, | ||
| content, | ||
| content_type: str = None, | ||
| extension: str = None, | ||
| directory: str = None, | ||
| repair: str = None, | ||
| extract: str = None, | ||
| version_id: str = None, | ||
| temporal_document: str = None, | ||
| ): | ||
| self.uri = uri | ||
| self.content = content | ||
| self.content_type = content_type | ||
| self.extension = extension | ||
| self.directory = directory | ||
| self.repair = repair | ||
| self.extract = extract | ||
| self.version_id = version_id | ||
| self.temporal_document = temporal_document | ||
|
|
||
| def to_request_field(self) -> RequestField: | ||
| data = self.content | ||
| if type(data) is dict: | ||
| data = json.dumps(data) | ||
| field = RequestField(name=self.uri, data=data, filename=self.uri) | ||
| field.make_multipart( | ||
| content_disposition=self._make_disposition(), | ||
| content_type=self.content_type, | ||
| ) | ||
| return field | ||
|
|
||
| def _make_disposition(self) -> str: | ||
| disposition = "attachment" | ||
|
|
||
| if not self.uri: | ||
| disposition = "inline" | ||
| if self.extension: | ||
| disposition = f"{disposition};extension={self.extension}" | ||
| if self.directory: | ||
| disposition = f"{disposition};directory={self.directory}" | ||
|
|
||
| if self.repair: | ||
| disposition = f"{disposition};repair={self.repair}" | ||
|
|
||
| if self.extract: | ||
| disposition = f"{disposition};extract={self.extract}" | ||
|
|
||
| if self.version_id: | ||
| disposition = f"{disposition};versionId={self.version_id}" | ||
|
|
||
| if self.temporal_document: | ||
| disposition = f"{disposition};temporal-document={self.temporal_document}" | ||
|
|
||
| return disposition | ||
|
|
||
|
|
||
| class DocumentManager: | ||
| def __init__(self, session: Session): | ||
| self._session = session | ||
|
|
||
| def write(self, documents: list[Document], **kwargs): | ||
| fields = [self._make_default_metadata_field()] | ||
| for doc in documents: | ||
| fields.append(doc.to_request_field()) | ||
|
|
||
| data, content_type = encode_multipart_formdata(fields) | ||
|
|
||
| headers = kwargs.pop("headers", {}) | ||
| headers["Content-Type"] = "".join( | ||
| ("multipart/mixed",) + content_type.partition(";")[1:] | ||
| ) | ||
| if not headers.get("Accept"): | ||
| headers["Accept"] = "application/json" | ||
|
|
||
| return self._session.post("/v1/documents", data=data, headers=headers, **kwargs) | ||
|
|
||
| def _make_default_metadata_field(self): | ||
| """ | ||
| Temporary method to ensure the test user can see written documents. Will be | ||
| removed when this feature is implemented for real. | ||
| """ | ||
| metadata_field = RequestField( | ||
| name="request-metadata", | ||
| data=json.dumps( | ||
| { | ||
| "permissions": [ | ||
| { | ||
| "role-name": "python-tester", | ||
| "capabilities": ["read", "update"], | ||
| } | ||
| ] | ||
| } | ||
| ), | ||
| ) | ||
| metadata_field.make_multipart( | ||
| content_disposition="inline; category=metadata", | ||
| content_type="application/json", | ||
| ) | ||
| return metadata_field | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| .gradle | ||
| gradle-local.properties | ||
| build |
23 changes: 23 additions & 0 deletions
23
test-app/src/main/ml-config/security/roles/python-tester.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| { | ||
| "role-name": "python-tester", | ||
| "role": [ | ||
| "rest-extension-user" | ||
| ], | ||
| "privilege": [ | ||
| { | ||
| "privilege-name": "rest-reader", | ||
| "action": "http://marklogic.com/xdmp/privileges/rest-reader", | ||
| "kind": "execute" | ||
| }, | ||
| { | ||
| "privilege-name": "rest-writer", | ||
| "action": "http://marklogic.com/xdmp/privileges/rest-writer", | ||
| "kind": "execute" | ||
| }, | ||
| { | ||
| "privilege-name": "xdbc:eval", | ||
| "action": "http://marklogic.com/xdmp/privileges/xdbc-eval", | ||
| "kind": "execute" | ||
| } | ||
| ] | ||
| } |
7 changes: 7 additions & 0 deletions
7
test-app/src/main/ml-config/security/users/python-test-admin.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "user-name": "python-test-admin", | ||
| "password": "password", | ||
| "role": [ | ||
| "admin" | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
test-app/src/main/ml-config/temporal/axes/temporal-system-axis.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "axis-name": "system", | ||
| "axis-start": { | ||
| "element-reference": { | ||
| "namespace-uri": "", | ||
| "localname": "systemStart" | ||
| } | ||
| }, | ||
| "axis-end": { | ||
| "element-reference": { | ||
| "namespace-uri": "", | ||
| "localname": "systemEnd" | ||
| } | ||
| } | ||
| } |
15 changes: 15 additions & 0 deletions
15
test-app/src/main/ml-config/temporal/axes/temporal-valid-axis.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "axis-name": "valid", | ||
| "axis-start": { | ||
| "element-reference": { | ||
| "namespace-uri": "", | ||
| "localname": "validStart" | ||
| } | ||
| }, | ||
| "axis-end": { | ||
| "element-reference": { | ||
| "namespace-uri": "", | ||
| "localname": "validEnd" | ||
| } | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
test-app/src/main/ml-config/temporal/collections/temporal-collection.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "collection-name": "temporal-collection", | ||
| "system-axis": "system", | ||
| "valid-axis": "valid", | ||
| "option": [ | ||
| "updates-admin-override" | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| *=rest-reader,read,rest-writer,update | ||
| *=python-tester,read,python-tester,update |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "update-policy": "VERSION_OPTIONAL" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thought on this design - we should be able to reuse this class for reading documents - i.e. getting back an array of Document objects. But the args starting with content_type are all specific to writing a set of documents, and they won't be populated when reading a document. We could capture that in the docstring for the constructor, but I think they're still potentially confusing in that they're only for writing a document.
An alternative would be to use
**kwargsand clearly document what keyword args can be passed in based on what you plan to do with a Document. That might be worth changing to after doing the "Read documents" story.