# Path: experiments/0.8/crosscompute/constants.py
# VIEW_NAMES = [
#     'text',
#     'number',
#     'markdown',
#     'table',
#     'image',
#     'map',
#     'electricity-network',
#     'file',
# ]
# DEFAULT_VIEW_NAME = 'text'
# PRINT_FORMAT_NAMES = [
#     'pdf'
# ]
# DEBUG_VARIABLE_DEFINITIONS = [{
#     'id': 'stdout',
#     'name': 'Standard Output',
#     'view': 'text',
#     'path': 'stdout.txt',
# }, {
#     'id': 'stderr',
#     'name': 'Standard Error',
#     'view': 'text',
#     'path': 'stderr.txt',
# }]
# 
# Path: experiments/0.8/crosscompute/exceptions.py
# class CrossComputeConnectionError(HTTPInternalServerError, CrossComputeError):
#     pass
# 
# class CrossComputeExecutionError(HTTPBadRequest, CrossComputeError):
#     pass
# 
# class CrossComputeImplementationError(
#         HTTPInternalServerError, CrossComputeError):
#     pass
# 
# class CrossComputeKeyboardInterrupt(KeyboardInterrupt, CrossComputeError):
#     pass
# 
# Path: experiments/0.8/crosscompute/macros.py
# def sanitize_name(name):
# def is_valid_name_character(x):
# def sanitize_json_value(value):
# def parse_number_safely(raw_value):
# def parse_number(raw_value):
# def split_path(path):
# def is_compatible_version(target_version_name, source_version_name):
# def get_plain_value(x):


import json
import requests
from sseclient import SSEClient
from ..constants import (
    BASH_CONFIGURATION_TEXT,
    CLIENT_URL,
    SERVER_URL)
from ..exceptions import (
    CrossComputeConnectionError,
    CrossComputeExecutionError,
    CrossComputeImplementationError,
    CrossComputeKeyboardInterrupt)
from ..macros import (
    get_environment_value)



def get_bash_configuration_text():
    return BASH_CONFIGURATION_TEXT.format(
        client_url=get_client_url(),
        server_url=get_server_url(),
        token=get_token('YOUR-TOKEN'))


def fetch_resource(
        resource_name, resource_id=None, method='GET', data=None,
        server_url=None, token=None):
    f = getattr(requests, method.lower())
    server_url = server_url if server_url else get_server_url()
    url = get_resource_url(server_url, resource_name, resource_id)
    token = token if token else get_token()
    kw = {} if data is None else {'json': data}
    try:
        response = f(url, headers={'Authorization': 'Bearer ' + token}, **kw)
    except requests.ConnectionError:
        raise CrossComputeConnectionError({
            'url': 'could not connect to server ' + url})
    status_code = response.status_code

# Path: experiments/0.8/crosscompute/constants.py
# VIEW_NAMES = [
#     'text',
#     'number',
#     'markdown',
#     'table',
#     'image',
#     'map',
#     'electricity-network',
#     'file',
# ]
# DEFAULT_VIEW_NAME = 'text'
# PRINT_FORMAT_NAMES = [
#     'pdf'
# ]
# DEBUG_VARIABLE_DEFINITIONS = [{
#     'id': 'stdout',
#     'name': 'Standard Output',
#     'view': 'text',
#     'path': 'stdout.txt',
# }, {
#     'id': 'stderr',
#     'name': 'Standard Error',
#     'view': 'text',
#     'path': 'stderr.txt',
# }]
# 
# Path: experiments/0.8/crosscompute/exceptions.py
# class CrossComputeConnectionError(HTTPInternalServerError, CrossComputeError):
#     pass
# 
# class CrossComputeExecutionError(HTTPBadRequest, CrossComputeError):
#     pass
# 
# class CrossComputeImplementationError(
#         HTTPInternalServerError, CrossComputeError):
#     pass
# 
# class CrossComputeKeyboardInterrupt(KeyboardInterrupt, CrossComputeError):
#     pass
# 
# Path: experiments/0.8/crosscompute/macros.py
# def sanitize_name(name):
# def is_valid_name_character(x):
# def sanitize_json_value(value):
# def parse_number_safely(raw_value):
# def parse_number(raw_value):
# def split_path(path):
# def is_compatible_version(target_version_name, source_version_name):
# def get_plain_value(x):


# Path: experiments/0.8/crosscompute/routines/connection.py
import json
import requests
from sseclient import SSEClient
from ..constants import (
    BASH_CONFIGURATION_TEXT,
    CLIENT_URL,
    SERVER_URL)
from ..exceptions import (
    CrossComputeConnectionError,
    CrossComputeExecutionError,
    CrossComputeImplementationError,
    CrossComputeKeyboardInterrupt)
from ..macros import (
    get_environment_value)




def get_bash_configuration_text():
    return BASH_CONFIGURATION_TEXT.format(
        client_url=get_client_url(),
        server_url=get_server_url(),
        token=get_token('YOUR-TOKEN'))


def fetch_resource(
        resource_name, resource_id=None, method='GET', data=None,
        server_url=None, token=None):
    f = getattr(requests, method.lower())
    server_url = server_url if server_url else get_server_url()
    url = get_resource_url(server_url, resource_name, resource_id)
    token = token if token else get_token()
    kw = {} if data is None else {'json': data}
    try:
        response = f(url, headers={'Authorization': 'Bearer ' + token}, **kw)
    except requests.ConnectionError:
        raise CrossComputeConnectionError({
            'url': 'could not connect to server ' + url})
    status_code = response.status_code

    d = {'url': url, 'token': token, 'statusCode': status_code}