Esempio n. 1
0
def cook_sous_chef(user, org, sous_chef):
    """
    Run Sous Chefs via the API.
    """
    sc = fetch_by_id_or_field(SousChef, 'slug', sous_chef)
    if not sc:
        raise NotFoundError(
            'A SousChef does not exist with ID/slug {}'
            .format(sous_chef))

    # setup kwargs for sous chef.
    kw = dict(
        org=org.to_dict(
            incl_auths=True,
            auths_as_dict=True,
            settings_as_dict=True,
            incl_domains=True,
            incl_users=True),
        apikey=user.apikey,
        passthrough=arg_bool('load')
    )

    # parse runtime options from params + body.
    ignore = ['apikey', 'org', 'localize', 'load']
    options = {
        k: v for k, v in dict(request.args.items()).items()
        if k not in ignore
    }
    options.update({
        k: v for k, v in request_data().items() if k not in ignore
    })
    kw.update(options)
    log.info('KWARGS:\n{}'.format(kw))
    # run the sous chef
    resp = sc_exec.run(sc.config, **kw)

    # stream results
    def generate():
        for item in resp:
            yield obj_to_json(item) + "\n"

    return Response(stream_with_context(generate()))
Esempio n. 2
0
def run(opts, **kw):

    from newslynx.sc import sc_exec
    from newslynx.lib import serialize
    from newslynx.cli.common import load_data
    from newslynx.client import API

    # connect to the api and fetch org
    kw['apikey'] = opts.apikey
    kw['api_url'] = opts.apiurl

    api = API(
        apikey=opts.apikey,
        org=opts.org,
        api_url=opts.apiurl,
        raise_errors=True)
    try:
        kw['org'] = api.orgs.get(opts.org)
    except:
        log.warning('Cannot connect to the API. Running in dev mode.')
        kw['org'] = {'id': opts.org}

    # parse body file / json string.
    recipe = load_data(opts.recipe)
    if recipe:
        kw.update(recipe)

    res = sc_exec.run(opts.sous_chef, **kw)
    if not res:
        return
    # stream output
    if isgenerator(res):
        for r in res:
            sys.stdout.write(serialize.obj_to_json(r) + "\n")
    # stream
    else:
        sys.stdout.write(serialize.obj_to_json(res))
Esempio n. 3
0
def run(opts, **kw):

    from newslynx.sc import sc_exec
    from newslynx.lib import serialize
    from newslynx.cli.common import load_data
    from newslynx.client import API

    # connect to the api and fetch org
    kw['apikey'] = opts.apikey
    kw['api_url'] = opts.apiurl

    api = API(apikey=opts.apikey,
              org=opts.org,
              api_url=opts.apiurl,
              raise_errors=True)
    try:
        kw['org'] = api.orgs.get(opts.org)
    except:
        log.warning('Cannot connect to the API. Running in dev mode.')
        kw['org'] = {'id': opts.org}

    # parse body file / json string.
    recipe = load_data(opts.recipe)
    if recipe:
        kw.update(recipe)

    res = sc_exec.run(opts.sous_chef, **kw)
    if not res:
        return
    # stream output
    if isgenerator(res):
        for r in res:
            sys.stdout.write(serialize.obj_to_json(r) + "\n")
    # stream
    else:
        sys.stdout.write(serialize.obj_to_json(res))