示例#1
0
def compaer_other(source):
    #Get the Accept Header
    acceptHeader = str(request.accept_mimetypes)
    
    #Default Response is none
    response = None;

    #Get the variable value and unit and range
    try:
        value = request.args.get('v')
        print(value)
        unit = request.args.get('u')
        print(unit)
        rng = request.args.get('r')
        print(rng)
    except IOError:
        return badRequest("Sorry, one of the parameters was not submitted correctly!");

    print("Trying to access resource: " + str(source))    

    if not (unit is None) and not(value is None) and not(rng is None):
        #Send Request to application
        handler = RequestHandler();
    
    #JSON-LD
        if "application/ld+json" in acceptHeader:

            print('Server: Received a JSON-LD request. For DataSource: ' + str(source))
            #Try to get a JSON-LD response
            try:
                response = handler.getResource(str(source), value, unit, rng)
            except ValueError:
                print('Server: A ValueError has occurred.')
                return badRequest('Please specify the parameters correctly!');
            except RuntimeError:
                print('Server: An RuntimeError has occurred.')
                return abort(500);    

            #Return answer        
            if not (response is None):
                #Valid response
                return response;
            else:
                #No result found
                return jsonify(result = 'no result was found');    
        else:
            return abort(406);        
    else:        
        return badRequest("Sorry, one of the parameters was not submitted correctly!");
示例#2
0
def compare():
    #Get the Accept Header
    acceptHeader = str(request.accept_mimetypes)

    #Default Response is None
    response = None

    #Get the variable value and unit
    try:
        value = request.args.get('v')
        unit = request.args.get('u')
    except IOError:
        return badRequest("Sorry, one of the parameters was not submitted correctly!");

            

    if not (unit is None) and not(value is None):
        #Send Request to application
        handler = RequestHandler();

        #Check the Accept Header for format

        #JSON-LD
        #Return a JSON-LD response
        if "application/ld+json" in acceptHeader:
            print('Server: Received a JSON-LD request.')
            #Try to get a JSON-LD response
            try:
                response = handler.getResponse(value, unit, 'json-ld')
            except ValueError as e:
                print('Server: A ValueError has occurred. ' + str(e))
                return badRequest("Please specify the parameters correctly!");
            except RuntimeError as e:
                print('Server: A RuntimeError has occurred. ' + str(e))
                return abort(500);    

            #Return answer        
            if not (response is None):
                #Valid response
                #Return it as JSON-LD
                #return response;
                return Response(response, content_type='application/ld+json; charset=utf-8')
            else:
                #No result found
                return jsonify(result = 'no result was found');

        #For any other Request a HTML Document is returned          
        else:    
            print('Server: Received a JSON request.')
            try:
                response = handler.getResponse(value, unit, 'json')
                print(response);

            #Handling Value Errors: Input
            except ValueError as e:
                print('Server: A ValueError has occured. ' + str(e))
                return badRequest("Please specify the parameters correctly!");

            #Handling Runtime Errors: Our Server failed somewhere 
            except RuntimeError as e:
                print('Server: A RuntimeError has occured. ' + str(e))
                return abort(500);     
                                      
            if not (response is None):
                #Render template and inject JSON
                return render_template('index_temp.html', data=response)
            else:
                response = " { 'result': 'no result found!', 'in_value' : '" + str (value) + "',  'in_unit' : '" + str(unit)+ "' }";
                return render_template('index_temp.html', data=response)

    else:
        return badRequest("The parameters provided are not valid!");