Skip to content

Commit

Permalink
refactor: Network
Browse files Browse the repository at this point in the history
  • Loading branch information
overcat committed Aug 11, 2019
1 parent 8a52be8 commit 95a60d9
Show file tree
Hide file tree
Showing 20 changed files with 102 additions and 42 deletions.
19 changes: 14 additions & 5 deletions stellar_sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@
from .operation import *
from .operation import __all__ as operation_all

from .account import Account
from .asset import Asset
from .keypair import Keypair
from .memo import Memo, NoneMemo, TextMemo, IdMemo, HashMemo, ReturnHashMemo
from .network import Network, PUBLIC, TESTNET
from .network import Network
from .price import Price
from .server import Server
from .signer import Signer
from .time_bounds import TimeBounds
from .transaction import Transaction
from .transaction_builder import TransactionBuilder
from .transaction_envelope import TransactionEnvelope


__all__ = [
"__title__",
Expand All @@ -27,17 +33,20 @@
"__author__",
"__author_email__",
"__license__",
"Account",
"Asset",
"AsyncServer" "Keypair",
"Keypair",
"Memo",
"NoneMemo",
"TextMemo",
"IdMemo",
"HashMemo",
"ReturnHashMemo",
"Network",
"PUBLIC",
"TESTNET",
"TransactionBuilder",
"Price",
"Server",
"Signer",
"TimeBounds",
"Transaction" "TransactionBuilder",
"TransactionEnvelope",
] + operation_all
2 changes: 2 additions & 0 deletions stellar_sdk/account.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from .strkey import StrKey

__all__ = ["Account"]


class Account:
"""The :class:`Account` object, which represents represents a single
Expand Down
2 changes: 2 additions & 0 deletions stellar_sdk/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from .xdr import Xdr
from .strkey import StrKey

__all__ = ["Asset"]


class Asset:
"""The :class:`Asset` object, which represents an asset and its
Expand Down
24 changes: 8 additions & 16 deletions stellar_sdk/call_builder/base_call_builder.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
from typing import (
Union,
Coroutine,
Any,
Dict,
Mapping,
Generator,
AsyncGenerator,
)
from typing import Union, Coroutine, Any, Dict, Mapping, Generator, AsyncGenerator
from urllib.parse import urljoin

from ..client.base_async_client import BaseAsyncClient
Expand All @@ -25,7 +17,7 @@ class BaseCallBuilder:
"""

def __init__(
self, horizon_url: str, client: Union[BaseAsyncClient, BaseSyncClient]
self, horizon_url: str, client: Union[BaseAsyncClient, BaseSyncClient]
) -> None:

self.__async: bool = False
Expand Down Expand Up @@ -61,7 +53,7 @@ async def __call_async(self) -> Dict[str, Any]:
return resp.json()

def stream(
self
self
) -> Union[
AsyncGenerator[Dict[str, Any], None], Generator[Dict[str, Any], None, None]
]:
Expand Down Expand Up @@ -132,7 +124,7 @@ def _add_query_param(self, key: str, value: Union[str, float, int, bool, None]):
self.params[key] = str(value)

def _add_query_params(
self, params: Mapping[str, Union[str, float, int, bool, None]]
self, params: Mapping[str, Union[str, float, int, bool, None]]
) -> None:
for k, v in params.items():
self._add_query_param(k, v)
Expand All @@ -141,8 +133,8 @@ def __eq__(self, other: object) -> bool:
if not isinstance(other, self.__class__):
return NotImplemented
return (
self.client == other.client
and self.params == other.params
and self.endpoint == other.endpoint
and self.horizon_url == other.horizon_url
self.client == other.client
and self.params == other.params
and self.endpoint == other.endpoint
and self.horizon_url == other.horizon_url
)
2 changes: 2 additions & 0 deletions stellar_sdk/client/aiohttp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"X-Client-Version": __version__,
}

__all__ = ["AiohttpClient"]


class AiohttpClient(BaseAsyncClient):
def __init__(
Expand Down
2 changes: 2 additions & 0 deletions stellar_sdk/client/requests_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
"X-Client-Version": __version__,
}

__all__ = ["RequestsClient"]


class RequestsClient(BaseSyncClient):
def __init__(
Expand Down
2 changes: 2 additions & 0 deletions stellar_sdk/client/response.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import json

__all__ = ["Response"]


class Response:
"""The :class:`Response <Response>` object, which contains a
Expand Down
2 changes: 2 additions & 0 deletions stellar_sdk/client/simple_requests_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"User-Agent": USER_AGENT,
}

