Skip to content

Commit

Permalink
Merge pull request #5 from shaiu/shaiu/archive-package
Browse files Browse the repository at this point in the history
add method to archive a package
  • Loading branch information
shaiu authored Aug 9, 2024
2 parents e50045c + fe4ee8c commit b550ef6
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "pyseventeentrack"
version = "1.0.0"
version = "1.0.1"
description = "A Simple Python API for 17track.net"
readme = "README.md"
authors = ["Shai Ungar <[email protected]>"]
Expand Down
31 changes: 31 additions & 0 deletions pyseventeentrack/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,34 @@ async def set_friendly_name(self, internal_id: str, friendly_name: str):
code = remark_resp.get("Code")
if code != 0:
raise RequestError(f"Non-zero status code in response: {code}")

async def archive_package(self, tracking_number: str):
"""Archive a package by tracking number."""
packages = await self.packages()

try:
package = next(p for p in packages if p.tracking_number == tracking_number)
except StopIteration as err:
raise InvalidTrackingNumberError(
f"Package not found by tracking number: {tracking_number}"
) from err

internal_id = package.id

_LOGGER.debug("Found internal ID of package: %s", internal_id)

archive_resp: dict = await self._request(
"post",
API_URL_BUYER,
json={
"version": "1.0",
"method": "SetTrackArchived",
"param": {"TrackInfoIds": [internal_id]},
},
)

_LOGGER.debug("Archive package response: %s", archive_resp)

code = archive_resp.get("Code")
if code != 0:
raise RequestError(f"Non-zero status code in response: {code}")
3 changes: 3 additions & 0 deletions tests/fixtures/archive_package_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"Code": 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Json":{},"Code":-100}
100 changes: 100 additions & 0 deletions tests/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,103 @@ async def test_add_existing_package(aresponses):
client = Client(session=session)
await client.profile.login(TEST_EMAIL, TEST_PASSWORD)
await client.profile.add_package("1234567890987654321")


@pytest.mark.asyncio
async def test_archive_package(aresponses):
"""Test archiving a package."""
aresponses.add(
"user.17track.net",
"/userapi/call",
"post",
aresponses.Response(
text=load_fixture("authentication_success_response.json"), status=200
),
)
aresponses.add(
"buyer.17track.net",
"/orderapi/call",
"post",
aresponses.Response(text=load_fixture("packages_response.json"), status=200),
)
aresponses.add(
"buyer.17track.net",
"/orderapi/call",
"post",
aresponses.Response(
text=load_fixture("archive_package_response.json"), status=200
),
)

async with aiohttp.ClientSession() as session:
client = Client(session=session)
await client.profile.login(TEST_EMAIL, TEST_PASSWORD)
res = await client.profile.archive_package("1234567890987654321")
assert res is None


@pytest.mark.asyncio
async def test_archive_package_non_existing(aresponses):
"""Test archiving a non existing package."""
aresponses.add(
"user.17track.net",
"/userapi/call",
"post",
aresponses.Response(
text=load_fixture("authentication_success_response.json"), status=200
),
)
aresponses.add(
"buyer.17track.net",
"/orderapi/call",
"post",
aresponses.Response(text=load_fixture("packages_response.json"), status=200),
)
aresponses.add(
"buyer.17track.net",
"/orderapi/call",
"post",
aresponses.Response(
text=load_fixture("archive_package_response.json"), status=200
),
)

async with aiohttp.ClientSession() as session:
with pytest.raises(InvalidTrackingNumberError):
client = Client(session=session)
await client.profile.login(TEST_EMAIL, TEST_PASSWORD)
await client.profile.archive_package("1234567890987654321111")


@pytest.mark.asyncio
async def test_archive_package_error_response(aresponses):
"""Test archiving a package with failed response."""
aresponses.add(
"user.17track.net",
"/userapi/call",
"post",
aresponses.Response(
text=load_fixture("authentication_success_response.json"), status=200
),
)
aresponses.add(
"buyer.17track.net",
"/orderapi/call",
"post",
aresponses.Response(text=load_fixture("packages_response.json"), status=200),
)
aresponses.add(
"buyer.17track.net",
"/orderapi/call",
"post",
aresponses.Response(
text=load_fixture("archive_package_response_failure_response.json"),
status=200,
),
)

async with aiohttp.ClientSession() as session:
with pytest.raises(RequestError):
client = Client(session=session)
await client.profile.login(TEST_EMAIL, TEST_PASSWORD)
await client.profile.archive_package("1234567890987654321")

0 comments on commit b550ef6

Please sign in to comment.