{"action":"created","comment":{"url":"https://api.github.com/repos/biocore/american-gut-web/pulls/comments/60949695","id":60949695,"diff_hunk":"@@ -0,0 +1,115 @@\n+from urllib import urlencode\n+from json import loads\n+\n+from tornado.web import HTTPError\n+from tornado.httpclient import HTTPClient\n+\n+from amgut.lib.config_manager import AMGUT_CONFIG\n+from amgut.lib.data_access.sql_connection import TRN\n+\n+\n+def _make_request(data):\n+    body = urlencode(data)\n+    client = HTTPClient()\n+    response = client.fetch(AMGUT_CONFIG.redcap_url, method='POST',\n+                            headers=None, body=body)\n+    # Response not always JSON, so can't always use loads\n+    if '{\"error\"' in response.body:\n+        raise HTTPError(400, loads(response.body)['error'])\n+    return response.body\n+\n+\n+def get_survey_url(record, instrument='ag_human_en_us'):\n+    \"\"\"Genereates a new survey link for a given record\n+\n+    Parameters\n+    ----------\n+    record : int\n+        Redcap record (consent) to attach this survey to\n+    instrument : str, optional\n+        The instrument (survey) to get the URL for. Default ag_human_en_us\n+\n+    Returns\n+    -------\n+    str\n+        URL for the survey\n+    \"\"\"\n+    with TRN:\n+        # Get the event ID for the survey, or 1 if first survey\n+        sql = \"\"\"SELECT MAX(redcap_event_id) + 1\n+                 FROM ag.ag_login_surveys\n+                 LEFT JOIN ag.ag_consent USING (redcap_record_id)\n+                 LEFT JOIN redcap_instruments\n+                    USING (redcap_instrument_id, lang)\n+                 WHERE redcap_record_id = %s AND redcap_instrument_id = %s;\n+        \"\"\"\n+        TRN.add(sql, [record, instrument])\n+        event = TRN.execute_fetchlast()\n+        if event is None:\n+            event = 1\n+        data = {\n+            'token': AMGUT_CONFIG.redcap_token,\n+            'content': 'surveyLink',\n+            'format': 'json',\n+            'instrument': 'ag_human',\n+            'event': 'event_%d_arm_1' % event,\n+            'record': record,\n+            'returnFormat': 'json'\n+        }\n+        return _make_request(data)\n+\n+\n+def log_complete(record, instrument, event):\n+    \"\"\"Logs a redcap survey as completed in the database\n+\n+    Parameters\n+    ----------\n+    records : list of int\n+        record to log for\n+    instrument : str\n+        The instrument (survey) to log for\n+    event : int\n+        Event to log for\n+    \"\"\"\n+    with TRN:","path":"amgut/lib/data_access/redcap.py","position":74,"original_position":74,"commit_id":"6afc11020fe2322bdd9b15fd83fd0a431feb0f43","original_commit_id":"6afc11020fe2322bdd9b15fd83fd0a431feb0f43","user":{"login":"wasade","id":474290,"avatar_url":"https://avatars.githubusercontent.com/u/474290?v=3","gravatar_id":"","url":"https://api.github.com/users/wasade","html_url":"https://github.com/wasade","followers_url":"https://api.github.com/users/wasade/followers","following_url":"https://api.github.com/users/wasade/following{/other_user}","gists_url":"https://api.github.com/users/wasade/gists{/gist_id}","starred_url":"https://api.github.com/users/wasade/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wasade/subscriptions","organizations_url":"https://api.github.com/users/wasade/orgs","repos_url":"https://api.github.com/users/wasade/repos","events_url":"https://api.github.com/users/wasade/events{/privacy}","received_events_url":"https://api.github.com/users/wasade/received_events","type":"User","site_admin":false},"body":"Ya, so, the insert is in `log_complete`. It is possible that multiple executions of `get_survey_url` can result in the same `redcap_event_id`. If/when that happens, this code will except assuming that `redcap_instrument_id` and `redcap_record_id` form a primary key","created_at":"2016-04-25T17:07:01Z","updated_at":"2016-04-25T17:07:01Z","html_url":"https://github.com/biocore/american-gut-web/pull/579#discussion_r60949695","pull_request_url":"https://api.github.com/repos/biocore/american-gut-web/pulls/579","_links":{"self":{"href":"https://api.github.com/repos/biocore/american-gut-web/pulls/comments/60949695"},"html":{"href":"https://github.com/biocore/american-gut-web/pull/579#discussion_r60949695"},"pull_request":{"href":"https://api.github.com/repos/biocore/american-gut-web/pulls/579"}}},"pull_request":{"url":"https://api.github.com/repos/biocore/american-gut-web/pulls/579","id":63264015,"html_url":"https://github.com/biocore/american-gut-web/pull/579","diff_url":"https://github.com/biocore/american-gut-web/pull/579.diff","patch_url":"https://github.com/biocore/american-gut-web/pull/579.patch","issue_url":"https://api.github.com/repos/biocore/american-gut-web/issues/579","number":579,"state":"open","locked":false,"title":"Redcap survey links","user":{"login":"squirrelo","id":3079066,"avatar_url":"https://avatars.githubusercontent.com/u/3079066?v=3","gravatar_id":"","url":"https://api.github.com/users/squirrelo","html_url":"https://github.com/squirrelo","followers_url":"https://api.github.com/users/squirrelo/followers","following_url":"https://api.github.com/users/squirrelo/following{/other_user}","gists_url":"https://api.github.com/users/squirrelo/gists{/gist_id}","starred_url":"https://api.github.com/users/squirrelo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/squirrelo/subscriptions","organizations_url":"https://api.github.com/users/squirrelo/orgs","repos_url":"https://api.github.com/users/squirrelo/repos","events_url":"https://api.github.com/users/squirrelo/events{/privacy}","received_events_url":"https://api.github.com/users/squirrelo/received_events","type":"User","site_admin":false},"body":"Adds helper functions and config options for back and forth communication with redcap API\r\n~~Requires #570~~ Merged ","created_at":"2016-03-17T17:46:01Z","updated_at":"2016-04-25T17:07:01Z","closed_at":null,"merged_at":null,"merge_commit_sha":"2b175f2af45e9f02c54968f9a7a58da6c3c5f206","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/biocore/american-gut-web/pulls/579/commits","review_comments_url":"https://api.github.com/repos/biocore/american-gut-web/pulls/579/comments","review_comment_url":"https://api.github.com/repos/biocore/american-gut-web/pulls/comments{/number}","comments_url":"https://api.github.com/repos/biocore/american-gut-web/issues/579/comments","statuses_url":"https://api.github.com/repos/biocore/american-gut-web/statuses/6afc11020fe2322bdd9b15fd83fd0a431feb0f43","head":{"label":"squirrelo:redcap_survey_links","ref":"redcap_survey_links","sha":"6afc11020fe2322bdd9b15fd83fd0a431feb0f43","user":{"login":"squirrelo","id":3079066,"avatar_url":"https://avatars.githubusercontent.com/u/3079066?v=3","gravatar_id":"","url":"https://api.github.com/users/squirrelo","html_url":"https://github.com/squirrelo","followers_url":"https://api.github.com/users/squirrelo/followers","following_url":"https://api.github.com/users/squirrelo/following{/other_user}","gists_url":"https://api.github.com/users/squirrelo/gists{/gist_id}","starred_url":"https://api.github.com/users/squirrelo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/squirrelo/subscriptions","organizations_url":"https://api.github.com/users/squirrelo/orgs","repos_url":"https://api.github.com/users/squirrelo/repos","events_url":"https://api.github.com/users/squirrelo/events{/privacy}","received_events_url":"https://api.github.com/users/squirrelo/received_events","type":"User","site_admin":false},"repo":{"id":22621217,"name":"american-gut-web","full_name":"squirrelo/american-gut-web","owner":{"login":"squirrelo","id":3079066,"avatar_url":"https://avatars.githubusercontent.com/u/3079066?v=3","gravatar_id":"","url":"https://api.github.com/users/squirrelo","html_url":"https://github.com/squirrelo","followers_url":"https://api.github.com/users/squirrelo/followers","following_url":"https://api.github.com/users/squirrelo/following{/other_user}","gists_url":"https://api.github.com/users/squirrelo/gists{/gist_id}","starred_url":"https://api.github.com/users/squirrelo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/squirrelo/subscriptions","organizations_url":"https://api.github.com/users/squirrelo/orgs","repos_url":"https://api.github.com/users/squirrelo/repos","events_url":"https://api.github.com/users/squirrelo/events{/privacy}","received_events_url":"https://api.github.com/users/squirrelo/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/squirrelo/american-gut-web","description":"The website for the American Gut Project participant portal","fork":true,"url":"https://api.github.com/repos/squirrelo/american-gut-web","forks_url":"https://api.github.com/repos/squirrelo/american-gut-web/forks","keys_url":"https://api.github.com/repos/squirrelo/american-gut-web/keys{/key_id}","collaborators_url":"https://api.github.com/repos/squirrelo/american-gut-web/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/squirrelo/american-gut-web/teams","hooks_url":"https://api.github.com/repos/squirrelo/american-gut-web/hooks","issue_events_url":"https://api.github.com/repos/squirrelo/american-gut-web/issues/events{/number}","events_url":"https://api.github.com/repos/squirrelo/american-gut-web/events","assignees_url":"https://api.github.com/repos/squirrelo/american-gut-web/assignees{/user}","branches_url":"https://api.github.com/repos/squirrelo/american-gut-web/branches{/branch}","tags_url":"https://api.github.com/repos/squirrelo/american-gut-web/tags","blobs_url":"https://api.github.com/repos/squirrelo/american-gut-web/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/squirrelo/american-gut-web/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/squirrelo/american-gut-web/git/refs{/sha}","trees_url":"https://api.github.com/repos/squirrelo/american-gut-web/git/trees{/sha}","statuses_url":"https://api.github.com/repos/squirrelo/american-gut-web/statuses/{sha}","languages_url":"https://api.github.com/repos/squirrelo/american-gut-web/languages","stargazers_url":"https://api.github.com/repos/squirrelo/american-gut-web/stargazers","contributors_url":"https://api.github.com/repos/squirrelo/american-gut-web/contributors","subscribers_url":"https://api.github.com/repos/squirrelo/american-gut-web/subscribers","subscription_url":"https://api.github.com/repos/squirrelo/american-gut-web/subscription","commits_url":"https://api.github.com/repos/squirrelo/american-gut-web/commits{/sha}","git_commits_url":"https://api.github.com/repos/squirrelo/american-gut-web/git/commits{/sha}","comments_url":"https://api.github.com/repos/squirrelo/american-gut-web/comments{/number}","issue_comment_url":"https://api.github.com/repos/squirrelo/american-gut-web/issues/comments{/number}","contents_url":"https://api.github.com/repos/squirrelo/american-gut-web/contents/{+path}","compare_url":"https://api.github.com/repos/squirrelo/american-gut-web/compare/{base}...{head}","merges_url":"https://api.github.com/repos/squirrelo/american-gut-web/merges","archive_url":"https://api.github.com/repos/squirrelo/american-gut-web/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/squirrelo/american-gut-web/downloads","issues_url":"https://api.github.com/repos/squirrelo/american-gut-web/issues{/number}","pulls_url":"https://api.github.com/repos/squirrelo/american-gut-web/pulls{/number}","milestones_url":"https://api.github.com/repos/squirrelo/american-gut-web/milestones{/number}","notifications_url":"https://api.github.com/repos/squirrelo/american-gut-web/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/squirrelo/american-gut-web/labels{/name}","releases_url":"https://api.github.com/repos/squirrelo/american-gut-web/releases{/id}","deployments_url":"https://api.github.com/repos/squirrelo/american-gut-web/deployments","created_at":"2014-08-04T21:49:51Z","updated_at":"2016-01-12T18:03:10Z","pushed_at":"2016-04-25T16:35:59Z","git_url":"git://github.com/squirrelo/american-gut-web.git","ssh_url":"git@github.com:squirrelo/american-gut-web.git","clone_url":"https://github.com/squirrelo/american-gut-web.git","svn_url":"https://github.com/squirrelo/american-gut-web","homepage":null,"size":79916,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"biocore:redcap-integration","ref":"redcap-integration","sha":"de4f3846742b76ec7539210aa4e2e490dff76378","user":{"login":"biocore","id":6241857,"avatar_url":"https://avatars.githubusercontent.com/u/6241857?v=3","gravatar_id":"","url":"https://api.github.com/users/biocore","html_url":"https://github.com/biocore","followers_url":"https://api.github.com/users/biocore/followers","following_url":"https://api.github.com/users/biocore/following{/other_user}","gists_url":"https://api.github.com/users/biocore/gists{/gist_id}","starred_url":"https://api.github.com/users/biocore/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/biocore/subscriptions","organizations_url":"https://api.github.com/users/biocore/orgs","repos_url":"https://api.github.com/users/biocore/repos","events_url":"https://api.github.com/users/biocore/events{/privacy}","received_events_url":"https://api.github.com/users/biocore/received_events","type":"Organization","site_admin":false},"repo":{"id":22621149,"name":"american-gut-web","full_name":"biocore/american-gut-web","owner":{"login":"biocore","id":6241857,"avatar_url":"https://avatars.githubusercontent.com/u/6241857?v=3","gravatar_id":"","url":"https://api.github.com/users/biocore","html_url":"https://github.com/biocore","followers_url":"https://api.github.com/users/biocore/followers","following_url":"https://api.github.com/users/biocore/following{/other_user}","gists_url":"https://api.github.com/users/biocore/gists{/gist_id}","starred_url":"https://api.github.com/users/biocore/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/biocore/subscriptions","organizations_url":"https://api.github.com/users/biocore/orgs","repos_url":"https://api.github.com/users/biocore/repos","events_url":"https://api.github.com/users/biocore/events{/privacy}","received_events_url":"https://api.github.com/users/biocore/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/biocore/american-gut-web","description":"The website for the American Gut Project participant portal","fork":false,"url":"https://api.github.com/repos/biocore/american-gut-web","forks_url":"https://api.github.com/repos/biocore/american-gut-web/forks","keys_url":"https://api.github.com/repos/biocore/american-gut-web/keys{/key_id}","collaborators_url":"https://api.github.com/repos/biocore/american-gut-web/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/biocore/american-gut-web/teams","hooks_url":"https://api.github.com/repos/biocore/american-gut-web/hooks","issue_events_url":"https://api.github.com/repos/biocore/american-gut-web/issues/events{/number}","events_url":"https://api.github.com/repos/biocore/american-gut-web/events","assignees_url":"https://api.github.com/repos/biocore/american-gut-web/assignees{/user}","branches_url":"https://api.github.com/repos/biocore/american-gut-web/branches{/branch}","tags_url":"https://api.github.com/repos/biocore/american-gut-web/tags","blobs_url":"https://api.github.com/repos/biocore/american-gut-web/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/biocore/american-gut-web/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/biocore/american-gut-web/git/refs{/sha}","trees_url":"https://api.github.com/repos/biocore/american-gut-web/git/trees{/sha}","statuses_url":"https://api.github.com/repos/biocore/american-gut-web/statuses/{sha}","languages_url":"https://api.github.com/repos/biocore/american-gut-web/languages","stargazers_url":"https://api.github.com/repos/biocore/american-gut-web/stargazers","contributors_url":"https://api.github.com/repos/biocore/american-gut-web/contributors","subscribers_url":"https://api.github.com/repos/biocore/american-gut-web/subscribers","subscription_url":"https://api.github.com/repos/biocore/american-gut-web/subscription","commits_url":"https://api.github.com/repos/biocore/american-gut-web/commits{/sha}","git_commits_url":"https://api.github.com/repos/biocore/american-gut-web/git/commits{/sha}","comments_url":"https://api.github.com/repos/biocore/american-gut-web/comments{/number}","issue_comment_url":"https://api.github.com/repos/biocore/american-gut-web/issues/comments{/number}","contents_url":"https://api.github.com/repos/biocore/american-gut-web/contents/{+path}","compare_url":"https://api.github.com/repos/biocore/american-gut-web/compare/{base}...{head}","merges_url":"https://api.github.com/repos/biocore/american-gut-web/merges","archive_url":"https://api.github.com/repos/biocore/american-gut-web/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/biocore/american-gut-web/downloads","issues_url":"https://api.github.com/repos/biocore/american-gut-web/issues{/number}","pulls_url":"https://api.github.com/repos/biocore/american-gut-web/pulls{/number}","milestones_url":"https://api.github.com/repos/biocore/american-gut-web/milestones{/number}","notifications_url":"https://api.github.com/repos/biocore/american-gut-web/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/biocore/american-gut-web/labels{/name}","releases_url":"https://api.github.com/repos/biocore/american-gut-web/releases{/id}","deployments_url":"https://api.github.com/repos/biocore/american-gut-web/deployments","created_at":"2014-08-04T21:47:22Z","updated_at":"2016-01-13T18:28:33Z","pushed_at":"2016-04-25T16:35:59Z","git_url":"git://github.com/biocore/american-gut-web.git","ssh_url":"git@github.com:biocore/american-gut-web.git","clone_url":"https://github.com/biocore/american-gut-web.git","svn_url":"https://github.com/biocore/american-gut-web","homepage":null,"size":105739,"stargazers_count":4,"watchers_count":4,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":13,"mirror_url":null,"open_issues_count":46,"forks":13,"open_issues":46,"watchers":4,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/biocore/american-gut-web/pulls/579"},"html":{"href":"https://github.com/biocore/american-gut-web/pull/579"},"issue":{"href":"https://api.github.com/repos/biocore/american-gut-web/issues/579"},"comments":{"href":"https://api.github.com/repos/biocore/american-gut-web/issues/579/comments"},"review_comments":{"href":"https://api.github.com/repos/biocore/american-gut-web/pulls/579/comments"},"review_comment":{"href":"https://api.github.com/repos/biocore/american-gut-web/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/biocore/american-gut-web/pulls/579/commits"},"statuses":{"href":"https://api.github.com/repos/biocore/american-gut-web/statuses/6afc11020fe2322bdd9b15fd83fd0a431feb0f43"}}}}