Skip to content

Commit

Permalink
Merge pull request #92 from oceanprotocol/content-type
Browse files Browse the repository at this point in the history
Updating logs & exception handling
  • Loading branch information
jamiehewitt15 authored Oct 9, 2023
2 parents f885560 + 6dc893d commit 421a3df
Showing 1 changed file with 80 additions and 72 deletions.
152 changes: 80 additions & 72 deletions server/oceandbs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,82 +81,90 @@ def post(self, request):
"""
POST a storage service, handling different error codes
"""
data = request.data
print(f"Received registration request data: {data}")
try:
data = request.data
print(f"Received registration request data: {data}")

# Extract the signature and original message from the request data
signature = data.get('signature')
original_message = data.get('url')

# Ensure the signature and original message are present
if not signature or not original_message:
print("Signature or original message missing in request data.")
return Response("Invalid input data.", status=400)
# Extract the signature and original message from the request data
signature = data.get('signature')
original_message = data.get('url')
# Ensure the signature and original message are present
if not signature or not original_message:
print("Signature or original message missing in request data.")
return Response("Invalid input data.", status=400)

if not data.get('type'):
print("Type key missing or None in request data.")
return Response("Invalid input data.", status=400)
if not data.get('type'):
print("Type key missing or None in request data.")
return Response("Invalid input data.", status=400)

# Verify the signature and get the address that signed the original message
try:
print(f"Received signature in request: {signature}")
print(f"Received original_message in request: {original_message}")
message = encode_defunct(text=original_message)
print(f"Encoded message: {message}")
recovered_address = w3.eth.account.recover_message(message, signature=signature)
print(f"Recovered Ethereum address: {recovered_address}")
# Verify the signature and get the address that signed the original message
try:
print(f"Received signature in request: {signature}")
print(f"Received original_message in request: {original_message}")
message = encode_defunct(text=original_message)
print(f"Encoded message: {message}")
recovered_address = w3.eth.account.recover_message(message, signature=signature)
print(f"Recovered Ethereum address: {recovered_address}")
except Exception as e:
print("Failed to verify the signature.")
print(f"Specific error: {e}")
return Response("Invalid signature.", status=400)

# Check if the recovered_address matches the APPROVED_ADDRESS from the environment variables
approved_address = os.environ.get('APPROVED_ADDRESS')
print(f"Approved Ethereum address from env: {approved_address}")
if recovered_address.lower() != approved_address.lower():
print("Registration request received from non-approved address.")
return Response("Registration request received from non-approved address.", status=403)

print("Registration request received from approved address. Proceeding with registration.")
storage, created = Storage.objects.get_or_create(type=data['type'])

if not created:
if storage.is_active:
print("Chosen storage type is already active and registered.")
return Response('Chosen storage type is already active and registered.', status=200)
else:
storage.is_active = True
storage.save()
print("Chosen storage type reactivated.")
return Response('Chosen storage type reactivated.', status=201)

# Set optional fields
storage.description = data.get('description')
storage.url = data.get('url')

# Validate the storage object
try:
storage.full_clean()
except ValidationError as e:
print(f"Validation Error: {e}")
return Response(str(e), status=400)

# Save the storage object to the database
storage.save()

# Handle payment methods and accepted tokens
for payment_method_data in data.get('payment', []):
print("Attempting to add payment method")
payment_method = PaymentMethod(
storage=storage, chainId=payment_method_data['chainId'])
payment_method.save()
print("Payment method created")

for token in payment_method_data['acceptedTokens']:
print("Attempting to add accepted token")
token_title, token_value = list(token.items())[0]
accepted_token = AcceptedToken(
paymentMethod=payment_method, title=token_title, value=token_value)
accepted_token.save()
print("Accepted token created")

return Response('Desired storage created.', status=201)
except Exception as e:
print("Failed to verify the signature.")
print(f"Specific error: {e}")
return Response("Invalid signature.", status=400)

# Check if the recovered_address matches the APPROVED_ADDRESS from the environment variables
approved_address = os.environ.get('APPROVED_ADDRESS')
print(f"Approved Ethereum address from env: {approved_address}")
if recovered_address.lower() != approved_address.lower():
print("Registration request received from non-approved address.")
return Response("Registration request received from non-approved address.", status=403)

print("Registration request received from approved address. Proceeding with registration.")
storage, created = Storage.objects.get_or_create(type=data['type'])

if not created:
if storage.is_active:
return Response('Chosen storage type is already active and registered.', status=200)
else:
storage.is_active = True
storage.save()
return Response('Chosen storage type reactivated.', status=201)

# Set optional fields
storage.description = data.get('description')
storage.url = data.get('url')

# Validate the storage object
try:
storage.full_clean()
except ValidationError as e:
print(f"Validation Error: {e}")
return Response(str(e), status=400)

# Save the storage object to the database
storage.save()

# Handle payment methods and accepted tokens
for payment_method_data in data.get('payment', []):
payment_method = PaymentMethod(
storage=storage, chainId=payment_method_data['chainId'])
payment_method.save()
print(f"Payment method created: {payment_method}")

for token in payment_method_data['acceptedTokens']:
token_title, token_value = list(token.items())[0]
accepted_token = AcceptedToken(
paymentMethod=payment_method, title=token_title, value=token_value)
accepted_token.save()
print(f"Accepted token created: {accepted_token}")

return Response('Desired storage created.', status=201)
print(f"Unhandled exception occurred: {e}")
return Response("Internal server error.", status=500)

# Storage service listing class

Expand Down

0 comments on commit 421a3df

Please sign in to comment.