예제 #1
0
def retrieve(request, id):
	# use HTTP status codes
	response = ''
	result = {}
	success = True
	error = ''
	status_code = 200

	if request is None:
		success = False
		error = "Bad request.  Somehow the request wasn't found."
		status_code = 400

	if id is None:
		success = False
		error = "Bad request.  No id was found."
		status_code = 400
	
	try:
		result = crud_helpers.retrieve_json_package(id)
	except HelperException as e:
		success = False
		error = e.error
		status_code = e.status_code

	if success is True:
		response = make_response(result, 200)
		response.headers['content-type'] = 'application/json'
	else:
		result['error'] = error
		response = createJsonResponse(result, status_code)

	return response
예제 #2
0
def retrieve(request, id, resource_name):
    result = {}
    error = ''

    if request is None:
        return crud_helpers.createErrorResponse("Request data was not found.", 400)

    # retrieve the package
    try:
        package_str = crud_helpers.retrieve_json_package(id)
        package = json.loads(package_str)
    except Exception as e:
        return crud_helpers.createErrorResponse(e.error, e.status_code)

    # get the resource
    try:
        for item in package['resources']:
            if item['name'] == resource_name:
                resource = item
                break
    except Exception as e:
        return crud_helpers.createErrorResponse("An error occured while attempting to get the specified resource. " + str(e), 500)

	# determine the resource type
    if resource['type'] == 'url':
        # make the response and set content type
        result['url'] = resource['url']
        try:
            response = make_response(json.dumps(result), 200)
        except Exception as e:
            return crud_helpers.createErrorResponse("An error occured while attempting to get the URL of the resource. " + str(e), 500)

    	response.headers['content-type'] = "application/json"
    	return response