def setUp(self): self.api_client = SWAPIClient() fake_responses = [ # (:method, :uri, :status, :fixture) # DETAIL /people ('GET', r'{}/people/100', 404, 'people_id_100.json'), ('GET', r'{}/people/1', 200, 'people_id_1.json'), # LIST /people ('GET', r'{}/people\?page=1', 200, 'people_page_1.json'), ('GET', r'{}/people\?page=2', 200, 'people_page_2.json'), ('GET', r'{}/people\?page=3', 404, 'people_page_3.json'), ('GET', r'{}/people', 200, 'people_page_1.json'), # DETAIL /films ('GET', r'{}/films/100', 404, 'films_id_100.json'), ('GET', r'{}/films/1', 200, 'films_id_1.json'), # LIST /films ('GET', r'{}/films\?page=1', 200, 'films_page_1.json'), ('GET', r'{}/films\?page=2', 404, 'films_page_2.json'), ('GET', r'{}/films', 200, 'films_page_1.json'), ] for method, uri, status, fixture_file in fake_responses: fixture_path = os.path.join(settings.BASE_DIR, 'tests', 'fixtures', fixture_file) with open(fixture_path) as f: json_fixture = json.load(f) responses.add(method, re.compile(uri.format(settings.BASE_URL)), json=json_fixture, status=status, content_type='application/json')
from starwars_api.client import SWAPIClient from starwars_api.exceptions import SWAPIClientError api_client = SWAPIClient() class BaseModel(object): RESOURCE_NAME = None def __init__(self, json_data): """ Dynamically assign all attributes in `json_data` as instance attributes of the Model. """ for key, value in json_data.items(): setattr(self, key, value) @classmethod def get(cls, resource_id): """ Returns an object of current Model requesting data to SWAPI using the api_client. """ method = 'get_{}'.format(cls.RESOURCE_NAME) method_name = getattr(api_client, method) json_data = method_name(resource_id) return cls(json_data) @classmethod def all(cls):