Connecting to RapidPro API in Python

91 views
Skip to first unread message

harshita joshi

unread,
Feb 3, 2021, 11:21:22 AM2/3/21
to rapidpro-dev
Hi,

I am trying to fetch the group contacts through RapidPro API in Python. The output gives location of contacts.

Command: client.get_contacts(group='XYZ').all(retry_on_rate_exceed=True)

Output: temba_client.v2.types.Contact at .................

How can I retrieve this record in a readable format and use it for further analysis?


Rowan Seymour

unread,
Feb 3, 2021, 11:24:46 AM2/3/21
to harshita joshi, rapidpro-dev
That code will give you the contacts as `temba_client.v2.types.Contact` objects and it's up to you to write some python to transform that into the format you need.

If you just want a spreadsheet of all your contacts, you could just use the Export feature on the contact list page in RapidPro.

-Rowan

--
You received this message because you are subscribed to the Google Groups "rapidpro-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rapidpro-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rapidpro-dev/48657ffb-7ac1-467d-9972-5fbf921354f2n%40googlegroups.com.


--
Rowan Seymour | +593 979099233 | @rowanseymour

Rowan Seymour

unread,
Feb 3, 2021, 11:44:01 AM2/3/21
to harshita joshi, rapidpro-dev
Hi Harshita

client.get_contacts(group='XYZ') will return CursorQuery object - but calling .all(retry_on_rate_exceed=True) on that iterates on that to fetch all of the objects, and returns them as a simple list.

Sometimes that's not ideal - if you're fetching a lot of contacts, then .all() won't return until it has fetched them all. Thus the other thing you can do with a CursorQuery object is iterate over each page of fetched contacts like...

for contact_batch in client.get_contacts(group='XYZ').iterfetches(retry_on_rate_exceed=True):
    for contact in contact_batch:
        print(contact.name)


-Rowan

On Wed, 3 Feb 2021 at 11:28, harshita joshi <harshi...@gmail.com> wrote:
Thank you for the response. The object returned in Python is a cursor query object. Would you be able to tell how to read from this object?

Using the excel will not be feasible as we are looking to update a dashboard in real time with the contacts information, hence using APIs for this purpose.

Warm Regards,
Harshita Joshi



Rowan Seymour

unread,
Feb 3, 2021, 1:46:44 PM2/3/21
to harshita joshi, rapidpro-dev
Those are just objects in Python which the JSON is loaded into.. look at https://github.com/rapidpro/rapidpro-python/blob/a06e985c3deece8172f5761eced30e8e203afb90/temba_client/v2/types.py#L124 if you want to understand what properties they have. You would access age like contact.fields.age.

-Rowan


On Wed, 3 Feb 2021 at 11:54, harshita joshi <harshi...@gmail.com> wrote:
Thank you for the prompt response.

We are interested in retrieving details like age, gender, state and other custom fields we have created in RapidPro for the contacts. However, print(contact.age) gives an error 'Contact' object has no attribute 'Age'

Also, when we connect to the API from the API explorer, the contacts are returned in JSON format. However, the list that is returned in Python is a list of locations that cannot be easily understood.

[ temba_client.v2.types.Contact at ................. ,
temba_client.v2.types.Contact at .................,
temba_client.v2.types.Contact at ................. ]

How can we convert the above output in a readable format?

Warm Regards,
Harshita Joshi



Rowan Seymour

unread,
Feb 4, 2021, 10:55:38 AM2/4/21
to harshita joshi, rapidpro-dev
If you want to get the objects in JSON, then you could just call the API endpoints directly. The Python client is just calling the endpoints and parsing the returned JSON to make it easier to work with in Python. You can even re-serialize the objects back to JSON by calling .serialize() on them if you really want.

(note when you're replying please reply to the mailing list on not just me personally)

-Rowan

On Wed, 3 Feb 2021 at 20:56, harshita joshi <harshi...@gmail.com> wrote:
This was helpful. How can we convert the  Query Object to JSON?

Regards,
Harshita Joshi

harshita joshi

unread,
Feb 4, 2021, 11:40:16 AM2/4/21
to Rowan Seymour, rapidpro-dev
Thank you for answering the questions. Hope to get the output in desired format from the suggested method.

Regards,
Harshita Joshi

harshita joshi

unread,
Feb 6, 2021, 7:39:40 AM2/6/21
to rapidpro-dev
Hi,

I tried using json.dumps() to serialize the list returned by client.get_contacts(). This is the error that I am getting- Object of type Contact is not JSON serializable

Could you please guide in this regard

Rudi Giesler

unread,
Feb 8, 2021, 3:49:07 AM2/8/21
to harshita joshi, rapidpro-dev
Hi

The python json library can only serialise certain kinds of objects into JSON, that list is found here: https://docs.python.org/3/library/json.html#py-to-json-table .

Since the `Contact` objects are class instances, they cannot be serialised, and must first be converted to a compatible object type before the python json library will be able to serialise it.

The "Contact" class provides a method, ".serialize", which will return a representation of the contact as a compatible object type for the python json library.

You could use it in a similar way to the following example:

json.dumps([contact.serialize() for contact in contact_list])

Regards,
Rudi

Reply all
Reply to author
Forward
0 new messages