Skip to content

Commit

Permalink
Update and modify files for the project
Browse files Browse the repository at this point in the history
- Updated files:
  - .github/workflows/tests.yml
  - noxfile.py
  - pdm.lock
  - pyproject.toml
  - src/bee_py/bee.py
  - src/bee_py/modules/bzz.py
  - src/bee_py/modules/chunk.py
  - src/bee_py/types/type.py
  - src/bee_py/utils/tar.py
  - src/bee_py/utils/type.py
  - tests/conftest.py
  - tests/integration/modules/test_bzz.py
  - tests/integration/modules/test_chunk.py
  - tests/test_files/bee_data.json
  • Loading branch information
Aviksaikat committed Dec 10, 2023
1 parent 89c2fc0 commit dc8db9e
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 91 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
python-version: ["3.9", "3.10", "3.11"]
python-version: ["3.9", "3.10", "3.11", "3.12"]

steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -54,7 +54,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
python-version: ["3.9", "3.10", "3.11"]
python-version: ["3.9", "3.10", "3.11", "3.12"]

steps:
- uses: actions/checkout@v4
Expand All @@ -81,7 +81,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
python-version: ["3.9", "3.10", "3.11"]
python-version: ["3.9", "3.10", "3.11", "3.12"]

steps:
- uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# import nox

# package = "bee_py"
# python_versions = ["3.11", "3.10", "3.9"]
# python_versions = ["3.12","3.11", "3.10", "3.9"]
# nox.needs_version = ">= 2023.4.22"
# nox.options.sessions = (
# "pre-commit",
Expand Down
10 changes: 5 additions & 5 deletions pdm.lock

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

