Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for plugin only request during initialization #93

Merged
merged 2 commits into from
Nov 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions skygear/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def _request_url(self, action_name):
endpoint = endpoint[:-1] if endpoint[-1] == '/' else endpoint
return endpoint + '/' + action_name.replace(':', '/')

def _payload(self, action_name, params):
def _payload(self, action_name, params, plugin_request):
payload = params.copy() if isinstance(params, dict) else {}
payload['action'] = action_name
if self.access_token:
Expand All @@ -66,6 +66,8 @@ def _payload(self, action_name, params):
payload['api_key'] = self.api_key
if self.user_id:
payload['_user_id'] = self.user_id
if plugin_request:
payload['_from_plugin'] = True
return payload

@classmethod
Expand All @@ -84,9 +86,9 @@ def set_default_endpoint(cls, endpoint):
def set_default_apikey(cls, api_key):
cls.api_key = api_key

def send_action(self, action_name, params):
def send_action(self, action_name, params, plugin_request=False):
resp = send_action(self._request_url(action_name),
self._payload(action_name, params))
self._payload(action_name, params, plugin_request))
if 'error' in resp:
raise error.SkygearException.from_dict(resp['error'])

Expand Down
2 changes: 2 additions & 0 deletions skygear/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
UndefinedOperation = 117
PluginUnavailable = 118
PluginTimeout = 119
RecordQueryInvalid = 120
PluginInitializing = 121

UnexpectedError = 10000

Expand Down
19 changes: 14 additions & 5 deletions skygear/tests/test_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,32 +49,41 @@ def test_send_date(self, mock_requests):
class TestContainer():
def test_payload_include_action(self):
c = SkygearContainer(endpoint='endpoint', access_token='access-token')
payload = c._payload('action:work', {'data': 'hello world'})
payload = c._payload('action:work', {'data': 'hello world'}, False)
assert payload['action'] == 'action:work'
assert payload['data'] == 'hello world'

def test_payload_include_api_key(self):
c = SkygearContainer(endpoint='endpoint', api_key='api-key')
payload = c._payload('action:work', {})
payload = c._payload('action:work', {}, False)
assert payload['api_key'] == 'api-key'

def test_payload_include_user_id(self):
c = SkygearContainer(endpoint='endpoint', api_key='api-key',
user_id='user-id')
payload = c._payload('action:work', {})
payload = c._payload('action:work', {}, False)
assert payload['_user_id'] == 'user-id'

def test_payload_include_access_token(self):
c = SkygearContainer(endpoint='endpoint', access_token='access-token')
payload = c._payload('action:work', {})
payload = c._payload('action:work', {}, False)
assert payload['access_token'] == 'access-token'

def test_payload_include_all_credentials(self):
c = SkygearContainer(endpoint='endpoint',
access_token='access-token',
api_key='api-key',
user_id='user-id')
payload = c._payload('action:work', {})
payload = c._payload('action:work', {}, False)
assert payload['access_token'] == 'access-token'
assert payload['api_key'] == 'api-key'
assert payload['_user_id'] == 'user-id'

def test_plugin_request_payload(self):
c = SkygearContainer(endpoint='endpoint',
access_token='access-token',
api_key='master-key')
payload = c._payload('action:work', {}, True)
assert payload['access_token'] == 'access-token'
assert payload['api_key'] == 'master-key'
assert payload['_from_plugin']