4
4
"""
5
5
import os
6
6
import logging
7
+ import uuid
7
8
from flask import Flask , request , Response
8
9
import urllib3
9
10
import requests
@@ -126,7 +127,9 @@ def fromproarc(path):
126
127
file_id = request .args .get ('file_id' )
127
128
filename = request .args .get ('filename' )
128
129
129
- entity = {
130
+ LOG .info (f'serving request for file_rno { file_id } and name { filename } ' )
131
+
132
+ soap_entity = {
130
133
"_soapheaders" : {},
131
134
"files" : {
132
135
"ListItems" : {
@@ -140,19 +143,25 @@ def fromproarc(path):
140
143
"id" : os .environ .get ('proarc_user' )
141
144
}
142
145
143
- # Continuing on the soap call
144
146
if os .environ .get ('transit_decode' , 'false' ).lower () == "true" :
145
- LOG .info ("transit_decode is set to True." )
146
- entity = typetransformer .transit_decode (entity )
147
+ LOG .debug ("transit_decode is set to True." )
148
+ soap_entity = typetransformer .transit_decode (soap_entity )
147
149
148
- LOG .info (f"Finished creating request: { str (entity )} " )
150
+ LOG .debug (f"Finished creating request: { str (soap_entity )} " )
151
+
152
+ with SOAP_CLIENT .settings (raw_response = True ):
153
+ proarc_response = do_soap (soap_entity , SOAP_CLIENT , path )
154
+
155
+ proarc_response .raise_for_status ()
156
+
157
+ LOG .debug (f"SOAPResponse : \n { str (proarc_response )} \n ----End-Response----" )
149
158
150
- response = do_soap (entity , SOAP_CLIENT , path )
151
- LOG .info (f"SOAPResponse : \n { str (response )} \n ----End-Response----" )
152
159
try :
153
160
if FILE_DOWNLOADER_URL :
161
+ LOG .debug (f"trying to download file { filename } from { FILE_DOWNLOADER_URL } " )
154
162
local_file_name = read_file_from_url (filename )
155
163
file_stream = read_local_file (local_file_name )
164
+ os .remove (local_file_name )
156
165
else :
157
166
file_stream = read_file (filename )
158
167
except IOError as exc :
@@ -225,8 +234,11 @@ def read_local_file(filename):
225
234
226
235
def read_file_from_url (file_name ):
227
236
# NOTE the stream=True parameter below
228
- with requests .get (f'{ FILE_DOWNLOADER_URL } /get/{ PROARC_SHARE_NAME } /{ PROARC_SHARE_PATH } ' , stream = True ) as r :
237
+ file_url = f'{ FILE_DOWNLOADER_URL } /get/{ PROARC_SHARE_NAME } /{ PROARC_SHARE_PATH } /{ file_name } '
238
+ with requests .get (file_url , stream = True ) as r :
229
239
r .raise_for_status ()
240
+ # if we have to requests fetching the same file we need to be able to not mix them
241
+ file_name += f"__{ str (uuid .uuid4 ())} "
230
242
with open (file_name , 'wb' ) as f :
231
243
for chunk in r .iter_content (chunk_size = 8192 ):
232
244
if chunk : # filter out keep-alive new chunks
0 commit comments