def graph():
    """
    Make a request to the "resource server" (service app) API to
    do the graph processing.
    """
    if request.method == 'GET':
        return render_template('graph.jinja2', datasets=datasets)

    selected_ids = request.form.getlist('dataset')
    selected_year = request.form.get('year')

    if not (selected_ids and selected_year):
        flash("Please select at least one dataset and a year to graph.")
        return redirect(url_for('graph'))

    tokens = get_portal_tokens()
    service_token = tokens.get('GlobusWorld Resource Server')['token']

    service_url = '{}/{}'.format(app.config['SERVICE_URL_BASE'], 'api/doit')
    req_headers = dict(Authorization='Bearer {}'.format(service_token))

    req_data = dict(datasets=selected_ids,
                    year=selected_year,
                    user_identity_id=session.get('primary_identity'),
                    user_identity_name=session.get('primary_username'))

    resp = requests.post(service_url,
                         headers=req_headers,
                         data=req_data,
                         verify=False)

    resp.raise_for_status()

    resp_data = resp.json()
    dest_ep = resp_data.get('dest_ep')
    dest_path = resp_data.get('dest_path')
    dest_name = resp_data.get('dest_name')
    graph_count = resp_data.get('graph_count')

    flash("%d-file SVG upload to %s on %s completed!" %
          (graph_count, dest_path, dest_name))

    return redirect(
        url_for('browse',
                endpoint_id=dest_ep,
                endpoint_path=dest_path.lstrip('/')))
def graph_cleanup():
    """Make a request to the service app API to do the graph processing."""
    tokens = get_portal_tokens()
    service_token = tokens.get('GlobusWorld Resource Server')['token']

    service_url = '{}/{}'.format(app.config['SERVICE_URL_BASE'], 'api/cleanup')
    req_headers = dict(Authorization='Bearer {}'.format(service_token))

    resp = requests.post(
        service_url,
        headers=req_headers,
        data=dict(user_identity_name=session['primary_username']),
        verify=False)

    resp.raise_for_status()

    task_id = resp.json()['task_id']

    msg = '{} ({}).'.format('Your existing processed graphs have been removed',
                            task_id)
    flash(msg)
    return redirect(url_for('graph'))