"""Interface for the ows-product-physical microservice.""" from oto import response from owsrequest import request as requests from owsrequest.constants import headers as req_headers import sentry_sdk from product.constants import error from product.constants import service_name def copy_product(product_id, fields, orchard_user_id): """Copy a product. Args: product_id (int): The product_id (release_id) of the product to copy. fields (dict): Fields that override original product fields when copying the product. orchard_user_id (str|None): orchard user id from grass. """ try: headers = None if orchard_user_id: headers = {req_headers.ORCHARD_USER_ID: orchard_user_id} copy_product_response = requests.post( service_name.OWS_PRODUCT_PHYSICAL, "/product/{product_id}/copy".format(product_id=product_id), json=fields, headers=headers, ) except Exception as exception: sentry_sdk.capture_exception(exception) return response.create_fatal_response( message="Could not connect to ows-product-physical." ) return response.Response( status=copy_product_response.status_code, message=copy_product_response.json() ) def copy_product_tracks(product_id, new_product_id): """Copy a product's tracks. Args: product_id (int): The product_id of the product to copy from. new_product_id (int): The product_id of the product to copy to. """ try: copy_product_tracks_response = requests.post( service_name.OWS_PRODUCT_PHYSICAL, "/product/{}/tracks/copy/{}".format(product_id, new_product_id), ) except Exception as exception: sentry_sdk.capture_exception(exception) return response.create_fatal_response( message="Could not connect to ows-product-physical." ) return response.Response(status=copy_product_tracks_response.status_code) def get_product_by_product_id(product_id): """Get a physical product.""" try: physical_response = requests.get( service_name.OWS_PRODUCT_PHYSICAL, "/product/{}".format(product_id) ) except Exception as exception: sentry_sdk.capture_exception(exception) return response.create_fatal_response( message="Could not connect to ows-product-physical." ) return response.Response( status=physical_response.status_code, message=physical_response.json() ) def delete_physical_product(product_id): """Delete physical product. Args: product_id (int): physical product id to delete. Returns: response.Response: status of deletion """ try: delete_status = requests.delete( service_name.OWS_PRODUCT_PHYSICAL, "/product/{product_id}".format(product_id=product_id), ) return response.Response(status=delete_status.status_code) except Exception as exception: sentry_sdk.capture_exception(exception) return response.create_error_response( code=error.INTERNAL_ERROR, message="connection error", status=500 ) def get_tracks(product_id): """Get tracks. Args: product_id (int): physical product id to get tracks for. Returns: response.Response: tracks """ try: tracks_response = requests.get( service_name.OWS_PRODUCT_PHYSICAL, "/product/{product_id}/tracks".format(product_id=product_id), ) return response.Response(message=tracks_response.json()) except Exception as exception: sentry_sdk.capture_exception(exception) return response.create_error_response( code=error.INTERNAL_ERROR, message="connection error", status=500 )