Esempio n. 1
0
def forward_request(request):
    path = request['path'].replace("/services/json/v2", "")
    method = request['method']

    messages = []
    content = {"messages": messages}

    response, content = forward.make_request(
        request["base_path"] + path,
        headers=request['headers'],
        get=request['get'],
        post=request['post'],
        payload=request['payload'],
        method=request['method'],
        session_key=request['session_key'],
        basic_auth=get_authorization(request)
        if is_basicauth(request) else None)
    if response.status == 401:
        messages.append({"type": "ERROR", "text": "Login Failed"})
    elif response.status == 402:
        messages.append({"type": "ERROR", "text": "License Restriction"})
    elif response.status == 403:
        messages.append({
            "type": "ERROR",
            "text": "Authorization Failed: %s" % uri
        })
    elif response.status == 404:
        # Some 404 reponses, such as those for expired jobs which were originally
        # run by the scheduler return extra data about the original resource.
        # In this case we add that additional info into the exception object
        # as the resourceInfo parameter so others might use it.
        try:
            body = et.fromstring(content)
            resource_info = body.find('dict')
            if resource_info is not None:
                messages.append({
                    "type":
                    "ERROR",
                    "text":
                    "Resource not found: %s" %
                    xml2json.node_to_primitive(resource_info)
                })
            else:
                extracted_messages = xml2json.extract_messages(body)
                xml2json.combine_messages(messages, extracted_messages)
        except Exception, e:
            logger.info(e)
            pass
def forward_request(request):
    path = request['path'].replace("/services/json/v2", "")                
    method = request['method']
    
    messages = []
    content = {"messages": messages}
    
    response, content = forward.make_request(
        request["base_path"] + path,
        headers=request['headers'],
        get=request['get'],
        post=request['post'],
        payload=request['payload'],
        method=request['method'],
        session_key=request['session_key'],
        basic_auth=get_authorization(request) if is_basicauth(request) else None
    )
    if response.status == 401:
        messages.append({"type": "ERROR", "text": "Login Failed"})
    elif response.status == 402:
        messages.append({"type": "ERROR", "text": "License Restriction"})
    elif response.status == 403:
        messages.append({"type": "ERROR", "text": "Authorization Failed: %s" % uri})  
    elif response.status == 404:
        # Some 404 reponses, such as those for expired jobs which were originally
        # run by the scheduler return extra data about the original resource.
        # In this case we add that additional info into the exception object
        # as the resourceInfo parameter so others might use it.
        try:
            body = et.fromstring(content)
            resource_info = body.find('dict')
            if resource_info is not None:
                messages.append({"type": "ERROR", "text": "Resource not found: %s" % xml2json.node_to_primitive(resource_info)})
            else:
                extracted_messages = xml2json.extract_messages(body)
                xml2json.combine_messages(messages, extracted_messages)
        except Exception, e:
            logger.info(e);
            pass
Esempio n. 3
0
                    "text":
                    "Resource not found: %s" %
                    xml2json.node_to_primitive(resource_info)
                })
            else:
                extracted_messages = xml2json.extract_messages(body)
                xml2json.combine_messages(messages, extracted_messages)
        except Exception, e:
            logger.info(e)
            pass
    elif response.status < 200 or response.status > 299:
        # service may return messages in the body; try to parse them
        try:
            body = et.fromstring(content)
            extracted_messages = xml2json.extract_messages(body)
            xml2json.combine_messages(messages, extracted_messages)
        except:
            pass

    return response.status, content


def status_ok(status):
    return status >= 200 and status <= 299


def create_job(request, *args, **kwargs):
    status, content = forward_request(request)
    if status_ok(status):
        if request["post"].get("exec_mode", "").lower() == "oneshot":
            return status, xml2json.from_job_results(
            body = et.fromstring(content)
            resource_info = body.find('dict')
            if resource_info is not None:
                messages.append({"type": "ERROR", "text": "Resource not found: %s" % xml2json.node_to_primitive(resource_info)})
            else:
                extracted_messages = xml2json.extract_messages(body)
                xml2json.combine_messages(messages, extracted_messages)
        except Exception, e:
            logger.info(e);
            pass
    elif response.status < 200 or response.status > 299:
        # service may return messages in the body; try to parse them
        try:
            body = et.fromstring(content)
            extracted_messages = xml2json.extract_messages(body)
            xml2json.combine_messages(messages, extracted_messages)
        except:
            pass
    
    return response.status, content
    
def status_ok(status):
    return status >= 200 and status <= 299
    
def create_job(request, *args, **kwargs):    
    status, content = forward_request(request)
    if status_ok(status):
        if request["post"].get("exec_mode", "").lower() == "oneshot":
            return status, xml2json.from_job_results(content, format=xml2json.ResultFormat.ROW)
        else:
            return status, xml2json.from_job_create(content)