Skip to content
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

Allow Service URl as base URL #256

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ from atproto import Client

client = Client()
# By default, it uses the server of bsky.app. To change this behavior, pass the base api URL to constructor
# Client('https://my.awesome.server/xrpc')
# Client('https://example.com')
```

For async:
Expand All @@ -107,7 +107,7 @@ from atproto import AsyncClient

client = AsyncClient()
# By default, it uses the server of bsky.app. To change this behavior, pass the base api URL to constructor
# AsyncClient('https://my.awesome.server/xrpc')
# AsyncClient('https://example.com')
```

In the snippets below, only the sync version will be presented.
Expand Down
4 changes: 2 additions & 2 deletions docs/source/readme.content.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ from atproto import Client

client = Client()
# By default, it uses the server of bsky.app. To change this behavior, pass the base api URL to constructor
# Client('https://my.awesome.server/xrpc')
# Client('https://example.com')
```

For async:
Expand All @@ -31,7 +31,7 @@ from atproto import AsyncClient

client = AsyncClient()
# By default, it uses the server of bsky.app. To change this behavior, pass the base api URL to constructor
# AsyncClient('https://my.awesome.server/xrpc')
# AsyncClient('https://example.com')
```

In the snippets below, only the sync version will be presented.
Expand Down
18 changes: 12 additions & 6 deletions packages/atproto_client/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,25 @@ def _handle_kwagrs(kwargs: dict) -> None:
kwargs.pop('output_encoding', None)


def _handle_base_url(base_url: t.Optional[str] = None) -> str:
if base_url is None:
return _BASE_API_URL

if not base_url.endswith('/xrpc'):
return f'{base_url.rstrip("/")}/xrpc'

return base_url


class ClientBase:
"""Low-level methods are here."""

def __init__(self, base_url: t.Optional[str] = None, request: t.Optional[Request] = None) -> None:
if request is None:
request = Request()
if base_url is None:
base_url = _BASE_API_URL

self._request = request
self._base_url = base_url
self._base_url = _handle_base_url(base_url)

@property
def request(self) -> Request:
Expand Down Expand Up @@ -95,11 +103,9 @@ class AsyncClientBase:
def __init__(self, base_url: t.Optional[str] = None, request: t.Optional[AsyncRequest] = None) -> None:
if request is None:
request = AsyncRequest()
if base_url is None:
base_url = _BASE_API_URL

self._request = request
self._base_url = base_url
self._base_url = _handle_base_url(base_url)

@property
def request(self) -> AsyncRequest:
Expand Down
11 changes: 11 additions & 0 deletions tests/test_atproto_client/client/test_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from atproto_client.client.base import _BASE_API_URL, _handle_base_url


def test_handle_base_url() -> None:
service_url = 'https://example.com/xrpc'

assert _handle_base_url() == _BASE_API_URL
assert _handle_base_url(None) == _BASE_API_URL
assert _handle_base_url('https://example.com') == service_url
assert _handle_base_url('https://example.com/') == service_url
assert _handle_base_url('https://example.com/xrpc') == service_url
Loading