"""Interface to the ows-assets microservice.""" from oto import response from owsrequest import request MAX_REQUEST_SIZE = 200 def get_image_locations(product_ids): """Get image locations for given product IDs. Args: product_ids (list[int]): List of product IDs Return response.Response: Response object containing dictionary of image locations where keys are product IDs and values are image URLs """ merged_body = {} for chunked_ids in _chunks(product_ids, MAX_REQUEST_SIZE): resource = '/image/product/cover/location?ids={}'.format( ','.join([str(pid) for pid in chunked_ids if pid is not None]) ) ows_assets_response = request.get('ows-assets', resource) response_body = ows_assets_response.json() if ows_assets_response.status_code != 200: return response.create_error_response( status=ows_assets_response.status_code, code=response_body.get('code'), message=response_body.get('message')) merged_body.update(response_body) return response.Response(message=merged_body) def _chunks(array, chunk_size): """Split given array into even sized chunks. Args: array (list): Array to split chunk_size (int): Number of items in each chunk Returns: Array of arrays """ chunk_size = max(1, chunk_size) return (array[i:i+chunk_size] for i in range(0, len(array), chunk_size))