Skip to content

Commit

Permalink
chore: add aiohttp and aiohttp-sse-client as optional dependencies. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
overcat authored Aug 27, 2023
1 parent f0b012d commit 4202abf
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/continuous-integration-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ jobs:
run: |
while true; do response=$(curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1:9876/get); if [ "$response" = "200" ]; then echo "HTTP 200 OK received"; break; else echo "Retrying..."; sleep 1; fi; done
- name: Echo installed packages
run: |
pip freeze
- name: Test with pytest
run: poetry run pytest -v -rs tests --runslow --cov=./ --cov-report=xml

Expand Down
7 changes: 7 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ Installing
pip install -U stellar-sdk
If you need to use asynchronous, please install it using the following method.

.. code-block:: text
pip install -U stellar-sdk[aiohttp]
We follow `Semantic Versioning 2.0.0 <https://semver.org/>`_, and I strongly
recommend that you specify its major version number in the dependency
file to avoid the unknown effects of breaking changes.
Expand Down
7 changes: 5 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,18 @@ classifiers = [
python = ">=3.7,<4.0"
PyNaCl = "^1.4.0"
requests = "^2.26.0"
aiohttp = "^3.8.1"
aiohttp-sse-client = "^0.2.1"
aiohttp = { version = "^3.8.1", optional = true}
aiohttp-sse-client = { version = "^0.2.1", optional = true}
stellar-base-sseclient = "^0.0.21"
mnemonic = "^0.20"
toml = "^0.10.2"
urllib3 = "^1.26.7"
pydantic = "^2.3.0"
xdrlib3 = "^0.1.1"

[tool.poetry.extras]
aiohttp = ["aiohttp", "aiohttp-sse-client"]

[tool.poetry.dev-dependencies]
pytest = "^7.3.1"
pytest-cov = "^4.0.0"
Expand Down
9 changes: 7 additions & 2 deletions stellar_sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
)
from .account import *
from .asset import *
from .client.aiohttp_client import AiohttpClient
from .client.requests_client import RequestsClient
from .decorated_signature import *
from .fee_bump_transaction import *
Expand All @@ -28,7 +27,6 @@
from .preconditions import *
from .price import *
from .server import *
from .server_async import *
from .signer import *
from .signer_key import *
from .soroban_data_builder import *
Expand All @@ -38,3 +36,10 @@
from .transaction import *
from .transaction_builder import *
from .transaction_envelope import *

# aiohttp required
try:
from .client.aiohttp_client import AiohttpClient
from .server_async import *
except ImportError:
pass
17 changes: 13 additions & 4 deletions stellar_sdk/client/aiohttp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
import logging
from typing import Any, AsyncGenerator, Dict, Optional

import aiohttp
from aiohttp_sse_client.client import EventSource

from ..__version__ import __version__
from ..exceptions import ConnectionError, StreamClientError
from . import defines
Expand Down Expand Up @@ -58,7 +55,14 @@ async def __readline(self) -> bytes:
return b"".join(line)


aiohttp.streams.StreamReader.readline = __readline # type: ignore[assignment]
_AIOHTTP_DEPS_INSTALLED = True
try:
import aiohttp
from aiohttp_sse_client.client import EventSource

aiohttp.streams.StreamReader.readline = __readline # type: ignore[assignment]
except ImportError:
_AIOHTTP_DEPS_INSTALLED = False


class AiohttpClient(BaseAsyncClient):
Expand All @@ -83,6 +87,11 @@ def __init__(
custom_headers: Optional[Dict[str, str]] = None,
**kwargs,
) -> None:
if not _AIOHTTP_DEPS_INSTALLED:
raise ImportError(
"The required dependencies have not been installed. "
"Please install `stellar-sdk[aiohttp]` to use this feature."
)
self.pool_size = pool_size
self.backoff_factor: Optional[float] = backoff_factor
self.request_timeout: float = request_timeout
Expand Down

0 comments on commit 4202abf

Please sign in to comment.