{"action":"created","comment":{"url":"https://api.github.com/repos/Kinto/kinto-updater/pulls/comments/40647721","id":40647721,"diff_hunk":"@@ -0,0 +1,214 @@\n+import copy\n+import hashlib\n+import json\n+import operator\n+import uuid\n+import collections\n+from contextlib import contextmanager\n+\n+import kintoclient\n+\n+import signing\n+\n+\n+@contextmanager\n+def batch_requests(session, endpoints):\n+    batch = Batch(session, endpoints)\n+    yield batch\n+    batch.send()\n+\n+\n+class Batch(object):\n+\n+    def __init__(self, session, endpoints):\n+        self.session = session\n+        self.endpoints = endpoints\n+        self.requests = []\n+\n+    def add(self, method, url, data=None, permissions=None):\n+        # Store all the requests in a dict, to be read later when .send()\n+        # is called.\n+        self.requests.append((method, url, data, permissions))\n+\n+    def _build_requests(self):\n+        requests = []\n+        for (method, url, data, permissions) in self.requests:\n+            request = {\n+                'method': method,\n+                'path': url}\n+\n+            request['body'] = {}\n+            if data is not None:\n+                request['body']['data'] = data\n+            if permissions is not None:\n+                request['body']['permissions'] = permissions\n+            requests.append(request)\n+        return requests\n+\n+    def send(self):\n+        requests = self._build_requests()\n+        resp = self.session.request(\n+            'POST',\n+            self.endpoints.batch(),\n+            data={'requests': requests}\n+        )\n+        self.requests = []\n+        return resp\n+\n+\n+class Endpoints(object):\n+    def __init__(self, root=''):\n+        self.root = root\n+\n+    def collection(self, bucket, coll):\n+        return ('{root}/buckets/{bucket}/collections/{coll}'\n+                .format(root=self.root, bucket=bucket, coll=coll))\n+\n+    def records(self, bucket, coll):\n+        return ('{root}/buckets/{bucket}/collections/{coll}/records'\n+                .format(root=self.root, bucket=bucket, coll=coll))\n+\n+    def record(self, bucket, coll, record_id):\n+        return ('{root}/buckets/{bucket}/collections/{coll}/records/{rid}'\n+                .format(root=self.root, bucket=bucket, coll=coll,\n+                        rid=record_id))\n+\n+    def batch(self):\n+        return '{root}/batch'.format(root=self.root)\n+\n+    def root(self):\n+        return '{root}/'.format(root=self.root)\n+\n+\n+class Updater(object):\n+\n+    def __init__(self, bucket, collection, auth=None,\n+                 server_url=kintoclient.DEFAULT_SERVER_URL,\n+                 session=None, endpoints=None,\n+                 signer=None, settings=None):\n+        if session is None and auth is None:\n+            raise ValueError('session or auth should be defined')\n+        if session is None:\n+            session = kintoclient.create_session(\n+                server_url, auth)\n+        self.session = session\n+\n+        if settings is None:\n+            settings = {}\n+        self.settings = settings\n+\n+        if signer is None:\n+            signer = signing.RSABackend(self.settings)\n+        self.signer = signer\n+\n+        if endpoints is None:\n+            endpoints = Endpoints()\n+        self.endpoints = endpoints\n+\n+        self.bucket = bucket\n+        self.collection = collection\n+        self.server_url = server_url\n+\n+    def gather_remote_collection(self):\n+        '''Retrieves the remote collection and returns it.'''\n+        collection_resp, _ = self.session.request(\n+            'get',\n+            self.endpoints.collection(self.bucket, self.collection))\n+\n+        def _get_records(records=None, token=None):\n+            if records is None:\n+                records = {}\n+\n+            kwargs = {}\n+            if token is not None:\n+                kwargs['_token'] = token\n+            record_resp, headers = self.session.request(\n+                'get',\n+                self.endpoints.records(self.bucket, self.collection),\n+                **kwargs)\n+\n+            records.update({record['id']: record\n+                            for record in record_resp['data']})\n+\n+            if 'Next-Page' in headers.keys():\n+                return _get_records(records, token=headers['Next-Page'])\n+            return records\n+\n+        records = _get_records()\n+        return records, collection_resp['data']\n+\n+    def check_data_validity(self, records, signature):\n+        local_hash = compute_hash(records.values())\n+        self.signer.verify(local_hash, signature)\n+\n+    def add_records(self, new_records):\n+        new_records = copy.deepcopy(new_records)\n+        records, collection_data = self.gather_remote_collection()\n+\n+        if records:\n+            remote_hash = collection_data['hash']\n+            signature = collection_data['signature']\n+            self.check_data_validity(records, signature)\n+\n+        with batch_requests(self.session, self.endpoints) as batch:\n+            # Create IDs for records which don't already have one.\n+            for record in new_records:\n+                if 'id' not in record:\n+                    record['id'] = str(uuid.uuid4())\n+\n+                record_endpoint = self.endpoints.record(\n+                    self.bucket, self.collection, record['id'])\n+\n+                batch.add('PUT', record_endpoint, data=record)\n+\n+            # Compute the hash of the old + new records\n+            records.update({record['id']: record\n+                            for record in new_records})\n+            new_hash = compute_hash(records.values())\n+            signature = self.signer.sign(new_hash)\n+\n+            # Send the new hash + signature to the remote.\n+            batch.add(\n+                'PUT',\n+                self.endpoints.records(self.bucket, self.collection),\n+                data={'signature': signature}\n+            )\n+\n+def compute_hash(records):\n+    records = copy.deepcopy(records)\n+    for record in records:\n+        if 'last_modified' in record.keys():\n+            del record['last_modified']\n+\n+    records = sorted(records, key=operator.itemgetter('id'))\n+\n+    serialized = json.dumps(records, sort_keys=True)\n+    print(serialized)\n+    h = hashlib.new('sha256')\n+    h.update(serialized)\n+    return h.hexdigest()\n+\n+def generate_key():\n+    signer = signing.RSABackend()\n+    return signer.generate_key()","path":"kintoupdater/__init__.py","position":193,"original_position":193,"commit_id":"0314f933f514565d0eb77077739de053925dda52","original_commit_id":"0314f933f514565d0eb77077739de053925dda52","user":{"login":"leplatrem","id":546692,"avatar_url":"https://avatars.githubusercontent.com/u/546692?v=3","gravatar_id":"","url":"https://api.github.com/users/leplatrem","html_url":"https://github.com/leplatrem","followers_url":"https://api.github.com/users/leplatrem/followers","following_url":"https://api.github.com/users/leplatrem/following{/other_user}","gists_url":"https://api.github.com/users/leplatrem/gists{/gist_id}","starred_url":"https://api.github.com/users/leplatrem/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/leplatrem/subscriptions","organizations_url":"https://api.github.com/users/leplatrem/orgs","repos_url":"https://api.github.com/users/leplatrem/repos","events_url":"https://api.github.com/users/leplatrem/events{/privacy}","received_events_url":"https://api.github.com/users/leplatrem/received_events","type":"User","site_admin":false},"body":"unused ?","created_at":"2015-09-29T08:49:28Z","updated_at":"2015-09-29T08:49:28Z","html_url":"https://github.com/Kinto/kinto-updater/pull/1#discussion_r40647721","pull_request_url":"https://api.github.com/repos/Kinto/kinto-updater/pulls/1","_links":{"self":{"href":"https://api.github.com/repos/Kinto/kinto-updater/pulls/comments/40647721"},"html":{"href":"https://github.com/Kinto/kinto-updater/pull/1#discussion_r40647721"},"pull_request":{"href":"https://api.github.com/repos/Kinto/kinto-updater/pulls/1"}}},"pull_request":{"url":"https://api.github.com/repos/Kinto/kinto-updater/pulls/1","id":46248274,"html_url":"https://github.com/Kinto/kinto-updater/pull/1","diff_url":"https://github.com/Kinto/kinto-updater/pull/1.diff","patch_url":"https://github.com/Kinto/kinto-updater/pull/1.patch","issue_url":"https://api.github.com/repos/Kinto/kinto-updater/issues/1","number":1,"state":"open","locked":false,"title":"Initial version","user":{"login":"leplatrem","id":546692,"avatar_url":"https://avatars.githubusercontent.com/u/546692?v=3","gravatar_id":"","url":"https://api.github.com/users/leplatrem","html_url":"https://github.com/leplatrem","followers_url":"https://api.github.com/users/leplatrem/followers","following_url":"https://api.github.com/users/leplatrem/following{/other_user}","gists_url":"https://api.github.com/users/leplatrem/gists{/gist_id}","starred_url":"https://api.github.com/users/leplatrem/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/leplatrem/subscriptions","organizations_url":"https://api.github.com/users/leplatrem/orgs","repos_url":"https://api.github.com/users/leplatrem/repos","events_url":"https://api.github.com/users/leplatrem/events{/privacy}","received_events_url":"https://api.github.com/users/leplatrem/received_events","type":"User","site_admin":false},"body":"I opened this PR to ease review :)","created_at":"2015-09-29T08:21:51Z","updated_at":"2015-09-29T08:49:28Z","closed_at":null,"merged_at":null,"merge_commit_sha":"ac35bc0ec63764e6d10139ebdf16e4706c7f4d67","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/Kinto/kinto-updater/pulls/1/commits","review_comments_url":"https://api.github.com/repos/Kinto/kinto-updater/pulls/1/comments","review_comment_url":"https://api.github.com/repos/Kinto/kinto-updater/pulls/comments{/number}","comments_url":"https://api.github.com/repos/Kinto/kinto-updater/issues/1/comments","statuses_url":"https://api.github.com/repos/Kinto/kinto-updater/statuses/0314f933f514565d0eb77077739de053925dda52","head":{"label":"Kinto:initial-version","ref":"initial-version","sha":"0314f933f514565d0eb77077739de053925dda52","user":{"login":"Kinto","id":13413813,"avatar_url":"https://avatars.githubusercontent.com/u/13413813?v=3","gravatar_id":"","url":"https://api.github.com/users/Kinto","html_url":"https://github.com/Kinto","followers_url":"https://api.github.com/users/Kinto/followers","following_url":"https://api.github.com/users/Kinto/following{/other_user}","gists_url":"https://api.github.com/users/Kinto/gists{/gist_id}","starred_url":"https://api.github.com/users/Kinto/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Kinto/subscriptions","organizations_url":"https://api.github.com/users/Kinto/orgs","repos_url":"https://api.github.com/users/Kinto/repos","events_url":"https://api.github.com/users/Kinto/events{/privacy}","received_events_url":"https://api.github.com/users/Kinto/received_events","type":"Organization","site_admin":false},"repo":{"id":42935019,"name":"kinto-updater","full_name":"Kinto/kinto-updater","owner":{"login":"Kinto","id":13413813,"avatar_url":"https://avatars.githubusercontent.com/u/13413813?v=3","gravatar_id":"","url":"https://api.github.com/users/Kinto","html_url":"https://github.com/Kinto","followers_url":"https://api.github.com/users/Kinto/followers","following_url":"https://api.github.com/users/Kinto/following{/other_user}","gists_url":"https://api.github.com/users/Kinto/gists{/gist_id}","starred_url":"https://api.github.com/users/Kinto/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Kinto/subscriptions","organizations_url":"https://api.github.com/users/Kinto/orgs","repos_url":"https://api.github.com/users/Kinto/repos","events_url":"https://api.github.com/users/Kinto/events{/privacy}","received_events_url":"https://api.github.com/users/Kinto/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/Kinto/kinto-updater","description":"Update a signed kinto instance with new records","fork":false,"url":"https://api.github.com/repos/Kinto/kinto-updater","forks_url":"https://api.github.com/repos/Kinto/kinto-updater/forks","keys_url":"https://api.github.com/repos/Kinto/kinto-updater/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Kinto/kinto-updater/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Kinto/kinto-updater/teams","hooks_url":"https://api.github.com/repos/Kinto/kinto-updater/hooks","issue_events_url":"https://api.github.com/repos/Kinto/kinto-updater/issues/events{/number}","events_url":"https://api.github.com/repos/Kinto/kinto-updater/events","assignees_url":"https://api.github.com/repos/Kinto/kinto-updater/assignees{/user}","branches_url":"https://api.github.com/repos/Kinto/kinto-updater/branches{/branch}","tags_url":"https://api.github.com/repos/Kinto/kinto-updater/tags","blobs_url":"https://api.github.com/repos/Kinto/kinto-updater/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Kinto/kinto-updater/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Kinto/kinto-updater/git/refs{/sha}","trees_url":"https://api.github.com/repos/Kinto/kinto-updater/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Kinto/kinto-updater/statuses/{sha}","languages_url":"https://api.github.com/repos/Kinto/kinto-updater/languages","stargazers_url":"https://api.github.com/repos/Kinto/kinto-updater/stargazers","contributors_url":"https://api.github.com/repos/Kinto/kinto-updater/contributors","subscribers_url":"https://api.github.com/repos/Kinto/kinto-updater/subscribers","subscription_url":"https://api.github.com/repos/Kinto/kinto-updater/subscription","commits_url":"https://api.github.com/repos/Kinto/kinto-updater/commits{/sha}","git_commits_url":"https://api.github.com/repos/Kinto/kinto-updater/git/commits{/sha}","comments_url":"https://api.github.com/repos/Kinto/kinto-updater/comments{/number}","issue_comment_url":"https://api.github.com/repos/Kinto/kinto-updater/issues/comments{/number}","contents_url":"https://api.github.com/repos/Kinto/kinto-updater/contents/{+path}","compare_url":"https://api.github.com/repos/Kinto/kinto-updater/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Kinto/kinto-updater/merges","archive_url":"https://api.github.com/repos/Kinto/kinto-updater/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Kinto/kinto-updater/downloads","issues_url":"https://api.github.com/repos/Kinto/kinto-updater/issues{/number}","pulls_url":"https://api.github.com/repos/Kinto/kinto-updater/pulls{/number}","milestones_url":"https://api.github.com/repos/Kinto/kinto-updater/milestones{/number}","notifications_url":"https://api.github.com/repos/Kinto/kinto-updater/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Kinto/kinto-updater/labels{/name}","releases_url":"https://api.github.com/repos/Kinto/kinto-updater/releases{/id}","created_at":"2015-09-22T13:21:26Z","updated_at":"2015-09-22T13:27:51Z","pushed_at":"2015-09-29T08:21:51Z","git_url":"git://github.com/Kinto/kinto-updater.git","ssh_url":"git@github.com:Kinto/kinto-updater.git","clone_url":"https://github.com/Kinto/kinto-updater.git","svn_url":"https://github.com/Kinto/kinto-updater","homepage":"","size":0,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"Kinto:master","ref":"master","sha":"97375d4138aab565cc79c7068c1932b77cfdc9b2","user":{"login":"Kinto","id":13413813,"avatar_url":"https://avatars.githubusercontent.com/u/13413813?v=3","gravatar_id":"","url":"https://api.github.com/users/Kinto","html_url":"https://github.com/Kinto","followers_url":"https://api.github.com/users/Kinto/followers","following_url":"https://api.github.com/users/Kinto/following{/other_user}","gists_url":"https://api.github.com/users/Kinto/gists{/gist_id}","starred_url":"https://api.github.com/users/Kinto/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Kinto/subscriptions","organizations_url":"https://api.github.com/users/Kinto/orgs","repos_url":"https://api.github.com/users/Kinto/repos","events_url":"https://api.github.com/users/Kinto/events{/privacy}","received_events_url":"https://api.github.com/users/Kinto/received_events","type":"Organization","site_admin":false},"repo":{"id":42935019,"name":"kinto-updater","full_name":"Kinto/kinto-updater","owner":{"login":"Kinto","id":13413813,"avatar_url":"https://avatars.githubusercontent.com/u/13413813?v=3","gravatar_id":"","url":"https://api.github.com/users/Kinto","html_url":"https://github.com/Kinto","followers_url":"https://api.github.com/users/Kinto/followers","following_url":"https://api.github.com/users/Kinto/following{/other_user}","gists_url":"https://api.github.com/users/Kinto/gists{/gist_id}","starred_url":"https://api.github.com/users/Kinto/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Kinto/subscriptions","organizations_url":"https://api.github.com/users/Kinto/orgs","repos_url":"https://api.github.com/users/Kinto/repos","events_url":"https://api.github.com/users/Kinto/events{/privacy}","received_events_url":"https://api.github.com/users/Kinto/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/Kinto/kinto-updater","description":"Update a signed kinto instance with new records","fork":false,"url":"https://api.github.com/repos/Kinto/kinto-updater","forks_url":"https://api.github.com/repos/Kinto/kinto-updater/forks","keys_url":"https://api.github.com/repos/Kinto/kinto-updater/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Kinto/kinto-updater/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Kinto/kinto-updater/teams","hooks_url":"https://api.github.com/repos/Kinto/kinto-updater/hooks","issue_events_url":"https://api.github.com/repos/Kinto/kinto-updater/issues/events{/number}","events_url":"https://api.github.com/repos/Kinto/kinto-updater/events","assignees_url":"https://api.github.com/repos/Kinto/kinto-updater/assignees{/user}","branches_url":"https://api.github.com/repos/Kinto/kinto-updater/branches{/branch}","tags_url":"https://api.github.com/repos/Kinto/kinto-updater/tags","blobs_url":"https://api.github.com/repos/Kinto/kinto-updater/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Kinto/kinto-updater/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Kinto/kinto-updater/git/refs{/sha}","trees_url":"https://api.github.com/repos/Kinto/kinto-updater/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Kinto/kinto-updater/statuses/{sha}","languages_url":"https://api.github.com/repos/Kinto/kinto-updater/languages","stargazers_url":"https://api.github.com/repos/Kinto/kinto-updater/stargazers","contributors_url":"https://api.github.com/repos/Kinto/kinto-updater/contributors","subscribers_url":"https://api.github.com/repos/Kinto/kinto-updater/subscribers","subscription_url":"https://api.github.com/repos/Kinto/kinto-updater/subscription","commits_url":"https://api.github.com/repos/Kinto/kinto-updater/commits{/sha}","git_commits_url":"https://api.github.com/repos/Kinto/kinto-updater/git/commits{/sha}","comments_url":"https://api.github.com/repos/Kinto/kinto-updater/comments{/number}","issue_comment_url":"https://api.github.com/repos/Kinto/kinto-updater/issues/comments{/number}","contents_url":"https://api.github.com/repos/Kinto/kinto-updater/contents/{+path}","compare_url":"https://api.github.com/repos/Kinto/kinto-updater/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Kinto/kinto-updater/merges","archive_url":"https://api.github.com/repos/Kinto/kinto-updater/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Kinto/kinto-updater/downloads","issues_url":"https://api.github.com/repos/Kinto/kinto-updater/issues{/number}","pulls_url":"https://api.github.com/repos/Kinto/kinto-updater/pulls{/number}","milestones_url":"https://api.github.com/repos/Kinto/kinto-updater/milestones{/number}","notifications_url":"https://api.github.com/repos/Kinto/kinto-updater/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Kinto/kinto-updater/labels{/name}","releases_url":"https://api.github.com/repos/Kinto/kinto-updater/releases{/id}","created_at":"2015-09-22T13:21:26Z","updated_at":"2015-09-22T13:27:51Z","pushed_at":"2015-09-29T08:21:51Z","git_url":"git://github.com/Kinto/kinto-updater.git","ssh_url":"git@github.com:Kinto/kinto-updater.git","clone_url":"https://github.com/Kinto/kinto-updater.git","svn_url":"https://github.com/Kinto/kinto-updater","homepage":"","size":0,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/Kinto/kinto-updater/pulls/1"},"html":{"href":"https://github.com/Kinto/kinto-updater/pull/1"},"issue":{"href":"https://api.github.com/repos/Kinto/kinto-updater/issues/1"},"comments":{"href":"https://api.github.com/repos/Kinto/kinto-updater/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/Kinto/kinto-updater/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/Kinto/kinto-updater/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/Kinto/kinto-updater/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/Kinto/kinto-updater/statuses/0314f933f514565d0eb77077739de053925dda52"}}}}