|
| 1 | +""" |
| 2 | +Edit Document Example |
| 3 | +
|
| 4 | +This example demonstrates how to edit a document's title using the editDocument API. |
| 5 | +It shows the complete workflow: |
| 6 | +1. Creating a new document |
| 7 | +2. Editing the document title |
| 8 | +3. Verifying the change |
| 9 | +""" |
| 10 | + |
| 11 | +from datetime import datetime |
| 12 | +from vaiz import VaizClient, CreateDocumentRequest, EditDocumentRequest, GetDocumentsRequest, Kind |
| 13 | +from config import API_KEY, SPACE_ID |
| 14 | + |
| 15 | + |
| 16 | +def main(): |
| 17 | + # Initialize client |
| 18 | + client = VaizClient(api_key=API_KEY, space_id=SPACE_ID, verify_ssl=False) |
| 19 | + |
| 20 | + print("=== Edit Document Example ===\n") |
| 21 | + |
| 22 | + # 1. Create a new document |
| 23 | + print("1. Creating a new document...") |
| 24 | + original_title = f"Test Document - {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}" |
| 25 | + |
| 26 | + create_request = CreateDocumentRequest( |
| 27 | + kind=Kind.Space, |
| 28 | + kind_id=SPACE_ID, |
| 29 | + title=original_title, |
| 30 | + index=0 |
| 31 | + ) |
| 32 | + |
| 33 | + create_response = client.create_document(create_request) |
| 34 | + document = create_response.payload.document |
| 35 | + |
| 36 | + print(f"✅ Created document:") |
| 37 | + print(f" ID: {document.id}") |
| 38 | + print(f" Title: {document.title}") |
| 39 | + print(f" Kind: {document.kind}") |
| 40 | + print(f" Size: {document.size} bytes\n") |
| 41 | + |
| 42 | + # 2. Edit document title |
| 43 | + print("2. Editing document title...") |
| 44 | + new_title = f"Updated Document - {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}" |
| 45 | + |
| 46 | + edit_request = EditDocumentRequest( |
| 47 | + document_id=document.id, |
| 48 | + title=new_title |
| 49 | + ) |
| 50 | + |
| 51 | + edit_response = client.edit_document(edit_request) |
| 52 | + edited_document = edit_response.payload.document |
| 53 | + |
| 54 | + print(f"✅ Edited document:") |
| 55 | + print(f" ID: {edited_document.id}") |
| 56 | + print(f" Old Title: {original_title}") |
| 57 | + print(f" New Title: {edited_document.title}") |
| 58 | + print(f" Updated At: {edited_document.updated_at}\n") |
| 59 | + |
| 60 | + # 3. Verify the change |
| 61 | + print("3. Verifying title change in document list...") |
| 62 | + list_request = GetDocumentsRequest( |
| 63 | + kind=Kind.Space, |
| 64 | + kind_id=SPACE_ID |
| 65 | + ) |
| 66 | + |
| 67 | + list_response = client.get_documents(list_request) |
| 68 | + |
| 69 | + # Find our document in the list |
| 70 | + found_document = None |
| 71 | + for doc in list_response.payload.documents: |
| 72 | + if doc.id == document.id: |
| 73 | + found_document = doc |
| 74 | + break |
| 75 | + |
| 76 | + if found_document: |
| 77 | + print(f"✅ Document found in list:") |
| 78 | + print(f" ID: {found_document.id}") |
| 79 | + print(f" Title: {found_document.title}") |
| 80 | + print(f" Title matches: {found_document.title == new_title}") |
| 81 | + else: |
| 82 | + print("❌ Document not found in list") |
| 83 | + |
| 84 | + print("\n=== Example completed successfully! ===") |
| 85 | + |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + main() |
| 89 | + |
0 commit comments