Skip to content

Commit

Permalink
Merge from stable to master: v0.9.2
Browse files Browse the repository at this point in the history
Merge pull request #108 from CSCfi/stable
  • Loading branch information
junsk1 authored Aug 30, 2018
2 parents 89412b3 + d96bd38 commit 30e1ba8
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions etsin_finder/download_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
# :license: MIT

from flask import Response, stream_with_context
from requests import get
from flask_restful import abort
from requests import get, exceptions

from etsin_finder.finder import app

Expand All @@ -18,20 +19,34 @@ class DownloadAPIService:
def __init__(self, download_api_config):
if download_api_config:
self.DOWNLOAD_API_BASE_URL = 'https://{0}/api/v1/dataset'.format(download_api_config['HOST']) + '/{0}'
self.TIMEOUT = 1800
self.TIMEOUT = 5 # If no bytes have been received on the underlying socket for timeout seconds

def download(self, cr_id, file_ids, dir_ids):
url = self._create_url(cr_id, file_ids, dir_ids)
req = get(url, stream=True, timeout=self.TIMEOUT)
res = Response(response=stream_with_context(req.iter_content(chunk_size=1024)), status=req.status_code)

if 'Content-Type' in req.headers:
res.headers['Content-Type'] = req.headers['Content-Type']
if 'Content-Disposition' in req.headers:
res.headers['Content-Disposition'] = req.headers['Content-Disposition']
if 'Content-Length' in req.headers:
res.headers['Content-Length'] = req.headers['Content-Length']
return res
try:
dl_api_response = get(url, stream=True, timeout=self.TIMEOUT)
dl_api_response.raise_for_status()
except exceptions.Timeout:
log.error("Request to Download API timed out")
return abort(400, message="Unable to get files. Please try again later.")
except exceptions.ConnectionError:
log.error("Unable to connect to Download API")
return abort(400, message="Unable to get files. Please try again later.")
except exceptions.HTTPError:
log.debug("Download API returned an unsuccessful status code")
return '', 404
else:
response = Response(response=stream_with_context(dl_api_response.iter_content(chunk_size=1024)),
status=dl_api_response.status_code)

if 'Content-Type' in dl_api_response.headers:
response.headers['Content-Type'] = dl_api_response.headers['Content-Type']
if 'Content-Disposition' in dl_api_response.headers:
response.headers['Content-Disposition'] = dl_api_response.headers['Content-Disposition']
if 'Content-Length' in dl_api_response.headers:
response.headers['Content-Length'] = dl_api_response.headers['Content-Length']

return response

def _create_url(self, cr_id, file_ids, dir_ids):
url = self.DOWNLOAD_API_BASE_URL.format(cr_id)
Expand Down

0 comments on commit 30e1ba8

Please sign in to comment.