Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: easypost.Event.retrieve_all_payloads results in AttributeError: type object 'Payload' has no attribute 'construct_from' #284

Closed
NathanSmeltzer opened this issue May 31, 2023 · 3 comments · Fixed by #285
Assignees
Labels
triage Issue is being researched

Comments

@NathanSmeltzer
Copy link

Software Version

7.13.0

Language Version

3.10.8

Operating System

Ubuntu 22.04.2

What happened?

  1. I'm trying to retrieve payloads for an event as described in the api docs:
import easypost
easypost.api_key = config("EASYPOST_API_KEY_TEST")
def test_list_payloads():
    # api documentation: https://www.easypost.com/docs/api/python#retrieve-a-list-of-payloads
    payloads = easypost.Event.retrieve_all_payloads(
        "evt_a93cbcd2fbef11ed9b16693a7541b68d"
    )
  1. The test fails with this error (line 84 from easypost_objects.py
             cls = getattr(easypost, class_name, EasyPostObject)
`>           obj = cls.construct_from(values=response, api_key=api_key, parent=parent, name=name)
E           AttributeError: type object 'Payload' has no attribute 'construct_from'`

What was expected?

The call did not return a list of payloads as expected.

Sample Code

No response

Relevant logs

Pytest output:

shipaware_django/shipments/utils/tests/test_easypost.py:47 (test_list_payloads)
def test_list_payloads():
        # api documentation: https://www.easypost.com/docs/api/python#retrieve-a-list-of-payloads
>       payloads = easypost.Event.retrieve_all_payloads(
            "evt_a93cbcd2fbef11ed9b16693a7541b68d"
        )

test_easypost.py:50: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/home/nate/.venv/shipaware/lib/python3.10/site-packages/easypost/event.py:30: in retrieve_all_payloads
    return convert_to_easypost_object(response=response, api_key=api_key)
/home/nate/.venv/shipaware/lib/python3.10/site-packages/easypost/easypost_object.py:75: in convert_to_easypost_object
    obj = cls.construct_from(values=response, api_key=api_key, parent=parent, name=name)
/home/nate/.venv/shipaware/lib/python3.10/site-packages/easypost/easypost_object.py:163: in construct_from
    instance.refresh_from(values=values, api_key=api_key)
/home/nate/.venv/shipaware/lib/python3.10/site-packages/easypost/easypost_object.py:175: in refresh_from
    self.__dict__[k] = convert_to_easypost_object(response=v, api_key=api_key, parent=self, name=k)
/home/nate/.venv/shipaware/lib/python3.10/site-packages/easypost/easypost_object.py:62: in convert_to_easypost_object
    return [convert_to_easypost_object(response=item, api_key=api_key, parent=parent) for item in response]
/home/nate/.venv/shipaware/lib/python3.10/site-packages/easypost/easypost_object.py:62: in <listcomp>
    return [convert_to_easypost_object(response=item, api_key=api_key, parent=parent) for item in response]
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

response = {'created_at': '2023-05-26T18:04:18Z', 'id': 'payload_bb6a216afbef11edafff179518355bf1', 'object': 'Payload', 'request...s_return":false,"public_url":"https://track.easypost.com/djE6dHJrXzAyMzhkYjkyZmU5MjQyZTViMTc3MGRlMWU3ZWFlMzk3"}}', ...}
api_key = [REMOVED]
parent = <EasyPostObject at 0x7f8038106fb0> JSON: {}, name = None

    def convert_to_easypost_object(
        response: Dict[str, Any],
        api_key: Optional[str] = None,
        parent: object = None,
        name: Optional[str] = None,
    ):
        """Convert a response to an EasyPost object."""
        if isinstance(response, list):
            return [convert_to_easypost_object(response=item, api_key=api_key, parent=parent) for item in response]
        elif isinstance(response, dict):
            response = response.copy()
            object_type_str = response.get("object", "EasyPostObject")
            class_name = OBJECT_CLASS_NAME_OVERRIDES.get(object_type_str, object_type_str)
    
            object_id = response.get("id", None)
            if object_id is not None:
                # If an object ID is present, use it to find the class type instead.
                object_id_prefix = object_id.split("_")[0]
                class_name = EASYPOST_OBJECT_ID_PREFIX_TO_CLASS_NAME_MAP.get(object_id_prefix, "EasyPostObject")
    
            cls = getattr(easypost, class_name, EasyPostObject)
>           obj = cls.construct_from(values=response, api_key=api_key, parent=parent, name=name)
E           AttributeError: type object 'Payload' has no attribute 'construct_from'

/home/nate/.venv/shipaware/lib/python3.10/site-packages/easypost/easypost_object.py:75: AttributeError

Process finished with exit code 1
@NathanSmeltzer NathanSmeltzer added the triage Issue is being researched label May 31, 2023
@NathanSmeltzer NathanSmeltzer changed the title [Bug]: [Bug]: easypost.Event.retrieve_all_payloads results in AttributeError: type object 'Payload' has no attribute 'construct_from' May 31, 2023
@Justintime50
Copy link
Member

Thanks for reporting this! It appears that Payload doesn't inherit Resource which then inherits EasyPostObject which has the construct_from function. I've put up #285 which should patch this issue and get you on your way. Should have a release out today.

@Justintime50 Justintime50 self-assigned this May 31, 2023
@Justintime50
Copy link
Member

v7.13.1 is released and should fix this bug, let me know if you have further problems with this!

@NathanSmeltzer
Copy link
Author

Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage Issue is being researched
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants