Skip to content

Commit

Permalink
feat[confluence]: Permit passing params to Confluence client
Browse files Browse the repository at this point in the history
- This enables use of parameters such as backoff_and_retry which is
important for the client to react well to rate limit (429) responses,
as described in
https://developer.atlassian.com/cloud/confluence/rate-limiting/
For available parameters, refer also to:
https://github.com/atlassian-api/atlassian-python-api/blob/3.41.16/atlassian/rest_client.py#L48
  • Loading branch information
rehevkor5 committed Nov 14, 2024
1 parent 286891a commit cbec60b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ base_url = "https://yoursite.atlassian.com/wiki"
page_ids = ["<page_id_1>", "<page_id_2>", "<page_id_3"]
space_key = "<space_key>"

reader = ConfluenceReader(base_url=base_url, oauth2=oauth2_dict)
reader = ConfluenceReader(
base_url=base_url,
oauth2=oauth2_dict,
client_args={"backoff_and_retry": True},
)
documents = reader.load_data(
space_key=space_key, include_attachments=True, page_status="current"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class ConfluenceReader(BaseReader):
api_token (str): Confluence API token, see https://confluence.atlassian.com/cloud/api-tokens-938839638.html
user_name (str): Confluence username, used for basic auth. Must be used with `password`.
password (str): Confluence password, used for basic auth. Must be used with `user_name`.
client_args (dict): Additional keyword arguments to pass directly to the Atlassian Confluence client constructor, for example `{'backoff_and_retry': True}`.
"""

Expand All @@ -44,6 +45,7 @@ def __init__(
api_token: Optional[str] = None,
user_name: Optional[str] = None,
password: Optional[str] = None,
client_args: Optional[dict] = None,
) -> None:
if base_url is None:
raise ValueError("Must provide `base_url`")
Expand All @@ -58,20 +60,30 @@ def __init__(
" atlassian-python-api`"
)
self.confluence: Confluence = None
if client_args is None:
client_args = {}
if oauth2:
self.confluence = Confluence(url=base_url, oauth2=oauth2, cloud=cloud)
self.confluence = Confluence(
url=base_url, oauth2=oauth2, cloud=cloud, **client_args
)
else:
if api_token is not None:
self.confluence = Confluence(url=base_url, token=api_token, cloud=cloud)
self.confluence = Confluence(
url=base_url, token=api_token, cloud=cloud, **client_args
)
elif user_name is not None and password is not None:
self.confluence = Confluence(
url=base_url, username=user_name, password=password, cloud=cloud
url=base_url,
username=user_name,
password=password,
cloud=cloud,
**client_args,
)
else:
api_token = os.getenv(CONFLUENCE_API_TOKEN)
if api_token is not None:
self.confluence = Confluence(
url=base_url, token=api_token, cloud=cloud
url=base_url, token=api_token, cloud=cloud, **client_args
)
else:
user_name = os.getenv(CONFLUENCE_USERNAME)
Expand All @@ -82,6 +94,7 @@ def __init__(
username=user_name,
password=password,
cloud=cloud,
**client_args,
)
else:
raise ValueError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ license = "MIT"
maintainers = ["zywilliamli"]
name = "llama-index-readers-confluence"
readme = "README.md"
version = "0.2.1"
version = "0.2.2"

[tool.poetry.dependencies]
python = ">=3.8.1,<4.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from unittest.mock import patch

import pytest
from llama_index.readers.confluence import ConfluenceReader

Expand Down Expand Up @@ -31,6 +33,22 @@ def test_confluence_reader_with_api_token():
assert reader.confluence is not None


def test_confluence_reader_with_client_args():
with patch("atlassian.Confluence") as MockConstructor:
reader = ConfluenceReader(
base_url="https://example.atlassian.net/wiki",
api_token="example_api_token",
client_args={"backoff_and_retry": True},
)
assert reader.confluence is not None
MockConstructor.assert_called_once_with(
url="https://example.atlassian.net/wiki",
token="example_api_token",
cloud=True,
backoff_and_retry=True,
)


def test_confluence_reader_with_basic_auth():
reader = ConfluenceReader(
base_url="https://example.atlassian.net/wiki",
Expand Down

0 comments on commit cbec60b

Please sign in to comment.