Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
[LIB-783] - move logic checking available endpoint request methods to…
… Meta class, apply changes to ScriptEndpoint model
  • Loading branch information
ilu2112 committed Jun 28, 2016
commit 478e325f172f4673b55cd9f23062a755c9df4ace
8 changes: 8 additions & 0 deletions syncano/models/archetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,11 @@ def get_endpoint_data(self):
if field.has_endpoint_data:
properties[field.name] = getattr(self, field.name)
return properties

def make_endpoint_request(self, connection, endpoint_name, http_method, **kwargs):
if self._meta.is_http_method_available_for_endpoint(http_method, endpoint_name):
properties = self.get_endpoint_data()
endpoint = self._meta.resolve_endpoint(endpoint_name, properties)
return connection.request(http_method, endpoint, **kwargs)
else:
raise SyncanoValidationError('HTTP method {0} not allowed for endpoint name "{1}".'.format(http_method, endpoint_name))
11 changes: 5 additions & 6 deletions syncano/models/incentives.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,7 @@ def run(self, cache_key=None, **payload):
if self.is_new():
raise SyncanoValidationError('Method allowed only on existing model.')

properties = self.get_endpoint_data()
endpoint = self._meta.resolve_endpoint('run', properties)
endpoint_name = 'run'
connection = self._get_connection(**payload)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In:

    def resolve_endpoint(self, name, properties):
        endpoint = self.get_endpoint(name)

        for name in endpoint['properties']:
            if name not in properties:
                raise SyncanoValueError('Request property "{0}" is required.'.format(name))

        return endpoint['path'].format(**properties)

is get_endpoint which is:

    def get_endpoint(self, name):
        if name not in self.endpoints:
            raise SyncanoValueError('Invalid path name: "{0}".'.format(name))
        return self.endpoints[name]

Maybe we should add check in resolve_endpoint for that? Would be much cleaner. What do you think?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@opalczynski Fine :)


params = {}
Expand All @@ -279,7 +278,7 @@ def run(self, cache_key=None, **payload):
if params:
kwargs.update({'params': params})

response = connection.request('POST', endpoint, **kwargs)
response = self.make_endpoint_request(connection, endpoint_name, 'POST', **kwargs)

if isinstance(response, dict) and 'result' in response and 'stdout' in response['result']:
response.update({'instance_name': self.instance_name,
Expand All @@ -295,11 +294,11 @@ def reset_link(self):
>>> se = ScriptEndpoint.please.get('instance-name', 'script-name')
>>> se.reset_link()
"""
properties = self.get_endpoint_data()
endpoint = self._meta.resolve_endpoint('reset', properties)
endpoint_name = 'reset'
connection = self._get_connection()

response = connection.request('POST', endpoint)
response = self.make_endpoint_request(connection, endpoint_name, 'POST')

self.public_link = response['public_link']


Expand Down
4 changes: 4 additions & 0 deletions syncano/models/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ def resolve_endpoint(self, name, properties):

return endpoint['path'].format(**properties)

def is_http_method_available_for_endpoint(self, http_method_name, endpoint_name):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_http_method_available_for_endpoint -> is_http_method_available is sufficient

available_methods = self.get_endpoint_methods(endpoint_name)
return http_method_name.lower() in available_methods

def get_endpoint_query_params(self, name, params):
properties = self.get_endpoint_properties(name)
return {k: v for k, v in six.iteritems(params) if k not in properties}
Expand Down