Skip to content

Commit

Permalink
deepmerge added for merging dictionaries. all tests passed
Browse files Browse the repository at this point in the history
  • Loading branch information
Aviksaikat committed Dec 19, 2023
1 parent 49869ec commit f9d08ba
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 18 deletions.
11 changes: 10 additions & 1 deletion pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies = [
"eth-pydantic-types>=0.1.0a3", # need for pydantic compatible HexBytes
"web3>=6.12.0",
"pydantic>=2.5.2", # TODO: remove this
"deepmerge>=1.1.1",
]
requires-python = ">=3.9"
readme = "README.md"
Expand Down
7 changes: 3 additions & 4 deletions src/bee_py/modules/chunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ def upload(
"data": data,
"headers": {**extract_upload_headers(postage_batch_id, options)},
}

response = http(request_options, config)
response = http(request_options, config, False)

if response.status_code != 200: # noqa: PLR2004
logger.info(response.json())
Expand All @@ -47,9 +46,9 @@ def upload(
def download(request_options: BeeRequestOptions, _hash: ReferenceOrENS) -> Data:
config = {
"url": f"/{ENDPOINT}/{_hash}",
"method": "get",
"method": "GET",
}
response = http(request_options, config)
response = http(request_options, config, False)

if response.status_code != 200: # noqa: PLR2004
logger.info(response.json())
Expand Down
12 changes: 6 additions & 6 deletions src/bee_py/utils/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from urllib.parse import urljoin

import requests
from deepmerge import always_merger # type: ignore

from bee_py.types.type import BeeRequestOptions

Expand Down Expand Up @@ -68,18 +69,17 @@ def http(
options = {key_mapping.get(k, k): v for k, v in tmp_options.items()}

try:
request_config = {
"headers": DEFAULT_HTTP_CONFIG["headers"].copy(),
**config,
**options,
}
intermediate_dict = always_merger.merge(config, options)
request_config = always_merger.merge(intermediate_dict, {"headers": DEFAULT_HTTP_CONFIG["headers"].copy()})

request_config = maybe_run_on_request_hook(options, request_config)

if sanitise:
request_config = sanitise_config(request_config)

if "http" not in request_config["url"]:
msg = f"Invalid URL: {request_config['url']}"
raise TypeError(msg)

response = requests.request(**request_config)
return response
except Exception as e:
Expand Down
4 changes: 2 additions & 2 deletions src/bee_py/utils/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ def assert_reference(value: Any) -> None:
def assert_reference_or_ens(value: Any) -> None:
if isinstance(value, dict):
value = value.get("reference", None)
if isinstance(value, ReferenceResponse):
value = value.reference
if isinstance(value, (ReferenceResponse, Reference)):
value = str(value)
if not isinstance(value, str):
msg = "ReferenceOrEns has to be a string!"
raise TypeError(msg)
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def peer_overlay(bee_debug_peer_ky_options) -> str:


@pytest.fixture
def random_byte_array(length=10, seed=500):
def random_byte_array(length=100, seed=500):
# * not completely random
random.seed(seed)
return bytearray(random.randint(0, 255) for _ in range(length)) # noqa: S311
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/test_bee_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ def test_strip_trailing_slash():
assert bee.url == "http://127.0.0.1:1633"


def test_upload_and_downalod_chunk(bee_class, get_debug_postage):
content = bytes(bytearray(100))
def test_upload_and_downalod_chunk(bee_class, get_debug_postage, random_byte_array):
data = bytes(random_byte_array)

referece = bee_class.upload_chunk(get_debug_postage, content)
referece = bee_class.upload_chunk(get_debug_postage, data)
downloaded_chunk = bee_class.download_chunk(referece)

assert downloaded_chunk == content
assert downloaded_chunk.data == data

0 comments on commit f9d08ba

Please sign in to comment.