Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CloudKit Web Services

CloudKit Web Services

Marcin Krzyzanowski

January 20, 2016
Tweet

More Decks by Marcin Krzyzanowski

Other Decks in Programming

Transcript

  1. CLOUDKIT WEB SERVICES
    MARCIN KRZYŻANOWSKI, BERLIN 20.01.2016

    View Slide

  2. WELCOME
    Marcin Krzyżanowski
    @krzyzanowskim
    krzyzanowskim.com
    github.com/krzyzanowskim

    View Slide

  3. WELCOME
    CryptoSwift
    ObjectivePGP
    Natalie Storyboard Generator
    github.com/krzyzanowskim

    View Slide

  4. CLOUDKIT WEB SERVICES
    AGENDA
    ▸ Architecture overview
    ▸ Use case
    ▸ API

    View Slide

  5. ARCHITECTURE OVERVIEW
    CLOUDKIT
    ▸ Data store in a containers
    ▸ Public and Private database
    ▸ Zones (default and custom)
    ▸ Records

    View Slide

  6. ARCHITECTURE OVERVIEW
    CLOUDKIT
    ▸ CloudKit Server
    ▸ Native CloudKit.framework
    ▸ CloudKit Web Services

    View Slide

  7. CLOUDKIT WEB SERVICES
    CLOUDKIT WEB SERVICES - WHAT IS THIS?
    ▸ JSON interface to CloudKit Server
    ▸ HTTP interface

    View Slide

  8. CLOUDKIT WEB SERVICES
    FEATURES
    ▸ Access to Public and Private containers
    ▸ CRUD (create, read, update, delete) records
    ▸ Upload and downlod Assets
    ▸ User discoverability
    ▸ Web companion to native framework

    View Slide

  9. CLOUDKIT WEB SERVICES
    ▸ Parse #not
    ▸ Azure #not
    ▸ iCloud #yes

    View Slide

  10. HANDLING CLOUDKIT RIGHT IS HARDER
    THAN HANDLING COREDATA RIGHT
    Stève Jöbs #never
    CLOUDKIT WEB SERVICES

    View Slide

  11. USE CASE
    CLOUDKIT WEB SERVICES

    View Slide

  12. View Slide

  13. ▸ iOS Client Application
    ▸ OSX Content Management Application

    View Slide

  14. REQUIRE
    MAC APP STORE
    FOR PRODUCTION DATABASE

    View Slide

  15. View Slide

  16. REQUIRE
    MAC APP STORE
    FOR PRODUCTION
    DATABASE

    View Slide

  17. CLOUDKIT WEB SERVICES
    HOW IT IS?
    ▸ Seems to work in general
    ▸ Well documented
    ▸ Quite easy to understand
    ▸ StackOverflow is not helpful
    ▸ Hardly anybody is using it

    View Slide

  18. CLOUDKIT WEB SERVICES
    API - AUTHENTICATION
    ▸ OAuth
    ▸ CloudKit Dashboard
    ▸ Web Token - ckAPIToken
    ▸ Session key - ckSession

    View Slide

  19. CLOUDKIT WEB SERVICES
    https://api.apple-cloudkit.com/database/v1/[container]/
    [production]/[operation-specific subpath]?ckAPIToken=[API
    token]&ckSession=[session]
    URL

    View Slide

  20. CLOUDKIT WEB SERVICES
    SESSION KEY PARAMETER
    let charSet = NSCharacterSet(charactersInString: “+/=").invertedSet
    sessionToken.stringByAddingPercentEncodingWithAllowedCharacters(charSet)

    View Slide

  21. CLOUDKIT WEB SERVICES
    FETCHING RECORDS - REQUEST
    POST […]/[container]/[environment]/
    [database]/records/query
    {
    "query" : {
    "recordType" : "NewsFeedItem"
    },
    "zoneID" : {
    "zoneName" : "_defaultZone"
    }
    }

    View Slide

  22. CLOUDKIT WEB SERVICES
    FETCHING RECORDS - RESPONSE
    POST […]/[container]/[environment]/
    [database]/records/query
    {
    "records": [
    {
    "created": {
    "deviceID": "2",
    "timestamp": 1452769506019,
    "userRecordName": "_de35cbe145f780b85996c675811e7aa7"
    },
    "fields": {
    "title": {
    "type": "STRING",
    "value": "Berlin meetup"
    }
    },
    "modified": {
    "deviceID": "2",
    "timestamp": 1452769506019,
    "userRecordName": "_de35cbe145f780b85996c675811e7aa7"
    },
    "recordChangeTag": "ije5gd0j",
    "recordName": "08F420AC-74A5-4408-8A92-C00E980FC74E",
    "recordType": "NewsFeedItem",
    "zoneID": {
    "zoneName": "_defaultZone"
    }
    }
    ]
    }

    View Slide

  23. CLOUDKIT WEB SERVICES
    IF LUCKY - BECAUSE OF ERRORS
    ▸ 22 HTTP error codes
    ▸ 500 for server fault
    ▸ 403 for unauthenticated requests
    ▸ 503 for retry

    View Slide

  24. CLOUDKIT WEB SERVICES
    ASK FOR MORE
    ▸ continuationMarker
    ▸ addition to the query parameters
    ▸ not retry - continuation
    ▸ Up to 100 records at once (tested)

    View Slide

  25. CLOUDKIT WEB SERVICES
    BUILDING QUERY
    ▸ ___modTime
    ▸ ___createTime
    {
    "query" : {
    "recordType" : "NewsFeedItem",
    "filterBy" : [
    {
    "fieldName" : "___modTime",
    "comparator" : "GREATER_THAN_OR_EQUALS",
    "fieldValue" : {
    "value" : 1453058445252,
    "type" : "TIMESTAMP"
    }
    }
    ]
    },
    "zoneID" : {
    "zoneName" : "_defaultZone"
    }
    }

    View Slide

  26. CLOUDKIT WEB SERVICES
    PARSING VALUES
    ▸ REFERENCE - is a link type to a record
    ▸ DOUBLE and INT64 are numbers
    ▸ BYTES is Base64 encoded array of bytes
    ▸ TIMESTAMP is for dates and times in
    seconds
    ▸ STRING is string obviously
    ▸ ASSETID is for assets. Assets are stored
    separately to the database itself.
    ▸ REFERENCE_LIST
    ▸ DOUBLE_LIST
    ▸ INT64_LIST
    ▸ BYTES_LIST
    ▸ TIMESTAMP_LIST
    ▸ STRING_LIST
    ▸ ASSETID_LIST

    View Slide

  27. CLOUDKIT WEB SERVICES
    ASSETS
    ▸ Up to 15 MB
    ▸ Separate storage
    ▸ Temporary download URL
    ▸ Content-Type required
    POST […]/[container]/[environment]/[database]/assets/upload

    View Slide

  28. CLOUDKIT WEB SERVICES
    MODIFY RECORDS
    ▸ 7 operations
    ▸ force* to ignore conflicts
    ▸ delete non-existing record
    POST […]/[container]/[environment]/[database]/records/modify
    ▸ create
    ▸ update
    ▸ forceUpdate
    ▸ replace
    ▸ forceReplace
    ▸ delete
    ▸ forceDelete

    View Slide

  29. CLOUDKIT WEB SERVICES
    MODIFY RECORDS
    ▸ 7 operations
    ▸ force* to ignore conflicts
    ▸ delete non-existing record
    ▸ little delay
    POST […]/[container]/[environment]/[database]/records/modify
    ▸ create
    ▸ update
    ▸ forceUpdate
    ▸ replace
    ▸ forceReplace
    ▸ delete
    ▸ forceDelete

    View Slide

  30. CLOUDKIT WEB SERVICES
    MODIFY RECORDS
    ▸ 7 operations
    ▸ force* to ignore conflicts
    ▸ delete non-existing record
    ▸ little delay
    ▸ ASSETID_LIST problem
    POST […]/[container]/[environment]/[database]/records/modify
    ▸ create
    ▸ update
    ▸ forceUpdate
    ▸ replace
    ▸ forceReplace
    ▸ delete
    ▸ forceDelete

    View Slide

  31. CLOUDKIT WEB SERVICES
    THERE IS MORE
    ▸ Creating new custom zones
    ▸ Manage subscriptions (query subscriptions)
    ▸ Fetch user data
    ▸ Find users
    ▸ https://developer.apple.com/library/ios/documentation/DataManagement/
    Conceptual/CloutKitWebServicesReference
    ▸ CloudKit JS

    View Slide

  32. CLOUDKIT WEB SERVICES
    RECAP
    ▸ CloudKit architecture (private/public/
    development/production)
    ▸ CloudKit Web Services allows you to
    deal with the CloudKit data without
    App Store
    ▸ Real life use case
    ▸ API details

    View Slide

  33. THANK YOU
    Marcin Krzyżanowski
    @krzyzanowskim
    krzyzanowskim.com
    github.com/krzyzanowskim

    View Slide