Skip to content
Merged
Next Next commit
[LIB-656] add cache_key support for data endpoints and script endpoints;
  • Loading branch information
opalczynski committed Jun 7, 2016
commit d938dcd0efb3c3050072c5b0141414df44b2a725
9 changes: 7 additions & 2 deletions syncano/models/data_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,17 @@ def clear_cache(self):
connection = self._get_connection()
return connection.request('POST', endpoint)

def get(self):
def get(self, cache_key=None):
properties = self.get_endpoint_data()
endpoint = self._meta.resolve_endpoint('get', properties)
connection = self._get_connection()

params = {}
if cache_key is not None:
params = {'cache_key': cache_key}

while endpoint is not None:
response = connection.request('GET', endpoint)
response = connection.request('GET', endpoint, params=params)
endpoint = response.get('next')
for obj in response['objects']:
yield obj
8 changes: 6 additions & 2 deletions syncano/models/incentives.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ class Meta:
}
}

def run(self, **payload):
def run(self, cache_key=None, **payload):
"""
Usage::

Expand All @@ -271,7 +271,11 @@ def run(self, **payload):
endpoint = self._meta.resolve_endpoint('run', properties)
connection = self._get_connection(**payload)

response = connection.request('POST', endpoint, **{'data': payload})
params = {}
if cache_key is not None:
params = {'cache_key': cache_key}

response = connection.request('POST', endpoint, **{'data': payload, 'params': params})

if isinstance(response, dict) and 'result' in response and 'stdout' in response['result']:
response.update({'instance_name': self.instance_name,
Expand Down
1 change: 1 addition & 0 deletions syncano/models/instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Instance(RenameMixin, Model):
# snippets and data fields;
scripts = fields.RelatedManagerField('Script')
script_endpoints = fields.RelatedManagerField('ScriptEndpoint')
data_endpoints = fields.RelatedManagerField('EndpointData')
templates = fields.RelatedManagerField('ResponseTemplate')

triggers = fields.RelatedManagerField('Trigger')
Expand Down
56 changes: 56 additions & 0 deletions tests/integration_test_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# -*- coding: utf-8 -*-
from syncano.models import RuntimeChoices
from tests.integration_test import InstanceMixin, IntegrationTest


class DataEndpointCacheTest(InstanceMixin, IntegrationTest):

@classmethod
def setUpClass(cls):
super(DataEndpointCacheTest, cls).setUpClass()
cls.klass = cls.instance.templates.create(
name='sample_klass',
schema=[
{'name': 'test1', 'type': 'string'},
{'name': 'test2', 'type': 'string'}
])
cls.data_object = cls.instance.templates.create(
class_name=cls.klass.name
)

cls.data_endpoint = cls.instance.data_endpoints.create(
name='test_data_endpoint',
description='test description',
class_name=cls.klass.name
)

def test_cache_request(self):
data_endpoint = list(self.data_endpoint.get(cache_key='12345'))

self.assertTrue(data_endpoint)

for data_object in data_endpoint:
self.assertTrue(data_object)


class ScriptEndpointCacheTest(InstanceMixin, IntegrationTest):

@classmethod
def setUpClass(cls):
super(ScriptEndpointCacheTest, cls).setUpClass()

cls.script = cls.instance.scripts.create(
label='test_script',
description='test script desc',
source='print(12)',
runtime_name=RuntimeChoices.PYTHON_V5_0,
)

cls.script_endpoint = cls.instance.script_endpoints.create(
name='test_script_endpoint',
script=cls.script.id
)

def test_cache_request(self):
response = self.script_endpoint.run(cache_key='123456')
self.assertEqual(response.result['stdout'], 12)