__all__ = ["SimpleRequestsClient"]


class SimpleRequestsClient(BaseSyncClient):
def get(self, url: str, params: Dict[str, str] = None) -> Response:
Expand Down
22 changes: 22 additions & 0 deletions stellar_sdk/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@
BuildInValueError = ValueError
BuildInTypeError = TypeError

__all__ = [
"SdkError",
"ValueError",
"TypeError",
"BadSignatureError",
"Ed25519PublicKeyInvalidError",
"Ed25519SecretSeedInvalidError",
"MissingEd25519SecretSeedError",
"MemoInvalidException",
"AssetCodeInvalidError",
"AssetIssuerInvalidError",
"NoApproximationError",
"SignatureExistError",
"BaseRequestError",
"ConnectionError",
"NotFoundError",
"BadRequestError",
"BadResponseError",
"UnknownRequestError",
]


class SdkError(Exception):
"""Base exception for all stellar sdk related errors
Expand Down Expand Up @@ -147,6 +168,7 @@ class UnknownRequestError(BaseHorizonError):
"""


def raise_request_exception(response: Response) -> None:
status_code = response.status_code
if status_code == 200:
Expand Down
2 changes: 2 additions & 0 deletions stellar_sdk/keypair.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from .strkey import StrKey
from .xdr import Xdr

__all__ = ["Keypair"]


class Keypair:
"""The :class:`Keypair` object, which represents a signing and
Expand Down
2 changes: 2 additions & 0 deletions stellar_sdk/memo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from .exceptions import MemoInvalidException
from .xdr import Xdr

__all__ = ["Memo", "NoneMemo", "TextMemo", "IdMemo", "HashMemo", "ReturnHashMemo"]


class Memo(object, metaclass=abc.ABCMeta):
"""The :class:`Memo` object, which represents the base class for memos for
Expand Down
37 changes: 24 additions & 13 deletions stellar_sdk/network.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from .utils import sha256

__all__ = ["Network"]


class Network:
"""The :class:`Network` object, which represents a Stellar network.
Expand All @@ -11,6 +13,12 @@ class Network:
"""

PUBLIC_NETWORK_PASSPHRASE: str = "Public Global Stellar Network ; September 2015"
"""Get the Public network passphrase."""

TESTNET_NETWORK_PASSPHRASE: str = "Test SDF Network ; September 2015"
"""Get the Test network passphrase."""

def __init__(self, network_passphrase: str) -> None:
self.network_passphrase = network_passphrase

