Closed ghost closed 1 month ago
The SDK has recently been converted to return types. It does provide an option to use dictionaries for responses. You can read about how to do that here: https://github.com/klaviyo/klaviyo-api-python/blob/main/CHANGELOG.md#backwards-compatibility
Thank you! Yes, that works. :)
What are the benefits of the new object type? And what would be the best way to loop through each profile in this object and access each profile's attributes?
The object types allow you to discover the requirements of the API via typing. It's useful in IDEs for completion, but also if you just want a codified version of the payloads and responses the API uses.
As our APIs implement JSON API protocol, the types themselves are quite nested. We are considering what we can do to pair down these types as part of future work.
Here is an example of what you asked for:
client = KlaviyoAPI(
api_key,
)
Playground.client = client
profiles = client.Profiles.get_profiles()
for profile in profiles.data:
# access attributes by name
email = profile.attributes.email
first_name = profile.attributes.first_name
print(f"{email} {first_name}")
# access all attributes of a profile
attributes_dictionary = profile.attributes.dict()
for key, value in attributes_dictionary.items():
print(f"{key}: {value}")
closing this issue as all questions were addressed.
Thank you for your help!
When I use the SDK to receive profiles, I get an object of type _<class 'openapi_client.models.get_profile_response_collection_compound_document.GetProfileResponseCollectionCompoundDocument'>_:
This type of response occurs with other endpoints as well.
How do I get a JSON-like response, which I receive when using the requests library?