def get_items()

in src/recommendations/src/recommendations-service/experimentation/resolvers.py [0:0]


    def get_items(self, **kwargs):
        """ Returns recommended items given a product_id from curated list of products

        Arguments:
            product_id - item ID of the currently displayed product (optional)
            num_results - number of recommendations to return (optional)
        """
        product_id = kwargs.get('product_id')

        num_results = 10
        if kwargs.get('num_results'):
            num_results = int(kwargs['num_results'])

        items = []

        category = None

        if product_id:
            # Lookup product to determine if it belongs to a category
            url = f'http://{self.products_service_host}:{self.products_service_port}/products/id/{product_id}'
            log.debug('DefaultProductResolver - getting product details ' + url)
            try:
                response = requests.get(url)
                if response.ok:
                    category = response.json()['category']
            except requests.ConnectionError as e:
                log.error(f"Could not pull product information from URL {url} - error: {e}")

        if category:
            # Product belongs to a category so get list of products in same category
            url = f'http://{self.products_service_host}:{self.products_service_port}/products/category/{category}?fullyQualifyImageUrls={self.fully_qualify_image_urls}'
            log.debug('DefaultProductResolver - getting products for category ' + url)
            response = requests.get(url)
        else:
            # Product not specified or does not belong to a category so fallback to featured products
            url = f'http://{self.products_service_host}:{self.products_service_port}/products/featured?fullyQualifyImageUrls={self.fully_qualify_image_urls}'
            log.debug('DefaultProductResolver - getting featured products ' + url)
            response = requests.get(url)

        if response.ok:
            # Create response making sure not to include current product
            for product in response.json():
                if product['id'] != product_id:
                    items.append({'itemId': str(product['id'])})

                    if len(items) >= num_results:
                        break
        else:
            raise Exception(f'Error calling products service: {response.status_code}: {response.reason}')

        return items