from django.conf import settings
from urllib.parse import urljoin, urlencode


class ComputerDataImporter(object):

    def __init__(self):
        pass

    def build_api_url(self, **kwargs):
        api_url = urljoin(settings.LOMADEE_API_URL, settings.LOMADEE_APP_TOKEN)

        # Specific path to 'Computer' category
        url = urljoin(api_url, 'offer/_category/6424')
        kwargs['sourceId'] = settings.LOMADEE_SOURCE_ID

        return '{}?{}'.format(url, urlencode(kwargs))

from django.conf import settings
from urllib.parse import urljoin, urlencode
import requests


class ComputerDataImporter(object):

    def __init__(self):
        pass

    def build_api_url(self, **kwargs):
        api_url = urljoin(settings.LOMADEE_API_URL, settings.LOMADEE_APP_TOKEN)

        # Specific path to 'Computer' category
        url = urljoin('{}/'.format(api_url), 'offer/_category/6424')
        kwargs['sourceId'] = settings.LOMADEE_SOURCE_ID
        kwargs['size'] = 100

        return '{}?{}'.format(url, urlencode(kwargs))

    def get_data(self, url=None):
        if not url:
            url = self.build_api_url()

        data = requests.get(url).json()
        if data['requestInfo']['status'] != 'OK':
            return False

        final_data = []
        final_data.extend(data['offers'])
        pagination = data['pagination']

        # Get only 3 pages. To get all pages use:
        # if pagination['page'] < pagination['totalPage']
        if pagination['page'] < 3:
            next_page_data = self.get_data(
                self.build_api_url(page=pagination['page'] + 1)
            )
            final_data.extend(next_page_data)
        return final_data

Add get_data method to data importer
Add get_data method to data importer

Signed-off-by: Matheus Fernandes <9de26933bf4170c6685be29c190c23bc8c34d55f@gmail.com>

  from django.conf import settings
  from urllib.parse import urljoin, urlencode
+ import requests
  
  
  class ComputerDataImporter(object):
  
      def __init__(self):
          pass
  
      def build_api_url(self, **kwargs):
          api_url = urljoin(settings.LOMADEE_API_URL, settings.LOMADEE_APP_TOKEN)
  
          # Specific path to 'Computer' category
-         url = urljoin(api_url, 'offer/_category/6424')
+         url = urljoin('{}/'.format(api_url), 'offer/_category/6424')
          kwargs['sourceId'] = settings.LOMADEE_SOURCE_ID
+         kwargs['size'] = 100
  
          return '{}?{}'.format(url, urlencode(kwargs))
  
+     def get_data(self, url=None):
+         if not url:
+             url = self.build_api_url()
+ 
+         data = requests.get(url).json()
+         if data['requestInfo']['status'] != 'OK':
+             return False
+ 
+         final_data = []
+         final_data.extend(data['offers'])
+         pagination = data['pagination']
+ 
+         # Get only 3 pages. To get all pages use:
+         # if pagination['page'] < pagination['totalPage']
+         if pagination['page'] < 3:
+             next_page_data = self.get_data(
+                 self.build_api_url(page=pagination['page'] + 1)
+             )
+             final_data.extend(next_page_data)
+         return final_data
+ 
Add get_data method to data importer
## Code Before:
from django.conf import settings
from urllib.parse import urljoin, urlencode


class ComputerDataImporter(object):

    def __init__(self):
        pass

    def build_api_url(self, **kwargs):
        api_url = urljoin(settings.LOMADEE_API_URL, settings.LOMADEE_APP_TOKEN)

        # Specific path to 'Computer' category
        url = urljoin(api_url, 'offer/_category/6424')
        kwargs['sourceId'] = settings.LOMADEE_SOURCE_ID

        return '{}?{}'.format(url, urlencode(kwargs))

## Instruction:
Add get_data method to data importer
## Code After:
from django.conf import settings
from urllib.parse import urljoin, urlencode
import requests


class ComputerDataImporter(object):

    def __init__(self):
        pass

    def build_api_url(self, **kwargs):
        api_url = urljoin(settings.LOMADEE_API_URL, settings.LOMADEE_APP_TOKEN)

        # Specific path to 'Computer' category
        url = urljoin('{}/'.format(api_url), 'offer/_category/6424')
        kwargs['sourceId'] = settings.LOMADEE_SOURCE_ID
        kwargs['size'] = 100

        return '{}?{}'.format(url, urlencode(kwargs))

    def get_data(self, url=None):
        if not url:
            url = self.build_api_url()

        data = requests.get(url).json()
        if data['requestInfo']['status'] != 'OK':
            return False

        final_data = []
        final_data.extend(data['offers'])
        pagination = data['pagination']

        # Get only 3 pages. To get all pages use:
        # if pagination['page'] < pagination['totalPage']
        if pagination['page'] < 3:
            next_page_data = self.get_data(
                self.build_api_url(page=pagination['page'] + 1)
            )
            final_data.extend(next_page_data)
        return final_data
