Skip to content

Commit b2cdac9

Browse files
feat: adding getoutline library
1 parent fc70a4f commit b2cdac9

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed

glueops/getoutline.py

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import requests
2+
import setup_logging
3+
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO")
4+
logger = setup_logging.configure(level=LOG_LEVEL)
5+
6+
class GetOutlineClient:
7+
"""
8+
A client to interact with the Outline API for managing documents.
9+
"""
10+
11+
def __init__(self, api_url, document_id, api_token):
12+
"""
13+
Initializes the GetOutlineClient with the necessary API credentials.
14+
15+
:param api_url: The base URL for the Outline API.
16+
:param document_id: The ID of the document to manage.
17+
:param api_token: The API token for authentication.
18+
"""
19+
self.api_url = api_url.rstrip('/') # Ensure no trailing slash
20+
self.document_id = document_id
21+
self.api_token = api_token
22+
self.headers = {
23+
"Content-Type": "application/json",
24+
"Authorization": f"Bearer {self.api_token}"
25+
}
26+
27+
def update_document(self, markdown_text):
28+
"""
29+
Updates the content of the specified document with new markdown text.
30+
31+
:param markdown_text: The new markdown text to update the document with.
32+
"""
33+
logger.debug("Updating document on Outline.")
34+
url = f"{self.api_url}/api/documents.update"
35+
payload = {
36+
"id": self.document_id,
37+
"text": markdown_text
38+
}
39+
40+
try:
41+
response = requests.post(url, json=payload, headers=self.headers)
42+
response.raise_for_status()
43+
logger.info(f"Document update response code: {response.status_code}")
44+
except requests.exceptions.RequestException as e:
45+
logger.error(f"Error updating document: {e}")
46+
47+
def get_document_uuid(self):
48+
"""
49+
Retrieves the UUID of the parent document.
50+
51+
:return: The UUID of the parent document or None if an error occurs.
52+
"""
53+
url = f"{self.api_url}/api/documents.info"
54+
payload = {
55+
"id": self.document_id
56+
}
57+
58+
try:
59+
response = requests.post(url, json=payload, headers=self.headers)
60+
response.raise_for_status()
61+
parent_id = response.json().get("data", {}).get("id")
62+
logger.debug(f"Parent document UUID: {parent_id}")
63+
return parent_id
64+
except requests.exceptions.RequestException as e:
65+
logger.error(f"Error getting parent document UUID: {e}")
66+
return None
67+
68+
def get_children_documents_to_delete(self, parent_document_id):
69+
"""
70+
Retrieves a list of child document IDs under the specified parent document.
71+
72+
:param parent_document_id: The UUID of the parent document.
73+
:return: A list of child document IDs.
74+
"""
75+
url = f"{self.api_url}/api/documents.list"
76+
payload = {
77+
"parentDocumentId": parent_document_id
78+
}
79+
80+
try:
81+
response = requests.post(url, json=payload, headers=self.headers)
82+
response.raise_for_status()
83+
child_docs = response.json().get("data", [])
84+
ids = [doc.get("id") for doc in child_docs if "id" in doc]
85+
logger.debug(f"Child document IDs to delete: {ids}")
86+
return ids
87+
except requests.exceptions.RequestException as e:
88+
logger.error(f"Error getting children documents: {e}")
89+
return []
90+
91+
def delete_document(self, document_id):
92+
"""
93+
Deletes a document with the specified document ID.
94+
95+
:param document_id: The ID of the document to delete.
96+
:return: True if deletion was successful, False otherwise.
97+
"""
98+
url = f"{self.api_url}/api/documents.delete"
99+
payload = {
100+
"id": document_id
101+
}
102+
103+
try:
104+
response = requests.post(url, json=payload, headers=self.headers)
105+
response.raise_for_status()
106+
logger.debug(f"Successfully deleted document with ID: {document_id}")
107+
return True
108+
except requests.exceptions.RequestException as e:
109+
logger.error(f"Error deleting document {document_id}: {e}")
110+
return False
111+
112+
def create_document(self, parent_document_id, title, text):
113+
"""
114+
Creates a new document under the specified parent document.
115+
116+
:param parent_document_id: The UUID of the parent document.
117+
:param title: The title of the new document.
118+
:param text: The markdown text content of the new document.
119+
:return: True if creation was successful, False otherwise.
120+
"""
121+
url = f"{self.api_url}/api/documents.create"
122+
payload = {
123+
"parentDocumentId": parent_document_id,
124+
"title": title,
125+
"text": text,
126+
"publish": True
127+
}
128+
129+
try:
130+
response = requests.post(url, json=payload, headers=self.headers)
131+
response.raise_for_status()
132+
logger.info(f"Successfully created document with title: {title}")
133+
return True
134+
except requests.exceptions.RequestException as e:
135+
logger.error(f"Error creating document '{title}': {e}")
136+
return False

0 commit comments

Comments
 (0)