diff --git a/correios/client.py b/correios/client.py index c3aa1ef..b010601 100644 --- a/correios/client.py +++ b/correios/client.py @@ -18,7 +18,7 @@ from typing import Union, Sequence, List, Dict from correios import xml_utils, DATADIR -from correios.exceptions import PostingListSerializerError +from correios.exceptions import PostingListSerializerError, TrackingCodesLimitExceededError from .models.address import ZipAddress, ZipCode from .models.posting import (NotFoundTrackingEvent, TrackingCode, PostingList, ShippingLabel, TrackingEvent, EventStatus) @@ -268,6 +268,7 @@ def get_xml(self, document) -> bytes: class Correios: PRODUCTION = "production" TEST = "test" + MAX_TRACKING_CODES_PER_REQUEST = 50 # 'environment': ('url', 'ssl_verification') sigep_urls = { @@ -364,6 +365,11 @@ def get_tracking_code_events(self, tracking_list): if isinstance(tracking_list, (str, TrackingCode)): tracking_list = [tracking_list] + if len(tracking_list) > Correios.MAX_TRACKING_CODES_PER_REQUEST: + msg = '{} tracking codes requested exceeds the limit of {} stabilished by the Correios' + msg = msg.format(len(tracking_list), Correios.MAX_TRACKING_CODES_PER_REQUEST) + raise TrackingCodesLimitExceededError(msg) + tracking_codes = {} for tracking_code in tracking_list: tracking_code = TrackingCode.create(tracking_code) diff --git a/correios/exceptions.py b/correios/exceptions.py index 149d4d4..5b574e4 100644 --- a/correios/exceptions.py +++ b/correios/exceptions.py @@ -89,6 +89,10 @@ class PostingListSerializerError(ClientError): pass +class TrackingCodesLimitExceededError(ClientError): + pass + + class RendererError(BaseCorreiosError): pass diff --git a/tests/test_client.py b/tests/test_client.py index a82c96b..ed7c8a7 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -16,7 +16,7 @@ import pytest from correios.client import ModelBuilder, Correios, PostingListSerializer -from correios.exceptions import PostingListSerializerError +from correios.exceptions import PostingListSerializerError, TrackingCodesLimitExceededError from correios.models.address import ZipCode from correios.models.data import SERVICE_SEDEX10, SERVICE_SEDEX from correios.models.posting import (NotFoundTrackingEvent, PostingList, ShippingLabel, @@ -149,6 +149,13 @@ def test_get_tracking_code_object_not_found_by_correios(): assert event.status.status == 1 +def test_get_tracking_codes_events_over_limit(): + client = Correios(username="solidarium2", password="d5kgag", environment=Correios.TEST) + codes = ["DU05508759BR"] * 51 + with pytest.raises(TrackingCodesLimitExceededError): + client.get_tracking_code_events(codes) + + def test_builder_posting_card_status(): builder = ModelBuilder() assert builder.build_posting_card_status("Normal") == PostingCard.ACTIVE