Skip to content
This repository was archived by the owner on Oct 30, 2018. It is now read-only.
4 changes: 0 additions & 4 deletions syncano_cli/custom_sockets/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ class ConfigNameMissingException(CLIBaseException):
default_message = u'Variable name should be provided in config.'


class OneEndpointPerMethodException(CLIBaseException):
default_message = u'Only one endpoint per method allowed.'


class BadYAMLDefinitionInEndpointsException(CLIBaseException):
default_message = u'Specify one general endpoint or specify endpoints for each method.'

Expand Down
63 changes: 53 additions & 10 deletions syncano_cli/custom_sockets/formatters.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# -*- coding: utf-8 -*-
import json
import os
from collections import defaultdict

import six
import yaml
from syncano_cli.custom_sockets.exceptions import BadYAMLDefinitionInEndpointsException, OneEndpointPerMethodException
from syncano_cli.custom_sockets.exceptions import BadYAMLDefinitionInEndpointsException
from syncano_cli.sync.scripts import ALLOWED_RUNTIMES


Expand Down Expand Up @@ -68,9 +70,47 @@ def _json_process_endpoints(cls, endpoints):

for name, endpoint_data in six.iteritems(endpoints):
api_endpoints[name] = {'calls': cls._get_calls(endpoint_data)}
api_endpoints[name].update({'metadata': cls._get_metadata(endpoint_data)})

return api_endpoints

@classmethod
def _process_response_example(cls, metadata_key, inner_data):
if metadata_key not in cls.ENDPOINT_TYPES:
if metadata_key == 'response':
if 'example' in inner_data:
if isinstance(inner_data['example'], dict):
inner_data['example'] = inner_data['example']
elif isinstance(inner_data['example'], six.string_types):
try:
inner_data['example'] = json.loads(inner_data['example'])
except (TypeError, ValueError):
inner_data['example'] = inner_data['example']

@classmethod
def _get_metadata(cls, endpoint_data):
metadata = defaultdict(dict)
for data_key, data in six.iteritems(endpoint_data):
if data_key in cls.HTTP_METHODS:
for metadata_key, inner_data in six.iteritems(data):
if metadata_key in cls.ENDPOINT_TYPES:
continue
if metadata_key == 'parameters':
metadata[metadata_key][data_key] = inner_data
else:
cls._process_response_example(metadata_key, inner_data)
metadata[metadata_key] = inner_data

elif data_key not in cls.ENDPOINT_TYPES:
if data_key == 'parameters':
metadata[data_key]['*'] = data
else:
cls._process_response_example(data_key, data)
metadata[data_key] = data
metadata[data_key] = data

return metadata

@classmethod
def _get_calls(cls, endpoint_data):
calls = []
Expand All @@ -89,16 +129,14 @@ def _get_calls(cls, endpoint_data):
})

elif type_or_method in cls.HTTP_METHODS:
if len(data) != 1:
raise OneEndpointPerMethodException()

for call_type, name in six.iteritems(data):
calls.append({
'type': call_type,
'methods': [type_or_method],
'name': name
})

if call_type in cls.ENDPOINT_TYPES:
calls.append({
'type': call_type,
'methods': [type_or_method],
'name': name
})
return calls

@classmethod
Expand All @@ -112,7 +150,7 @@ def _json_process_dependencies(cls, dependencies, directory):
'type': cls.DEPENDENCY_TYPES[dependencies_type],
'runtime_name': data['runtime_name'],
'name': dependency_name,
'source': cls._get_source(data['file'], directory)
'source': cls._get_source(data['file'], directory),
})
return api_dependencies

Expand All @@ -126,8 +164,13 @@ def _yml_process_endpoints(cls, endpoints):
yml_endpoints = {}
for endpoint_name, endpoint_data in six.iteritems(endpoints):
yml_endpoints[endpoint_name] = cls._yml_process_calls(endpoint_data['calls'])
yml_endpoints.update(cls._yml_process_metadata(endpoint_data))
return yml_endpoints

@classmethod
def _yml_process_metadata(cls, endpoint_data):
return endpoint_data.get('metadata', {}) # some old custom sockets do not have this field;

@classmethod
def _yml_process_calls(cls, data_calls):
calls = {}
Expand Down
4 changes: 3 additions & 1 deletion syncano_cli/execute/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import json

import six


def print_response(response):
if hasattr(response, 'result'):
Expand All @@ -16,7 +18,7 @@ def print_response(response):

def _print_result(result):
try:
if type(result) in [str, unicode]:
if isinstance(result, six.string_types):
output = json.loads(result)
else:
output = result
Expand Down
2 changes: 1 addition & 1 deletion tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def setUpClass(cls):
super(InstanceMixin, cls).setUpClass()

cls.instance = cls.connection.Instance.please.create(
name='test_cli_i%s' % cls.generate_hash()[:10],
name='test-cli-i%s' % cls.generate_hash()[:10],
description='IntegrationTest %s' % datetime.now(),
)

Expand Down