Skip to content

Commit

Permalink
Support for plugin only request during initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
cheungpat authored Nov 10, 2016
2 parents 5a8a2cd + 177320c commit 83195b2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
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']

0 comments on commit 83195b2

Please sign in to comment.