{"action":"created","comment":{"url":"https://api.github.com/repos/resin-io/resin-sdk-python/pulls/comments/43866156","id":43866156,"diff_hunk":"@@ -0,0 +1,363 @@\n+import os\n+\n+from ..base_request import BaseRequest\n+from ..settings import Settings\n+from .. import exceptions\n+\n+\n+class Supervisor(object):\n+    \"\"\"\n+    This class implements supervisor model for Resin Python SDK.\n+\n+    Attributes:\n+        SUPERVISOR_API_VERSION (str): supervisor API version.\n+        RESIN_SUPERVISOR_ADDRESS (str): supervisor endpoint address on device.\n+        RESIN_SUPERVISOR_API_KEY (str): supervisor API key on device.\n+        _on_device (bool): API endpoint flag.\n+            If True then all commands will be sent to the API on device.\n+            If False then all command will be sent to the Resin API proxy endpoint (api.resin.io/supervisor/<url>).\n+            If RESIN_SUPERVISOR_ADDRESS and RESIN_SUPERVISOR_API_KEY are available, _on_device will be set to True by default. Otherwise, it's False.\n+\n+    \"\"\"\n+\n+    SUPERVISOR_API_VERSION = 'v1'\n+    RESIN_SUPERVISOR_ADDRESS = os.environ.get('RESIN_SUPERVISOR_ADDRESS')\n+    RESIN_SUPERVISOR_API_KEY = os.environ.get('RESIN_SUPERVISOR_API_KEY')\n+\n+    # _on_device = True, if RESIN_SUPERVISOR_ADDRESS and RESIN_SUPERVISOR_API_KEY env vars are avaiable => can communicate with both supervisor API endpoints on device or Resin API endpoints.\n+    # _on_device = False, if RESIN_SUPERVISOR_ADDRESS and RESIN_SUPERVISOR_API_KEY env vars are not avaiable => can only communicate with Resin API endpoints.\n+    if RESIN_SUPERVISOR_ADDRESS is None or RESIN_SUPERVISOR_API_KEY is None:\n+        _on_device = False\n+    else:\n+        _on_device = True\n+\n+    def __init__(self):\n+        self.base_request = BaseRequest()\n+        self.settings = Settings()\n+\n+    def _check_args(self, device_id, app_id):\n+        if device_id is None:\n+            exceptions.MissingOption('device_id')\n+\n+        if app_id is None:\n+            exceptions.MissingOption('app_id')\n+\n+    def force_api_endpoint(self, endpoint):\n+        \"\"\"\n+        Force all API commands to a specific endpoint.\n+\n+        Args:\n+            endpoint (bool): True if selecting the API on device. False if selecting the Resin API proxy endpoint.\n+\n+        Raises:\n+            InvalidOption: if endpoint value is not bool.\n+\n+        \"\"\"\n+\n+        try:\n+            self._on_device = bool(endpoint)\n+        except ValueError:\n+            exceptions.InvalidOption('endpoint')\n+\n+    def ping(self, device_id=None, app_id=None):\n+        \"\"\"\n+        Check that the supervisor is alive and well.\n+        No need to set device_id and app_id if command is sent to the API on device.\n+\n+        Args:\n+            device_id (Optional[str]): device id.\n+            app_id (Optional[str]): application id.\n+\n+        Returns:\n+            str: `OK` signals that the supervisor is alive and well.\n+\n+        Raises:\n+            InvalidOption: if the endpoint is Resin API proxy endpoint and device_id or app_id is not set.\n+\n+        \"\"\"\n+\n+        if not self._on_device:","path":"resin/models/supervisor.py","position":79,"original_position":79,"commit_id":"46ea4103afc3dcb0d5306b7d2ae353313b69dec5","original_commit_id":"1003237db5d378447dc2545c6a948619f04664b5","user":{"login":"pcarranzav","id":2223590,"avatar_url":"https://avatars.githubusercontent.com/u/2223590?v=3","gravatar_id":"","url":"https://api.github.com/users/pcarranzav","html_url":"https://github.com/pcarranzav","followers_url":"https://api.github.com/users/pcarranzav/followers","following_url":"https://api.github.com/users/pcarranzav/following{/other_user}","gists_url":"https://api.github.com/users/pcarranzav/gists{/gist_id}","starred_url":"https://api.github.com/users/pcarranzav/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pcarranzav/subscriptions","organizations_url":"https://api.github.com/users/pcarranzav/orgs","repos_url":"https://api.github.com/users/pcarranzav/repos","events_url":"https://api.github.com/users/pcarranzav/events{/privacy}","received_events_url":"https://api.github.com/users/pcarranzav/received_events","type":"User","site_admin":false},"body":"You are repeating the \"if self._on_device\" check every time, this could be abstracted with a function like:\r\n```python\r\ndef do_request(self, endpoint, data, device_id=None, app_id=None, method='POST'):\r\n\tif not self._on_device:\r\n\t\tself._check_args(device_id, app_id)\r\n\t\tdata = {\r\n\t\t\t'deviceId': device_id,\r\n\t\t\t'appId': app_id,\r\n\t\t\t'method': method,\r\n\t\t\t'data': data\r\n\t\t}\r\n\t\treturn self.base_request.request(\r\n\t\t\t'supervisor/' + endpoint,\r\n\t\t\t'POST',\r\n\t\t\tdata=data,\r\n\t\t\tendpoint=self.settings.get('api_endpoint'),\r\n\t\t\tlogin=True\r\n\t\t)\r\n\telse:\r\n\t\treturn self.base_request.request(\r\n\t\t\tendpoint,\r\n\t\t\tmethod,\r\n\t\t\tendpoint=self.RESIN_SUPERVISOR_ADDRESS,\r\n\t\t\tapi_key=self.RESIN_SUPERVISOR_API_KEY\r\n\t\t)\r\n```\r\n\r\nThis would remove around 20 lines of duplicated code on every API-calling method. I will approve if you're convinced this doesn't make it more elegant, but I strongly suggest DRYing it up.","created_at":"2015-11-04T10:55:32Z","updated_at":"2015-11-04T10:55:32Z","html_url":"https://github.com/resin-io/resin-sdk-python/pull/15#discussion_r43866156","pull_request_url":"https://api.github.com/repos/resin-io/resin-sdk-python/pulls/15","_links":{"self":{"href":"https://api.github.com/repos/resin-io/resin-sdk-python/pulls/comments/43866156"},"html":{"href":"https://github.com/resin-io/resin-sdk-python/pull/15#discussion_r43866156"},"pull_request":{"href":"https://api.github.com/repos/resin-io/resin-sdk-python/pulls/15"}}},"pull_request":{"url":"https://api.github.com/repos/resin-io/resin-sdk-python/pulls/15","id":49006923,"html_url":"https://github.com/resin-io/resin-sdk-python/pull/15","diff_url":"https://github.com/resin-io/resin-sdk-python/pull/15.diff","patch_url":"https://github.com/resin-io/resin-sdk-python/pull/15.patch","issue_url":"https://api.github.com/repos/resin-io/resin-sdk-python/issues/15","number":15,"state":"open","locked":false,"title":"Implement 2fa support and resin supervisor","user":{"login":"nghiant2710","id":8970907,"avatar_url":"https://avatars.githubusercontent.com/u/8970907?v=3","gravatar_id":"","url":"https://api.github.com/users/nghiant2710","html_url":"https://github.com/nghiant2710","followers_url":"https://api.github.com/users/nghiant2710/followers","following_url":"https://api.github.com/users/nghiant2710/following{/other_user}","gists_url":"https://api.github.com/users/nghiant2710/gists{/gist_id}","starred_url":"https://api.github.com/users/nghiant2710/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nghiant2710/subscriptions","organizations_url":"https://api.github.com/users/nghiant2710/orgs","repos_url":"https://api.github.com/users/nghiant2710/repos","events_url":"https://api.github.com/users/nghiant2710/events{/privacy}","received_events_url":"https://api.github.com/users/nghiant2710/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-10-28T13:43:24Z","updated_at":"2015-11-04T10:55:32Z","closed_at":null,"merged_at":null,"merge_commit_sha":"4962b1590b9e9bfe02ce8a340ca6b38aa5ea04a2","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/resin-io/resin-sdk-python/pulls/15/commits","review_comments_url":"https://api.github.com/repos/resin-io/resin-sdk-python/pulls/15/comments","review_comment_url":"https://api.github.com/repos/resin-io/resin-sdk-python/pulls/comments{/number}","comments_url":"https://api.github.com/repos/resin-io/resin-sdk-python/issues/15/comments","statuses_url":"https://api.github.com/repos/resin-io/resin-sdk-python/statuses/46ea4103afc3dcb0d5306b7d2ae353313b69dec5","head":{"label":"nghiant2710:master","ref":"master","sha":"46ea4103afc3dcb0d5306b7d2ae353313b69dec5","user":{"login":"nghiant2710","id":8970907,"avatar_url":"https://avatars.githubusercontent.com/u/8970907?v=3","gravatar_id":"","url":"https://api.github.com/users/nghiant2710","html_url":"https://github.com/nghiant2710","followers_url":"https://api.github.com/users/nghiant2710/followers","following_url":"https://api.github.com/users/nghiant2710/following{/other_user}","gists_url":"https://api.github.com/users/nghiant2710/gists{/gist_id}","starred_url":"https://api.github.com/users/nghiant2710/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nghiant2710/subscriptions","organizations_url":"https://api.github.com/users/nghiant2710/orgs","repos_url":"https://api.github.com/users/nghiant2710/repos","events_url":"https://api.github.com/users/nghiant2710/events{/privacy}","received_events_url":"https://api.github.com/users/nghiant2710/received_events","type":"User","site_admin":false},"repo":{"id":41900541,"name":"resin-sdk-python","full_name":"nghiant2710/resin-sdk-python","owner":{"login":"nghiant2710","id":8970907,"avatar_url":"https://avatars.githubusercontent.com/u/8970907?v=3","gravatar_id":"","url":"https://api.github.com/users/nghiant2710","html_url":"https://github.com/nghiant2710","followers_url":"https://api.github.com/users/nghiant2710/followers","following_url":"https://api.github.com/users/nghiant2710/following{/other_user}","gists_url":"https://api.github.com/users/nghiant2710/gists{/gist_id}","starred_url":"https://api.github.com/users/nghiant2710/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nghiant2710/subscriptions","organizations_url":"https://api.github.com/users/nghiant2710/orgs","repos_url":"https://api.github.com/users/nghiant2710/repos","events_url":"https://api.github.com/users/nghiant2710/events{/privacy}","received_events_url":"https://api.github.com/users/nghiant2710/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/nghiant2710/resin-sdk-python","description":"Resin SDK for Python","fork":true,"url":"https://api.github.com/repos/nghiant2710/resin-sdk-python","forks_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/forks","keys_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/keys{/key_id}","collaborators_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/teams","hooks_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/hooks","issue_events_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/issues/events{/number}","events_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/events","assignees_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/assignees{/user}","branches_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/branches{/branch}","tags_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/tags","blobs_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/git/refs{/sha}","trees_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/git/trees{/sha}","statuses_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/statuses/{sha}","languages_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/languages","stargazers_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/stargazers","contributors_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/contributors","subscribers_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/subscribers","subscription_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/subscription","commits_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/commits{/sha}","git_commits_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/git/commits{/sha}","comments_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/comments{/number}","issue_comment_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/issues/comments{/number}","contents_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/contents/{+path}","compare_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/compare/{base}...{head}","merges_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/merges","archive_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/downloads","issues_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/issues{/number}","pulls_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/pulls{/number}","milestones_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/milestones{/number}","notifications_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/labels{/name}","releases_url":"https://api.github.com/repos/nghiant2710/resin-sdk-python/releases{/id}","created_at":"2015-09-04T06:08:12Z","updated_at":"2015-09-04T06:08:13Z","pushed_at":"2015-11-04T09:44:38Z","git_url":"git://github.com/nghiant2710/resin-sdk-python.git","ssh_url":"git@github.com:nghiant2710/resin-sdk-python.git","clone_url":"https://github.com/nghiant2710/resin-sdk-python.git","svn_url":"https://github.com/nghiant2710/resin-sdk-python","homepage":"","size":244,"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":"resin-io:master","ref":"master","sha":"3d74c73de67d44d8e6243838bb2d20dfc88342b6","user":{"login":"resin-io","id":6157842,"avatar_url":"https://avatars.githubusercontent.com/u/6157842?v=3","gravatar_id":"","url":"https://api.github.com/users/resin-io","html_url":"https://github.com/resin-io","followers_url":"https://api.github.com/users/resin-io/followers","following_url":"https://api.github.com/users/resin-io/following{/other_user}","gists_url":"https://api.github.com/users/resin-io/gists{/gist_id}","starred_url":"https://api.github.com/users/resin-io/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/resin-io/subscriptions","organizations_url":"https://api.github.com/users/resin-io/orgs","repos_url":"https://api.github.com/users/resin-io/repos","events_url":"https://api.github.com/users/resin-io/events{/privacy}","received_events_url":"https://api.github.com/users/resin-io/received_events","type":"Organization","site_admin":false},"repo":{"id":38891739,"name":"resin-sdk-python","full_name":"resin-io/resin-sdk-python","owner":{"login":"resin-io","id":6157842,"avatar_url":"https://avatars.githubusercontent.com/u/6157842?v=3","gravatar_id":"","url":"https://api.github.com/users/resin-io","html_url":"https://github.com/resin-io","followers_url":"https://api.github.com/users/resin-io/followers","following_url":"https://api.github.com/users/resin-io/following{/other_user}","gists_url":"https://api.github.com/users/resin-io/gists{/gist_id}","starred_url":"https://api.github.com/users/resin-io/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/resin-io/subscriptions","organizations_url":"https://api.github.com/users/resin-io/orgs","repos_url":"https://api.github.com/users/resin-io/repos","events_url":"https://api.github.com/users/resin-io/events{/privacy}","received_events_url":"https://api.github.com/users/resin-io/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/resin-io/resin-sdk-python","description":"Resin SDK for Python","fork":false,"url":"https://api.github.com/repos/resin-io/resin-sdk-python","forks_url":"https://api.github.com/repos/resin-io/resin-sdk-python/forks","keys_url":"https://api.github.com/repos/resin-io/resin-sdk-python/keys{/key_id}","collaborators_url":"https://api.github.com/repos/resin-io/resin-sdk-python/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/resin-io/resin-sdk-python/teams","hooks_url":"https://api.github.com/repos/resin-io/resin-sdk-python/hooks","issue_events_url":"https://api.github.com/repos/resin-io/resin-sdk-python/issues/events{/number}","events_url":"https://api.github.com/repos/resin-io/resin-sdk-python/events","assignees_url":"https://api.github.com/repos/resin-io/resin-sdk-python/assignees{/user}","branches_url":"https://api.github.com/repos/resin-io/resin-sdk-python/branches{/branch}","tags_url":"https://api.github.com/repos/resin-io/resin-sdk-python/tags","blobs_url":"https://api.github.com/repos/resin-io/resin-sdk-python/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/resin-io/resin-sdk-python/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/resin-io/resin-sdk-python/git/refs{/sha}","trees_url":"https://api.github.com/repos/resin-io/resin-sdk-python/git/trees{/sha}","statuses_url":"https://api.github.com/repos/resin-io/resin-sdk-python/statuses/{sha}","languages_url":"https://api.github.com/repos/resin-io/resin-sdk-python/languages","stargazers_url":"https://api.github.com/repos/resin-io/resin-sdk-python/stargazers","contributors_url":"https://api.github.com/repos/resin-io/resin-sdk-python/contributors","subscribers_url":"https://api.github.com/repos/resin-io/resin-sdk-python/subscribers","subscription_url":"https://api.github.com/repos/resin-io/resin-sdk-python/subscription","commits_url":"https://api.github.com/repos/resin-io/resin-sdk-python/commits{/sha}","git_commits_url":"https://api.github.com/repos/resin-io/resin-sdk-python/git/commits{/sha}","comments_url":"https://api.github.com/repos/resin-io/resin-sdk-python/comments{/number}","issue_comment_url":"https://api.github.com/repos/resin-io/resin-sdk-python/issues/comments{/number}","contents_url":"https://api.github.com/repos/resin-io/resin-sdk-python/contents/{+path}","compare_url":"https://api.github.com/repos/resin-io/resin-sdk-python/compare/{base}...{head}","merges_url":"https://api.github.com/repos/resin-io/resin-sdk-python/merges","archive_url":"https://api.github.com/repos/resin-io/resin-sdk-python/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/resin-io/resin-sdk-python/downloads","issues_url":"https://api.github.com/repos/resin-io/resin-sdk-python/issues{/number}","pulls_url":"https://api.github.com/repos/resin-io/resin-sdk-python/pulls{/number}","milestones_url":"https://api.github.com/repos/resin-io/resin-sdk-python/milestones{/number}","notifications_url":"https://api.github.com/repos/resin-io/resin-sdk-python/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/resin-io/resin-sdk-python/labels{/name}","releases_url":"https://api.github.com/repos/resin-io/resin-sdk-python/releases{/id}","created_at":"2015-07-10T16:54:48Z","updated_at":"2015-09-08T07:05:15Z","pushed_at":"2015-11-04T09:44:39Z","git_url":"git://github.com/resin-io/resin-sdk-python.git","ssh_url":"git@github.com:resin-io/resin-sdk-python.git","clone_url":"https://github.com/resin-io/resin-sdk-python.git","svn_url":"https://github.com/resin-io/resin-sdk-python","homepage":"","size":429,"stargazers_count":1,"watchers_count":1,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":4,"mirror_url":null,"open_issues_count":1,"forks":4,"open_issues":1,"watchers":1,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/resin-io/resin-sdk-python/pulls/15"},"html":{"href":"https://github.com/resin-io/resin-sdk-python/pull/15"},"issue":{"href":"https://api.github.com/repos/resin-io/resin-sdk-python/issues/15"},"comments":{"href":"https://api.github.com/repos/resin-io/resin-sdk-python/issues/15/comments"},"review_comments":{"href":"https://api.github.com/repos/resin-io/resin-sdk-python/pulls/15/comments"},"review_comment":{"href":"https://api.github.com/repos/resin-io/resin-sdk-python/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/resin-io/resin-sdk-python/pulls/15/commits"},"statuses":{"href":"https://api.github.com/repos/resin-io/resin-sdk-python/statuses/46ea4103afc3dcb0d5306b7d2ae353313b69dec5"}}}}