diff --git a/pyseventeentrack/profile.py b/pyseventeentrack/profile.py index 01a047d..913f35d 100644 --- a/pyseventeentrack/profile.py +++ b/pyseventeentrack/profile.py @@ -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}") diff --git a/tests/fixtures/archive_package_response.json b/tests/fixtures/archive_package_response.json new file mode 100644 index 0000000..f09360c --- /dev/null +++ b/tests/fixtures/archive_package_response.json @@ -0,0 +1,3 @@ +{ + "Code": 0 +} \ No newline at end of file diff --git a/tests/fixtures/archive_package_response_failure_response.json b/tests/fixtures/archive_package_response_failure_response.json new file mode 100644 index 0000000..fbf6c30 --- /dev/null +++ b/tests/fixtures/archive_package_response_failure_response.json @@ -0,0 +1 @@ +{"Json":{},"Code":-100} \ No newline at end of file diff --git a/tests/test_profile.py b/tests/test_profile.py index 29da1c0..170e835 100644 --- a/tests/test_profile.py +++ b/tests/test_profile.py @@ -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")