Expand All @@ -22,20 +30,23 @@ def network_id(self) -> bytes:
"""
return sha256(self.network_passphrase.encode())

def __eq__(self, other: object) -> bool:
if not isinstance(other, self.__class__):
return NotImplemented
return self.network_passphrase == other.network_passphrase

@classmethod
def public_network(cls) -> "Network":
"""Get the :class:`Network` object representing the PUBLIC Network.
PUBLIC_NETWORK_PASSPHRASE: str = "Public Global Stellar Network ; September 2015"
"""Get the Public network passphrase."""
:return: PUBLIC Network
"""
return cls(cls.PUBLIC_NETWORK_PASSPHRASE)

TESTNET_NETWORK_PASSPHRASE: str = "Test SDF Network ; September 2015"
"""Get the Test network passphrase."""
@classmethod
def testnet_network(cls) -> "Network":
"""Get the :class:`Network` object representing the TESTNET Network.
PUBLIC: Network = Network(PUBLIC_NETWORK_PASSPHRASE)
"""Get the :class:`Network` representing the PUBLIC Network."""
:return: TESTNET Network
"""
return cls(cls.TESTNET_NETWORK_PASSPHRASE)

TESTNET: Network = Network(TESTNET_NETWORK_PASSPHRASE)
"""Get the :class:`Network` representing the Test Network."""
def __eq__(self, other: object) -> bool:
if not isinstance(other, self.__class__):
return NotImplemented
return self.network_passphrase == other.network_passphrase
2 changes: 2 additions & 0 deletions stellar_sdk/price.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from .xdr import Xdr
from .utils import best_rational_approximation

__all__ = ["Price"]


class Price:
"""Create a new price. Price in Stellar is represented as a fraction.
Expand Down
4 changes: 2 additions & 2 deletions stellar_sdk/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ def submit_transaction(
return self.__submit_async(url, data)
return self.__submit_sync(url, data)

def __submit_sync(self, url, data):
def __submit_sync(self, url: str, data: Dict[str, str]) -> Dict[str, Any]:
resp = self._client.post(url=url, data=data)
raise_request_exception(resp)
return resp.json()

async def __submit_async(self, url, data):
async def __submit_async(self, url: str, data: Dict[str, str]) -> Dict[str, Any]:
resp = await self._client.post(url=url, data=data)
raise_request_exception(resp)
return resp.json()
Expand Down
2 changes: 2 additions & 0 deletions stellar_sdk/signer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from .strkey import StrKey
from .xdr import Xdr

__all__ = ["Signer"]


class Signer:
"""The :class:`Signer` object, which represents an account signer on Stellar's network.
Expand Down
2 changes: 2 additions & 0 deletions stellar_sdk/time_bounds.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from .xdr import Xdr

__all__ = ["TimeBounds"]


class TimeBounds:
"""TimeBounds represents the time interval that a transaction is valid.
Expand Down
2 changes: 2 additions & 0 deletions stellar_sdk/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from .time_bounds import TimeBounds
from .utils import pack_xdr_array, unpack_xdr_array

__all__ = ["Transaction"]


class Transaction:
"""The :class:`Transaction` object, which represents a transaction
Expand Down
6 changes: 4 additions & 2 deletions stellar_sdk/transaction_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
from .exceptions import ValueError
from .keypair import Keypair
from .memo import *
from .network import Network, TESTNET_NETWORK_PASSPHRASE
from .network import Network
from .operation import *
from .price import Price
from .signer import Signer
from .time_bounds import TimeBounds
from .transaction import Transaction
from .transaction_envelope import TransactionEnvelope

__all__ = ["TransactionBuilder"]


class TransactionBuilder:
"""Transaction builder helps constructs a new :class:`TransactionEnvelope
Expand All @@ -40,7 +42,7 @@ class TransactionBuilder:
def __init__(
self,
source_account: Account,
network_passphrase: str = TESTNET_NETWORK_PASSPHRASE,
network_passphrase: str = Network.TESTNET_NETWORK_PASSPHRASE,
base_fee: int = 100,
):
self.source_account: Account = source_account
Expand Down
2 changes: 2 additions & 0 deletions stellar_sdk/transaction_envelope.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from .transaction import Transaction
from .utils import sha256

__all__ = ["TransactionEnvelope"]


class TransactionEnvelope:
"""The :class:`TransactionEnvelope` object, which represents a transaction
Expand Down
6 changes: 2 additions & 4 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ def test_submit_transaction_with_xdr(self):
client = RequestsClient()
with Server(horizon_url, client) as server:
resp = server.submit_transaction(xdr)
assert resp.status_code == 200
assert resp.json()["envelope_xdr"] == xdr
assert resp["envelope_xdr"] == xdr

@pytest.mark.asyncio
async def test_submit_transaction_with_te(self):
Expand All @@ -137,5 +136,4 @@ async def test_submit_transaction_with_te(self):
client = AiohttpClient()
async with Server(horizon_url, client) as server:
resp = await server.submit_transaction(te)
assert resp.status_code == 200
assert resp.json()["envelope_xdr"] == xdr
assert resp["envelope_xdr"] == xdr

0 comments on commit 95a60d9

Please sign in to comment.