{"action":"created","comment":{"url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/pulls/comments/53781594","id":53781594,"diff_hunk":"@@ -1 +1,121 @@\n-// Your code will go here.\n+\"use strict\";\n+\n+import md5 from \"md5\";\n+import uuid from \"uuid\";\n+import \"isomorphic-fetch\";\n+\n+var localStorage;\n+if (typeof (localStorage) == \"undefined\" || localStorage === null) {\n+  var LocalStorage = require('node-localstorage').LocalStorage;\n+  localStorage = new LocalStorage('./lib/localStorage');\n+}\n+ function parseHexString(str) {\n+  var result = [];\n+  while (str.length >= 2) {\n+    result.push(parseInt(str.substring(0, 2), 16));\n+    str = str.substring(2, str.length);\n+  }\n+  return result;\n+}\n+\n+export function getUserIDHash(userID) {\n+  var hash = md5(userID);\n+  var input = parseHexString(hash);\n+  return uuid.v4({random: input});\n+};\n+\n+export function registerUserURL(userID, centralRepositoryURL, headers, userStorageURL){\n+\n+  var user_record_id = getUserIDHash(userID);\n+\n+  var key = 'kinto:server-url:' + userID;\n+  var cachedURL = localStorage.getItem(key);\n+  if(cachedURL != null){\n+    return Promise.resolve(cachedURL);\n+  }\n+\n+  let url = centralRepositoryURL + user_record_id;\n+  var body = JSON.stringify({\n+      data:{\n+        url: userStorageURL\n+      }\n+  });\n+\n+  if (!headers.hasOwnProperty(\"Authorization\"))\n+   {\n+      return Promise.reject(new Error(\"Missing Authorization header\"));\n+   }\n+\n+    return fetch(url,{\n+    method:'put',\n+    headers,\n+    body\n+  })\n+\n+    .then(function(response) {\n+      if (response.status >=200 && response.status <300){\n+\n+        return Promise.resolve(userStorageURL);\n+      }\n+      if (response.status == 401){\n+        throw Promise.reject(new Error(\"invalid headers\"));\n+      }\n+      if(response.status >=500){\n+        throw Promise.reject(new Error(\"server not reachable\"));\n+      }\n+      return Promise.reject(new Error(\"does not fit any case\"));\n+    })\n+    .then(function(userStorageURL){\n+      localStorage.setItem(key, userStorageURL);\n+\n+      return Promise.resolve(userStorageURL);\n+    })\n+    .catch(function(error) {\n+        return error;\n+    });\n+};\n+\n+\n+export function retrieveUserURL(userID, centralRepositoryURL, headers, defaultServer){\n+  if (defaultServer == ''){\n+    return \"default server is null\";\n+  }\n+  var user_record_id = getUserIDHash(userID);\n+  var key = 'kinto:server-url:' + userID;\n+  var cachedURL = localStorage.getItem(key);\n+  if(cachedURL != null){\n+    return Promise.resolve(cachedURL);\n+  }\n+  if (!headers.hasOwnProperty(\"Authorization\"))\n+   {        return Promise.reject(new Error(\"Missing Authorization header\"));\n+   }\n+\n+\n+\n+  let url = centralRepositoryURL + user_record_id;\n+\n+  return fetch(url, {headers})\n+    .then(function(response) {\n+      if (response.status >=200 && response.status <300){\n+        return response.json();\n+\n+      }\n+      if (response.status == 404 || response.status == 403){\n+        return Promise.resolve({data: {url: defaultServer}});\n+      }\n+      if (response.status == 401){\n+        return Promise.reject(new Error(\"invalid headers\"));","path":"lib/index.js","position":108,"original_position":108,"commit_id":"0d5559ae47972d1328672dca0a0cb5c0ad149d08","original_commit_id":"0d5559ae47972d1328672dca0a0cb5c0ad149d08","user":{"login":"Natim","id":229453,"avatar_url":"https://avatars.githubusercontent.com/u/229453?v=3","gravatar_id":"","url":"https://api.github.com/users/Natim","html_url":"https://github.com/Natim","followers_url":"https://api.github.com/users/Natim/followers","following_url":"https://api.github.com/users/Natim/following{/other_user}","gists_url":"https://api.github.com/users/Natim/gists{/gist_id}","starred_url":"https://api.github.com/users/Natim/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Natim/subscriptions","organizations_url":"https://api.github.com/users/Natim/orgs","repos_url":"https://api.github.com/users/Natim/repos","events_url":"https://api.github.com/users/Natim/events{/privacy}","received_events_url":"https://api.github.com/users/Natim/received_events","type":"User","site_admin":false},"body":"nit: ``invalid Authorization header``","created_at":"2016-02-23T13:52:55Z","updated_at":"2016-02-23T13:52:55Z","html_url":"https://github.com/Kinto/kinto-discovery-lib/pull/2#discussion_r53781594","pull_request_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/pulls/2","_links":{"self":{"href":"https://api.github.com/repos/Kinto/kinto-discovery-lib/pulls/comments/53781594"},"html":{"href":"https://github.com/Kinto/kinto-discovery-lib/pull/2#discussion_r53781594"},"pull_request":{"href":"https://api.github.com/repos/Kinto/kinto-discovery-lib/pulls/2"}}},"pull_request":{"url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/pulls/2","id":59355471,"html_url":"https://github.com/Kinto/kinto-discovery-lib/pull/2","diff_url":"https://github.com/Kinto/kinto-discovery-lib/pull/2.diff","patch_url":"https://github.com/Kinto/kinto-discovery-lib/pull/2.patch","issue_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/issues/2","number":2,"state":"open","locked":false,"title":"Trial","user":{"login":"oak11","id":11483384,"avatar_url":"https://avatars.githubusercontent.com/u/11483384?v=3","gravatar_id":"","url":"https://api.github.com/users/oak11","html_url":"https://github.com/oak11","followers_url":"https://api.github.com/users/oak11/followers","following_url":"https://api.github.com/users/oak11/following{/other_user}","gists_url":"https://api.github.com/users/oak11/gists{/gist_id}","starred_url":"https://api.github.com/users/oak11/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/oak11/subscriptions","organizations_url":"https://api.github.com/users/oak11/orgs","repos_url":"https://api.github.com/users/oak11/repos","events_url":"https://api.github.com/users/oak11/events{/privacy}","received_events_url":"https://api.github.com/users/oak11/received_events","type":"User","site_admin":false},"body":"","created_at":"2016-02-15T12:07:48Z","updated_at":"2016-02-23T13:52:55Z","closed_at":null,"merged_at":null,"merge_commit_sha":null,"assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/pulls/2/commits","review_comments_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/pulls/2/comments","review_comment_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/pulls/comments{/number}","comments_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/issues/2/comments","statuses_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/statuses/0d5559ae47972d1328672dca0a0cb5c0ad149d08","head":{"label":"Kinto:trial","ref":"trial","sha":"0d5559ae47972d1328672dca0a0cb5c0ad149d08","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":51069408,"name":"kinto-discovery-lib","full_name":"Kinto/kinto-discovery-lib","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-discovery-lib","description":null,"fork":false,"url":"https://api.github.com/repos/Kinto/kinto-discovery-lib","forks_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/forks","keys_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/teams","hooks_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/hooks","issue_events_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/issues/events{/number}","events_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/events","assignees_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/assignees{/user}","branches_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/branches{/branch}","tags_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/tags","blobs_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/git/refs{/sha}","trees_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/statuses/{sha}","languages_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/languages","stargazers_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/stargazers","contributors_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/contributors","subscribers_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/subscribers","subscription_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/subscription","commits_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/commits{/sha}","git_commits_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/git/commits{/sha}","comments_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/comments{/number}","issue_comment_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/issues/comments{/number}","contents_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/contents/{+path}","compare_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/merges","archive_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/downloads","issues_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/issues{/number}","pulls_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/pulls{/number}","milestones_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/milestones{/number}","notifications_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/labels{/name}","releases_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/releases{/id}","deployments_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/deployments","created_at":"2016-02-04T10:45:48Z","updated_at":"2016-02-04T10:45:58Z","pushed_at":"2016-02-23T11:24:38Z","git_url":"git://github.com/Kinto/kinto-discovery-lib.git","ssh_url":"git@github.com:Kinto/kinto-discovery-lib.git","clone_url":"https://github.com/Kinto/kinto-discovery-lib.git","svn_url":"https://github.com/Kinto/kinto-discovery-lib","homepage":null,"size":13,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":2,"forks":0,"open_issues":2,"watchers":0,"default_branch":"master"}},"base":{"label":"Kinto:master","ref":"master","sha":"452cfe1bda49d97b7cc06903108096ac5f970b9e","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":51069408,"name":"kinto-discovery-lib","full_name":"Kinto/kinto-discovery-lib","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-discovery-lib","description":null,"fork":false,"url":"https://api.github.com/repos/Kinto/kinto-discovery-lib","forks_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/forks","keys_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/teams","hooks_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/hooks","issue_events_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/issues/events{/number}","events_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/events","assignees_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/assignees{/user}","branches_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/branches{/branch}","tags_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/tags","blobs_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/git/refs{/sha}","trees_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/statuses/{sha}","languages_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/languages","stargazers_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/stargazers","contributors_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/contributors","subscribers_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/subscribers","subscription_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/subscription","commits_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/commits{/sha}","git_commits_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/git/commits{/sha}","comments_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/comments{/number}","issue_comment_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/issues/comments{/number}","contents_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/contents/{+path}","compare_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/merges","archive_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/downloads","issues_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/issues{/number}","pulls_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/pulls{/number}","milestones_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/milestones{/number}","notifications_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/labels{/name}","releases_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/releases{/id}","deployments_url":"https://api.github.com/repos/Kinto/kinto-discovery-lib/deployments","created_at":"2016-02-04T10:45:48Z","updated_at":"2016-02-04T10:45:58Z","pushed_at":"2016-02-23T11:24:38Z","git_url":"git://github.com/Kinto/kinto-discovery-lib.git","ssh_url":"git@github.com:Kinto/kinto-discovery-lib.git","clone_url":"https://github.com/Kinto/kinto-discovery-lib.git","svn_url":"https://github.com/Kinto/kinto-discovery-lib","homepage":null,"size":13,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":2,"forks":0,"open_issues":2,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/Kinto/kinto-discovery-lib/pulls/2"},"html":{"href":"https://github.com/Kinto/kinto-discovery-lib/pull/2"},"issue":{"href":"https://api.github.com/repos/Kinto/kinto-discovery-lib/issues/2"},"comments":{"href":"https://api.github.com/repos/Kinto/kinto-discovery-lib/issues/2/comments"},"review_comments":{"href":"https://api.github.com/repos/Kinto/kinto-discovery-lib/pulls/2/comments"},"review_comment":{"href":"https://api.github.com/repos/Kinto/kinto-discovery-lib/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/Kinto/kinto-discovery-lib/pulls/2/commits"},"statuses":{"href":"https://api.github.com/repos/Kinto/kinto-discovery-lib/statuses/0d5559ae47972d1328672dca0a0cb5c0ad149d08"}}}}