from __future__ import print_function

import json
import sys
from ConfigParser import NoOptionError

import click
from syncano_cli import LOG

from .connection import create_connection


@click.group()
def top_execute():
    pass


@top_execute.command()
@click.option('--config', help=u'Account configuration file.')
@click.argument('instance_name', envvar='SYNCANO_INSTANCE')
@click.argument('script_endpoint_name')
@click.option('--payload', help=u'Script payload in JSON format.')
def execute(config, instance_name, script_endpoint_name, payload):
    """
    Execute script endpoint in given instance
    """
    try:
        connection = create_connection(config)
    except NoOptionError:
        LOG.error('Do a login first: syncano login.')
        sys.exit(1)
    instance = connection.Instance.please.get(instance_name)
    se = instance.script_endpoints.get(instance_name, script_endpoint_name)
    data = json.loads(payload.strip() or '{}')
    response = se.run(**data)
    print(json.dumps(response.result, indent=4, sort_keys=True))

from __future__ import print_function

import json
import sys
from ConfigParser import NoOptionError

import click
from syncano.exceptions import SyncanoDoesNotExist
from syncano_cli import LOG

from .connection import create_connection


@click.group()
def top_execute():
    pass


@top_execute.command()
@click.option('--config', help=u'Account configuration file.')
@click.argument('instance_name', envvar='SYNCANO_INSTANCE')
@click.argument('script_endpoint_name')
@click.option('--payload', help=u'Script payload in JSON format.')
def execute(config, instance_name, script_endpoint_name, payload):
    """
    Execute script endpoint in given instance
    """
    try:
        connection = create_connection(config)
        instance = connection.Instance.please.get(instance_name)
        se = instance.script_endpoints.get(instance_name, script_endpoint_name)
        data = json.loads((payload or '').strip() or '{}')
        response = se.run(**data)
        if response.status == 'success':
            print(json.dumps(response.result['stdout'], indent=4, sort_keys=True))
        else:
            LOG.error(response.result['stderr'])
    except NoOptionError:
        LOG.error('Do a login first: syncano login.')
        sys.exit(1)
    except SyncanoDoesNotExist as e:
        LOG.error(e)
        sys.exit(1)
    except ValueError as e:
        LOG.error('Invalid payload format: {error}'.format(error=e))
        sys.exit(1)

Refactor the code: add error handling of invalid or empty parameters, improve output format.
Refactor the code: add error handling of invalid or empty parameters, improve output format.

  from __future__ import print_function
  
  import json
  import sys
  from ConfigParser import NoOptionError
  
  import click
+ from syncano.exceptions import SyncanoDoesNotExist
  from syncano_cli import LOG
  
  from .connection import create_connection
  
  
  @click.group()
  def top_execute():
      pass
  
  
  @top_execute.command()
  @click.option('--config', help=u'Account configuration file.')
  @click.argument('instance_name', envvar='SYNCANO_INSTANCE')
  @click.argument('script_endpoint_name')
  @click.option('--payload', help=u'Script payload in JSON format.')
  def execute(config, instance_name, script_endpoint_name, payload):
      """
      Execute script endpoint in given instance
      """
      try:
          connection = create_connection(config)
+         instance = connection.Instance.please.get(instance_name)
+         se = instance.script_endpoints.get(instance_name, script_endpoint_name)
+         data = json.loads((payload or '').strip() or '{}')
+         response = se.run(**data)
+         if response.status == 'success':
+             print(json.dumps(response.result['stdout'], indent=4, sort_keys=True))
+         else:
+             LOG.error(response.result['stderr'])
      except NoOptionError:
          LOG.error('Do a login first: syncano login.')
          sys.exit(1)
-     instance = connection.Instance.please.get(instance_name)
-     se = instance.script_endpoints.get(instance_name, script_endpoint_name)
-     data = json.loads(payload.strip() or '{}')
-     response = se.run(**data)
-     print(json.dumps(response.result, indent=4, sort_keys=True))
+     except SyncanoDoesNotExist as e:
+         LOG.error(e)
+         sys.exit(1)
+     except ValueError as e:
+         LOG.error('Invalid payload format: {error}'.format(error=e))
+         sys.exit(1)
  
Refactor the code: add error handling of invalid or empty parameters, improve output format.
## Code Before:
from __future__ import print_function

import json
import sys
from ConfigParser import NoOptionError

import click
from syncano_cli import LOG

from .connection import create_connection


@click.group()
def top_execute():
    pass


@top_execute.command()
@click.option('--config', help=u'Account configuration file.')
@click.argument('instance_name', envvar='SYNCANO_INSTANCE')
@click.argument('script_endpoint_name')
@click.option('--payload', help=u'Script payload in JSON format.')
def execute(config, instance_name, script_endpoint_name, payload):
    """
    Execute script endpoint in given instance
    """
    try:
        connection = create_connection(config)
    except NoOptionError:
        LOG.error('Do a login first: syncano login.')
        sys.exit(1)
    instance = connection.Instance.please.get(instance_name)
    se = instance.script_endpoints.get(instance_name, script_endpoint_name)
    data = json.loads(payload.strip() or '{}')
    response = se.run(**data)
    print(json.dumps(response.result, indent=4, sort_keys=True))

## Instruction:
Refactor the code: add error handling of invalid or empty parameters, improve output format.
## Code After:
from __future__ import print_function

import json
import sys
from ConfigParser import NoOptionError

import click
from syncano.exceptions import SyncanoDoesNotExist
from syncano_cli import LOG

from .connection import create_connection


@click.group()
def top_execute():
    pass


@top_execute.command()
@click.option('--config', help=u'Account configuration file.')
@click.argument('instance_name', envvar='SYNCANO_INSTANCE')
@click.argument('script_endpoint_name')
@click.option('--payload', help=u'Script payload in JSON format.')
def execute(config, instance_name, script_endpoint_name, payload):
    """
    Execute script endpoint in given instance
    """
    try:
        connection = create_connection(config)
        instance = connection.Instance.please.get(instance_name)
        se = instance.script_endpoints.get(instance_name, script_endpoint_name)
        data = json.loads((payload or '').strip() or '{}')
        response = se.run(**data)
        if response.status == 'success':
            print(json.dumps(response.result['stdout'], indent=4, sort_keys=True))
        else:
            LOG.error(response.result['stderr'])
    except NoOptionError:
        LOG.error('Do a login first: syncano login.')
        sys.exit(1)
    except SyncanoDoesNotExist as e:
        LOG.error(e)
        sys.exit(1)
    except ValueError as e:
        LOG.error('Invalid payload format: {error}'.format(error=e))
        sys.exit(1)
