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

Sending data a json data type #98

Merged
merged 2 commits into from
Oct 11, 2023
Merged
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
37 changes: 27 additions & 10 deletions server/oceandbs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def upload_files_to_ipfs(request_files, quote):
print(f"Error processing the uploaded files: {e}")
raise ValueError(f"Error processing the uploaded files: {e}")

print(f"files_reference: {files_reference}")
return files_reference


Expand Down Expand Up @@ -131,16 +132,19 @@ def create_allowance(quote, user_private_key, abi):
print(f"Transaction completed successfully")
return Response("Transaction completed successfully.", status=200)


# This function is used to upload the files to the target microservice
def upload_files_to_microservice(quote, params, files_reference):

# Initial data validations
if not quote or not hasattr(quote, 'storage') or not hasattr(quote.storage, 'url') or not hasattr(quote, 'quoteId'):
raise ValueError("Invalid quote object provided.")
error_message = "Invalid quote object provided."
print(error_message)
raise ValueError(error_message)

if not params or 'nonce' not in params or 'signature' not in params:
raise ValueError("Invalid params provided.")
error_message = "Invalid params provided."
print(error_message)
raise ValueError(error_message)

data = {
"quoteId": quote.quoteId,
Expand All @@ -151,19 +155,32 @@ def upload_files_to_microservice(quote, params, files_reference):

url = quote.storage.url + 'upload/?quoteId=' + str(quote.quoteId) + '&nonce=' + data['nonce'] + '&signature=' + data['signature']

headers = {'Content-Type': 'application/json'}

print(f"Preparing to send data to microservice: {data}")
print(f"Sending request to microservice url: {url}")

try:
print(f"Sending request to microservice url: {url}")
response = requests.post(url, data)
response.raise_for_status() # This will raise an HTTPError if the HTTP request returned an unsuccessful status code
response = requests.post(url, data=json.dumps(data), headers=headers)
response.raise_for_status()
except RequestException as e:
# Extract more detailed message from the response content, if available
detailed_message = e.response.text if hasattr(e, 'response') and hasattr(e.response, 'text') else "No detailed message provided."
raise RuntimeError(f"Error occurred while making the request: {str(e)}. Detailed message: {detailed_message}")
except Exception as e: # Catches any unforeseen exceptions
raise RuntimeError(f"Unexpected error occurred: {str(e)}")
error_message = f"Error occurred while making the request: {str(e)}. Detailed message: {detailed_message}"
print(error_message)
raise RuntimeError(error_message)
except Exception as e:
error_message = f"Unexpected error occurred: {str(e)}"
print(error_message)
raise RuntimeError(error_message)

print(f"Received response from microservice with status code: {response.status_code}")
if response.status_code != 200:
print(f"Response content: {response.text}")

return response



# This function is used to generate the signature for every request
def generate_signature(quoteId, nonce, pkey):
message = "0x" + hashlib.sha256((str(quoteId) + str(nonce)).encode('utf-8')).hexdigest()
Expand Down
Loading