def put(self, path: str, document: Document, force_replace: bool = False) -> DocumentFamily: """ Args: path (str): The path to the document family document (Document): The document you wish to upload force_replace (bool): True if you want to delete the family in this path first Returns: The new document family instance """ # We can only add a document if it doesn't already exist as a family if self.get_family_by_path(path) is None: new_document_family = DocumentFamily(path=path) new_event = self.add_document(new_document_family, document) document.to_kdxa( os.path.join(self.store_path, new_event.content_object.id) + ".kdxa") self.metastore.append(new_document_family) self.write_metastore() # Notify the listeners self.notify_listeners(new_event) document_family = self.get_family_by_path(path) if document_family is not None: return document_family raise Exception("Unable to get document family?")
def put_native(self, path: str, content) -> DocumentFamily: from kodexa import KodexaPlatform try: logger.info(f"Putting native content to path {path}") files = {"file": content} document_family_response = requests.post( f"{KodexaPlatform.get_url()}/api/stores/{self.ref.replace(':', '/')}/fs", params={ "path": path, "document": False }, headers={"x-access-token": KodexaPlatform.get_access_token()}, files=files) if document_family_response.status_code == 200: return DocumentFamily.parse_obj( document_family_response.json()) msg = "Document family create failed [" + document_family_response.text + "], response " + str( document_family_response.status_code) logger.warning(msg) raise Exception(msg) except JSONDecodeError: logger.warning("Unable to decode the JSON response") raise
def replace_content_object(self, document_family: DocumentFamily, content_object_id: str, document: Document) -> DocumentFamily: from kodexa import KodexaPlatform try: logger.info( f"Replacing document in family {document_family.id} content object {content_object_id}" ) files = {"file": document.to_kddb()} content_object_replace = requests.put( f"{KodexaPlatform.get_url()}/api/stores/{self.ref.replace(':', '/')}/families/{document_family.id}/objects/{content_object_id}/content", headers={"x-access-token": KodexaPlatform.get_access_token()}, files=files) if content_object_replace.status_code == 200: return DocumentFamily.parse_obj(content_object_replace.json()) msg = "Document replace failed [" + content_object_replace.text + "], response " + str( content_object_replace.status_code) logger.warning(msg) raise Exception(msg) except JSONDecodeError: logger.warning("Unable to decode the JSON response") raise
def get_family_by_path(self, path: str) -> Optional[DocumentFamily]: from kodexa import KodexaPlatform get_response = KodexaPlatform.get_client().get( f"api/stores/{self.ref.replace(':', '/')}/fs", params={ "path": path, "meta": True }) return DocumentFamily.parse_obj( get_response.json()) if get_response is not None else None
def put_native(self, path: str, content: Any, force_replace=False): """ Args: path (str): The path to the native file content (Any): The content to store force_replace (bool): Replace the object in the store Returns: """ # In order to store a native document we will first get the family # then we will create a content object for the native object # and also a content object for the document that references it family = self.get_family_by_path(path) if family is None: family = DocumentFamily(path=path) self.metastore.append(family) native_content_object = ContentObject(**{'contentType': 'NATIVE'}) native_content_object.id = str(uuid.uuid4()).replace("-", "") native_content_object.created_on = datetime.now() if family.content_objects is None: family.content_objects = [] family.content_objects.append(native_content_object) with open(os.path.join(self.store_path, native_content_object.id), 'wb') as file: file.write(content) document = Document() document.source.connector = "document-store" document.source.headers = { "ref": family.store_ref, "family": family.id, "id": native_content_object.id } content_event = self.add_document(family, document) document.to_kdxa( os.path.join(self.store_path, content_event.content_object.id) + ".kdxa")
def put(self, path: str, content, replace=False) -> DocumentFamily: """Put the content into the model store at the given path Args: path: The path you wish to put the content at content: The content for that object replace: Replace the content if it exists Returns: the document family that was created """ from kodexa import KodexaPlatform import requests try: files = {"file": content} if replace: delete_response = requests.delete( f"{KodexaPlatform.get_url()}/api/stores/{self.ref.replace(':', '/')}/fs", params={"path": path}, headers={ "x-access-token": KodexaPlatform.get_access_token() }) logger.info(f"Deleting {path} ({delete_response.status_code})") content_object_response = requests.post( f"{KodexaPlatform.get_url()}/api/stores/{self.ref.replace(':', '/')}/fs", params={"path": path}, headers={"x-access-token": KodexaPlatform.get_access_token()}, files=files) logger.info( f"Uploaded {path} ({content_object_response.status_code})") if content_object_response.status_code == 200: return DocumentFamily.parse_obj(content_object_response.json()) if content_object_response.status_code == 400: from addict import Dict bad_request = Dict(json.loads(content_object_response.text)) for error_key in bad_request.errors.keys(): print(bad_request.errors[error_key] + " (" + error_key + ")") raise Exception("Invalid request") msg = "Execution creation failed [" + content_object_response.text + "], response " + str( content_object_response.status_code) logger.warning(msg) raise Exception(msg) except JSONDecodeError: logger.warning("Unable to JSON decode the response?") raise
def get_family(self, document_family_id: str) -> Optional[DocumentFamily]: from kodexa import KodexaPlatform try: logger.info(f"Getting document family id {document_family_id}") document_family_response = requests.get( f"{KodexaPlatform.get_url()}/api/stores/{self.ref.replace(':', '/')}/families/{document_family_id}", headers={"x-access-token": KodexaPlatform.get_access_token()}) if document_family_response.status_code == 200: return DocumentFamily.parse_obj( document_family_response.json()) msg = "Get document family failed [" + document_family_response.text + "], response " + str( document_family_response.status_code) logger.warning(msg) raise Exception(msg) except JSONDecodeError: logger.warning("Unable to decode the JSON response") raise
def query_families(self, query: str = "*", page: int = 1, page_size: int = 100, sort=None) -> List[DocumentFamily]: params = {'page': page, 'pageSize': page_size, 'query': query} if sort is not None: params.sort = sort from kodexa import KodexaPlatform get_response = KodexaPlatform.get_client().get( f"api/stores/{self.ref.replace(':', '/')}/families", params=params) if get_response is not None: families = [] for fam_dict in get_response.json()['content']: families.append(DocumentFamily.parse_obj(fam_dict)) return families return []
def update_document_family_status(self, document_family, status): from kodexa import KodexaPlatform try: logger.info(f"Updating the status of {document_family.id}") document_family_response = requests.put( f"{KodexaPlatform.get_url()}/api/stores/{self.ref.replace(':', '/')}/families/{document_family.id}/status", headers={ "x-access-token": KodexaPlatform.get_access_token(), "content-type": "application/json" }, data=status.json(by_alias=True)) if document_family_response.status_code == 200: return DocumentFamily(**document_family_response.json()) msg = "Document family update failed [" + document_family_response.text + "], response " + str( document_family_response.status_code) logger.warning(msg) raise Exception(msg) except JSONDecodeError: logger.warning("Unable to decode the JSON response") raise