示例#1
0
文件: rest.py 项目: 18F/openFEC
def add_caching_headers(response):
    max_age = env.get_credential('FEC_CACHE_AGE')
    cache_all_requests = env.get_credential('CACHE_ALL_REQUESTS', False)
    status_code = response.status_code

    if max_age is not None:
        response.headers.add('Cache-Control', 'public, max-age={}'.format(max_age))

    if (cache_all_requests and status_code == 200):
        try:
            # convert the results to JSON
            json_data = utils.get_json_data(response)
            # format the URL by removing the api_key and special characters
            formatted_url = utils.format_url(request.url)
            # get s3 bucket env variables
            s3_bucket = utils.get_bucket()
            cached_url = "s3://{0}/cached-calls/{1}.json".format(s3_bucket.name, formatted_url)
            s3_key = utils.get_s3_key(cached_url)

            # upload the request_content.json file to s3 bucket
            with smart_open(s3_key, 'wb') as cached_file:
                cached_file.write(json_data)

            logger.info('The following request has been cached and uploaded successfully :%s ', cached_url)
        except:
            logger.error('Cache Upload failed')
    return response
示例#2
0
文件: rest.py 项目: nyulacska/openFEC
def add_caching_headers(response):
    max_age = env.get_credential('FEC_CACHE_AGE')
    cache_all_requests = env.get_credential('CACHE_ALL_REQUESTS', False)
    status_code = response.status_code

    if max_age is not None:
        response.headers.add('Cache-Control',
                             'public, max-age={}'.format(max_age))

    if (cache_all_requests and status_code == 200):
        try:
            # convert the results to JSON
            json_data = utils.get_json_data(response)
            # format the URL by removing the api_key and special characters
            formatted_url = utils.format_url(request.url)
            # get s3 bucket env variables
            s3_bucket = utils.get_bucket()
            cached_url = "s3://{0}/cached-calls/{1}.json".format(
                s3_bucket.name, formatted_url)
            s3_key = utils.get_s3_key(cached_url)

            # upload the request_content.json file to s3 bucket
            with smart_open(s3_key, 'wb') as cached_file:
                cached_file.write(json_data)

            logger.info(
                'The following request has been cached and uploaded successfully :%s ',
                cached_url)
        except:
            logger.error('Cache Upload failed')
    return response
示例#3
0
def make_bundle(resource):
    s3_key = task_utils.get_s3_key(resource['name'])
    with smart_open(s3_key, "wb") as fp:
        query = query_with_labels(resource['query'], resource['schema'])
        copy_to(query,
                fp,
                db.session.connection().engine,
                format='csv',
                header=True)