Legacy Dev Forum Posts

 View Only

Sign Up

Best practice building analytics query payload (Python SDK)

  • 1.  Best practice building analytics query payload (Python SDK)

    Posted 06-05-2025 18:11

    anon28066628 | 2016-10-26 12:55:14 UTC | #1

    When using the Python SDK, the Query classes have constructors with no parameters. A sample instantiation looks like:

    queryObj = PureCloudPlatformApiSdk.ConversationQuery()

    I am reading the query payload json from disk as a dictionary. Is there a best practice to ensure all the query fields are copied to the query object? I started with a naive approach:

    def BuildAnalyticsQuery(query_json): query = PureCloudPlatformApiSdk.ConversationQuery() query.interval = query_json.get('interval') query.conversation_filters = query_json.get('conversationFilters') query.evaluation_filters = query_json.get('evaluationFilters') query.segment_filters = query_json.get('segmentFilters') query.aggregations = query_json.get('aggregations') query.paging = query_json.get('paging') query.order = query_json.get('order') query.order_by = query_json.get('orderBy') return query

    Which doesn't seem future-proof or have general applicability to other objects. I settled on this:

    def BuildAnalyticsQuery(query_json): query = PureCloudPlatformApiSdk.ConversationQuery() for k in query.attribute_map: setattr(query, k, query_json.get(query.attribute_map[k])) return query

    This works but seems like "abusing" the swagger attribute map and working against the object model instead of with it. Just wondering if there's a more canonical / simple method I'm missing?


    KevinGlinski | 2016-10-26 13:30:40 UTC | #2

    Not sure about the specifics in python on the best way to do it aside from what google tells me, but the concept is deserialization from json. The SDK handles it in https://github.com/MyPureCloud/purecloud_api_sdk_python/blob/master/build/PureCloudPlatformApiSdk/api_client.py#L562-L579 when it deserializes the response json into a model


    anon28066628 | 2016-10-28 19:54:13 UTC | #3

    Thanks Kevin. This case is a little odd in that the json isn't being retrieved and deserialized from another endpoint, which would be the usual case (the intent here is to use the output from the online Dev Center Tools' Query builder). For what we need right now this 'copy by hand' should be ok!


    system | 2017-08-28 19:28:10 UTC | #4


    This post was migrated from the old Developer Forum.

    ref: 540