import os

from peolymp.client import HTTPClient


class AbstractAPI:
    def __init__(self, space_id):
        username = os.getenv('EOLYMP_USERNAME')
        password = os.getenv('EOLYMP_PASSWORD')
        self.space_id = space_id
        self.http_client = HTTPClient(username, password, space_id)

    def get_url(self):
        return "https://api.eolymp.com/spaces/" + self.space_id

    def get_many(self, f, filter=None):
        items = []
        offset = 0
        size = 100
        while True:
            m = f(offset=offset, size=size)
            for item in m.items:
                if filter is None or filter(item):
                    items += [item]
            if len(m.items) != size:
                break
            offset += size
            print(offset)
        return items
