CloudKit API with Python

1.9k Views Asked by At

My iOS app uses CloudKit as it's backend. The app basically collects simple user info (name, company, email phone).

I would like to export all this data from the cloudkit dashboard into a csv file so that I can then upload this data into a different database.

I followed this: https://github.com/lionheart/requests-cloudkit to authenticate my api.

However, I cannot figure out how to query and get the records from my database.

When I execute:

print cloudkit.public.zones.list()

It returns:

{u'zones': [{u'atomic': False, u'zoneID': {u'ownerRecordName': u'_17e6f384d00f4e11aeee6280a48f6724', u'zoneName': u'_defaultZone'}}]}

But, I want it to return all the record of recordtype "Establishment".

Any help is much appreciated!

Thanks!

2

There are 2 best solutions below

0
On

The solution from Alexis Le Baron works for me, but I had to install the requests-cloudkit codebase from git as the one that's committed is an older version.

pip install git+https://github.com/lionheart/requests-cloudkit.git

Someone did mention it in the git issues, but it seems the author hasn't corrected.

1
On

This work for me :

from requests_cloudkit import CloudKitAuth
from restmapper import restmapper
import json

KEY_ID = 'KEY_ID_APPLE'
SECRET_FILE_KEY = 'PATH_SECRET_FILE_KEY'
AUTH = CloudKitAuth(KEY_ID, SECRET_FILE_KEY)

PARAMS = {
    'query':{
        'recordType': 'PRODUCT_TYPE' #for example 'Users'
    },
}

CloudKit = restmapper.RestMapper("https://api.apple-cloudkit.com/database/[version]/[container]/[public or production...]/")
cloudkit = CloudKit(auth=AUTH)
response = cloudkit.POST.public.records.query(json.dumps(PARAMS))