7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
[project]
name = "bee-py"
version = "0.1.0"
description = "Python client library for connecting to Bee decentralised storage"
authors = [{ name = "SAIKAT KARMAKAR", email = "[email protected]" }]
description = ""
authors = [
{ name = "SAIKAT KARMAKAR", email = "[email protected]" },
]
dependencies = [
"eth-ape>=0.6.27",
"structlog>=23.2.0",
Expand Down Expand Up @@ -36,7 +38,6 @@ Changelog = "https://github.com/alienrobotninja/bee-py/releases"
requires = ["setuptools>=61", "wheel"]
build-backend = "setuptools.build_meta"


# pdm dependencies
[tool.pdm.dev-dependencies]
test = [
Expand Down
14 changes: 7 additions & 7 deletions src/bee_py/bee.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ def get_all_tags(self, options: Optional[AllTagsOptions] = None) -> list[Tag]:
Returns:
list[Tag]
See Also:
* [Bee docs - Syncing / Tags](https://docs.ethswarm.org/docs/access-the-swarm/syncing)
* [Bee docs - Syncing / Tags](https://docs.ethswarm.org/docs/develop/access-the-swarm/syncing)
* [Bee API reference - `GET /tags`](https://docs.ethswarm.org/api/#tag/Tag/paths/~1tags/get)
"""
assert_all_tags_options(options)
Expand All @@ -607,7 +607,7 @@ def delete_tag(
Returns:
None
See Also:
* [Bee docs - Syncing / Tags](https://docs.ethswarm.org/docs/access-the-swarm/syncing)
* [Bee docs - Syncing / Tags](https://docs.ethswarm.org/docs/develop/access-the-swarm/syncing)
* [Bee API reference - `DELETE /tags/{uid}`](https://docs.ethswarm.org/api/#tag/Tag/paths/~1tags~1{uid}/delete)
*/
"""
Expand Down Expand Up @@ -639,7 +639,7 @@ def update_tag(
Returns:
None
See also:
* [Bee docs - Syncing / Tags](https://docs.ethswarm.org/docs/access-the-swarm/syncing)
* [Bee docs - Syncing / Tags](https://docs.ethswarm.org/docs/develop/access-the-swarm/syncing)
* [Bee API reference - `PATCH /tags/{uid}`](https://docs.ethswarm.org/api/#tag/Tag/paths/~1tags~1{uid}/patch)
"""
assert_reference(reference)
Expand Down Expand Up @@ -667,7 +667,7 @@ def pin(self, reference: Union[Reference, str], request_options: Optional[BeeReq
Returns:
None
See also:
* [Bee docs - Pinning](https://docs.ethswarm.org/docs/access-the-swarm/pinning)
* [Bee docs - Pinning](https://docs.ethswarm.org/docs/develop/access-the-swarm/pinning)
"""
assert_reference(reference)
assert_request_options(request_options)
Expand All @@ -691,7 +691,7 @@ def unpin(self, reference: Union[Reference, str], request_options: Optional[BeeR
HTTPXError: If an HTTP error occurs during the request.
See also:
* [Bee docs - Pinning](https://docs.ethswarm.org/docs/access-the-swarm/pinning)
* [Bee docs - Pinning](https://docs.ethswarm.org/docs/develop/access-the-swarm/pinning)
"""
assert_reference(reference)
assert_request_options(request_options)
Expand All @@ -711,7 +711,7 @@ def get_all_pins(self, request_options: Optional[BeeRequestOptions] = None) -> R
HTTPXError: If an HTTP error occurs during the request.
See also:
* [Bee docs - Pinning](https://docs.ethswarm.org/docs/access-the-swarm/pinning)
* [Bee docs - Pinning](https://docs.ethswarm.org/docs/develop/access-the-swarm/pinning)
"""
assert_request_options(request_options)

Expand All @@ -735,7 +735,7 @@ def get_pin(self, reference: Union[Reference, str], request_options: Optional[Be
HTTPXError: If an HTTP error occurs during the request.
See also:
* [Bee docs - Pinning](https://docs.ethswarm.org/docs/access-the-swarm/pinning)
* [Bee docs - Pinning](https://docs.ethswarm.org/docs/develop/access-the-swarm/pinning)
"""
assert_reference(reference)
assert_request_options(request_options)
Expand Down
28 changes: 15 additions & 13 deletions src/bee_py/modules/bzz.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from typing import Optional, Union

from bee_py.types.type import ( # Reference,; UploadHeaders,; Data,; CollectionEntry,; CollectionUploadHeaders,; FileUploadHeaders, # noqa: E501
from bee_py.types.type import ( # Reference,; UploadHeaders,; Data,; CollectionEntry,; CollectionUploadHeaders,; FileUploadHeaders, # noqa: E501; FileHeaders,
BatchId,
BeeRequestOptions,
Collection,
CollectionUploadOptions,
FileData,
FileHeaders,
FileUploadOptions,
Reference,
ReferenceOrENS,
Expand All @@ -29,10 +28,10 @@ def extract_file_upload_headers(postage_batch_id: BatchId, options: Optional[Fil
options = FileUploadOptions.model_validate(options)

if options and options.size:
headers["content-length"] = str(options.size)
headers["Content-Length"] = str(options.size)

if options and options.content_type:
headers["content-type"] = options.content_type
headers["Content-Type"] = options.content_type

return headers

Expand Down Expand Up @@ -83,13 +82,13 @@ def upload_file(
logger.error(response.raise_for_status())

upload_response = response.json()
reference = Reference(upload_response["reference"])
reference = Reference(value=upload_response["reference"])
tag_uid = None

if "swarm-tag" in response.headers:
tag_uid = make_tag_uid(response.headers["swarm-tag"])
if "Swarm-Tag" in response.headers:
tag_uid = make_tag_uid(response.headers["Swarm-Tag"])

return UploadResult(reference=reference, tag_uid=tag_uid)
return UploadResult(reference=reference, tagUid=tag_uid)


def download_file(request_options: BeeRequestOptions, _hash: ReferenceOrENS, path: str = "") -> FileData:
Expand All @@ -105,6 +104,9 @@ def download_file(request_options: BeeRequestOptions, _hash: ReferenceOrENS, pat
FileData: Downloaded file data.
"""

if isinstance(_hash, Reference):
_hash = str(_hash)

config = {"url": f"{BZZ_ENDPOINT}/{_hash}/{path}", "method": "GET"}
response = http(request_options, config)

Expand All @@ -113,7 +115,7 @@ def download_file(request_options: BeeRequestOptions, _hash: ReferenceOrENS, pat
if response.raise_for_status():
logger.error(response.raise_for_status())

file_headers = FileHeaders.model_validate(read_file_headers(response.headers))
file_headers = read_file_headers(response.headers)
file_data = wrap_bytes_with_helpers(response.content)

return FileData(headers=file_headers, data=file_data.data)
Expand Down Expand Up @@ -219,10 +221,10 @@ def upload_collection(
logger.error(response.raise_for_status())

upload_response = response.json()
reference = Reference(upload_response["reference"])
reference = Reference(value=upload_response["reference"])
tag_uid = None

if "swarm-tag" in response.headers:
tag_uid = make_tag_uid(response.headers["swarm-tag"])
if "Swarm-Tag" in response.headers:
tag_uid = make_tag_uid(response.headers["Swarm-Tag"])

return UploadResult(reference=reference, tag_uid=tag_uid)
return UploadResult(reference=reference, tagUid=tag_uid)
2 changes: 1 addition & 1 deletion src/bee_py/modules/chunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def upload(
if response.raise_for_status():
logger.error(response.raise_for_status())

return Reference(response.json()["reference"])
return Reference(value=response.json()["reference"])


def download(request_options: BeeRequestOptions, _hash: ReferenceOrENS) -> Data:
Expand Down
51 changes: 27 additions & 24 deletions src/bee_py/types/type.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import json

# from abc import abstractmethod
from enum import Enum
from typing import Annotated, Any, Callable, Generic, NewType, Optional, TypeVar, Union

from ape.managers.accounts import AccountAPI
from ape.types import AddressType

# from eth_pydantic_types import HexBytes
from eth_pydantic_types import HexBytes as BaseHexBytes
from pydantic import BaseModel, Field, validator

# from requests import PreparedRequest, Response
from typing_extensions import TypeAlias

Expand Down Expand Up @@ -107,16 +110,16 @@ class BeeResponse(BaseModel):

headers: dict[str, str]
status: int
status_text: Optional[str] = Field(None, alias="statusText")
status_text: Optional[str] = Field(default="", alias="statusText")
request: BeeRequest


class BeeRequestOptions(BaseModel):
base_url: Optional[str] = Field(None, alias="baseURL")
base_url: Optional[str] = Field(default="", alias="baseURL")
timeout: Optional[int] = None
retry: Union[int, bool] = None
headers: dict = {}
on_request: bool = Field(None, alias="onRequest")
on_request: bool = Field(..., alias="onRequest")


class PssSubscription(BaseModel):
Expand Down Expand Up @@ -477,8 +480,8 @@ class NodeAddresses(BaseModel):
overlay: str
underlay: list[str]
ethereum: str
public_key: str = Field(..., alias="publicKey")
pss_public_key: str = Field(..., alias="pssPublicKey")
public_key: str = Field(default=None, alias="publicKey")
pss_public_key: str = Field(default=None, alias="pssPublicKey")


class Peer(BaseModel):
Expand Down Expand Up @@ -516,19 +519,19 @@ class RemovePeerResponse(BaseModel):
class Bin(BaseModel):
population: int
connected: int
disconnected_peers: Optional[list[Peer]] = Field(None, alias="disconnectedPeers")
connected_peers: Optional[list[Peer]] = Field(None, alias="connectedPeers")
disconnected_peers: Optional[list[Peer]] = Field(default=[], alias="disconnectedPeers")
connected_peers: Optional[list[Peer]] = Field(default=[], alias="connectedPeers")


class Topology(BaseModel):
base_address: str = Field(..., alias="baseAddr")
base_address: str = Field(default="", alias="baseAddr")
population: int
connected: int
timestamp: str
nn_low_watermark: int = Field(..., alias="nnLowWatermark")
nn_low_watermark: int = Field(default=None, alias="nnLowWatermark")
depth: int
reachability: str
network_availability: str = Field(..., alias="networkAvailability")
network_availability: str = Field(default="", alias="networkAvailability")
bins: dict[str, Bin]


Expand Down Expand Up @@ -597,8 +600,8 @@ class CashoutResult(BaseModel):
class LastCashoutActionResponse(BaseModel):
peer: str
uncashed_amount: str = Field(..., alias="uncashedAmount")
transaction_hash: Optional[str] = Field(..., alias="transactionHash")
last_cashed_cheque: Optional[Cheque] = Field(..., alias="lastCashedCheque")
transaction_hash: Optional[str] = Field(default="", alias="transactionHash")
last_cashed_cheque: Optional[Cheque] = Field(default="", alias="lastCashedCheque")
result: Optional[CashoutResult]


Expand Down Expand Up @@ -638,23 +641,23 @@ class PostageBatch(BaseModel):


class BatchBucket(BaseModel):
bucket_id: int = Field(..., alias="bucketID")
bucket_id: int = Field(default=None, alias="bucketID")
collisions: int


class PostageBatchBuckets(BaseModel):
depth: int
bucket_depth: int = Field(..., alias="bucketDepth")
bucket_upper_bound: int = Field(..., alias="bucketUpperBound")
bucket_depth: int = Field(default=None, alias="bucketDepth")
bucket_upper_bound: int = Field(default=None, alias="bucketUpperBound")
buckets: Optional[list[BatchBucket]]


class PostageBatchOptions(BaseModel):
label: Optional[str]
gas_price: Optional[str] = Field(None, alias="gasPrice")
gas_price: Optional[str] = Field(default="", alias="gasPrice")
immutable_flag: Optional[bool]
wait_for_usable: Optional[bool] = Field(True, alias="waitForUsable")
wait_for_usable_timeout: Optional[int] = Field(120, alias="waitForUsableTimeout")
wait_for_usable: Optional[bool] = Field(default=True, alias="waitForUsable")
wait_for_usable_timeout: Optional[int] = Field(default=120, alias="waitForUsableTimeout")


class StampResponse(BaseModel):
Expand Down Expand Up @@ -694,7 +697,7 @@ class WalletBalance(BaseModel):
# deprecated
bzz: Optional[str] = None
xDai: Optional[str] = None # noqa: N815
contract_address: Optional[str] = Field(None, alias="contractAddress")
contract_address: Optional[str] = Field(default="", alias="contractAddress")


class ExtendedTag(BaseModel):
Expand Down Expand Up @@ -735,7 +738,7 @@ class TransactionInfo(BaseModel):


class PendingTransactionsResponse(BaseModel):
pending_transactions: list[TransactionInfo] = Field(..., alias="pendingTransactions")
pending_transactions: list[TransactionInfo] = Field(default=[], alias="pendingTransactions")


class FeedType(Enum):
Expand Down Expand Up @@ -843,7 +846,7 @@ class JsonFeedOptions(BaseModel):

class UploadResult(BaseModel):
reference: Reference
tag_uid: Optional[int] = Field(None, alias="tagUid")
tag_uid: Optional[int] = Field(default=None, alias="tagUid")

class Config:
arbitrary_types_allowed = True
Expand Down Expand Up @@ -882,11 +885,11 @@ class Collection(BaseModel):


class CollectionUploadOptions(UploadOptions):
index_document: Optional[str] = Field(None, alias="indexDocument")
error_document: Optional[str] = Field(None, alias="errorDocument")
index_document: Optional[str] = Field(default="", alias="indexDocument")
error_document: Optional[str] = Field(default="", alias="errorDocument")


class FileData(FileHeaders, BaseModel):
class FileData(BaseModel):
headers: FileHeaders
data: bytes

Expand Down
2 changes: 1 addition & 1 deletion src/bee_py/utils/tar.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def make_tar(data: Union[Collection, list[dict]]) -> bytes:
with tarfile.open(mode="w", fileobj=tar_buffer) as tar:
for entry in data.entries:
entry_path = entry.path
entry_data = entry.data.encode()
entry_data = entry.data

info = tarfile.TarInfo(name=entry_path)
info.size = len(entry_data)
Expand Down
Loading

0 comments on commit dc8db9e

Please sign in to comment.