Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to rewrite resource url in cases when ckan.datapusher.callback_url_base is used in CKAN #206

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions datapusher/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import requests
try:
from urllib.parse import urlsplit
from urllib.parse import urlparse
except ImportError:
from urlparse import urlsplit
from urlparse import urlparse

import itertools
import datetime
Expand Down Expand Up @@ -42,6 +44,12 @@
if not SSL_VERIFY:
requests.packages.urllib3.disable_warnings()

if web.app.config.get('REWRITE_RESOURCES') in ['True', 'TRUE', '1', True, 1]:
REWRITE_RESOURCES = True
REWRITE_URL = web.app.config.get('REWRITE_URL') or 'http://ckan:5000/'
else:
REWRITE_RESOURCES = False

_TYPE_MAPPING = {
'String': 'text',
# 'int' may not be big enough,
Expand Down Expand Up @@ -360,6 +368,15 @@ def push_to_datastore(task_id, input, dry_run=False):
# If this is an uploaded file to CKAN, authenticate the request,
# otherwise we won't get file from private resources
headers['Authorization'] = api_key

# If CKAN is behind a firewall and ckan.datapusher.callback_url_base
# is used, we need to rewrite the URL of the resource
if REWRITE_RESOURCES:
resource_url = urlparse(url)
rewrite_url = urlparse(REWRITE_URL)
new_url = resource_url._replace(scheme=rewrite_url.scheme, netloc=rewrite_url.netloc)
url = new_url.geturl()
logger.info('Rewrote resource url to: {0}'.format(url))
try:
response = requests.get(
url,
Expand Down