Last active
August 17, 2023 20:25
-
-
Save mackintux/2b0e5e8ffdd42eb1fe14655290531d13 to your computer and use it in GitHub Desktop.
Easybroker - Challenge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
class PropertyService: | |
""" | |
Utilizando el lenguaje de programación que prefieras crea una clase que consuma la API de EasyBroker para leer | |
todas las propiedades de nuestra cuenta de staging e imprima sus títulos. Incluye al menos una prueba unitaria y | |
envíanos tu código como un gist. La documentación y las credenciales de la cuenta de staging están disponibles en: | |
https://dev.easybroker.com/docs. | |
""" | |
PROPERTY_CONTENT = 'content' | |
PROPERTY_PAGINATION = 'pagination' | |
PROPERTY_NEXT_PAGE = 'next_page' | |
PROPERTY_TITLE = 'title' | |
def __init__(self, api_url, api_key, limit=20): | |
self.api_url = api_url | |
self.api_key = api_key | |
self.limit = limit | |
def _get_headers(self): | |
return { | |
'accept': 'application/json', | |
'X-Authorization': f'{self.api_key}' | |
} | |
def _has_next_page(self, data): | |
return data.get(self.PROPERTY_PAGINATION).get(self.PROPERTY_NEXT_PAGE) | |
def _get_response(self, page): | |
return requests.get( | |
f'{self.api_url}/properties', | |
headers=self._get_headers(), | |
params={ | |
'page': page, | |
'limit': self.limit | |
} | |
) | |
def _validate_data(self, data): | |
return self.PROPERTY_CONTENT in data and isinstance(data[self.PROPERTY_CONTENT], list) | |
def _iterate_properties(self, data): | |
return [prop[self.PROPERTY_TITLE] for prop in data[self.PROPERTY_CONTENT]] | |
def _get_all_titles_recursive(self, page, all_titles): | |
response = self._get_response(page) | |
data = response.json() | |
if self._validate_data(data): | |
titles = self._iterate_properties(data) | |
all_titles.extend(titles) | |
if self._has_next_page(data): | |
self._get_all_titles_recursive(page + 1, all_titles) | |
def get_property_titles(self): | |
all_titles = [] | |
page = 1 | |
self._get_all_titles_recursive(page, all_titles) | |
return all_titles | |
# Unit test | |
def test_get_property_titles(): | |
api_url = 'https://api.stagingeb.com/v1/' | |
api_key = 'l7u502p8v46ba3ppgvj5y2aad50lb9' | |
property_service = PropertyService(api_url, api_key) | |
titles = property_service.get_property_titles() | |
assert isinstance(titles, list), "La función debe devolver una lista" | |
assert all(isinstance(title, str) for title in titles), "La lista debe contener solo cadenas de texto" | |
assert len(titles) > 0, "La lista de títulos no debe estar vacía" | |
if __name__ == '__main__': | |
test_get_property_titles() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I ended up adding more changes from Easybrocker's perspective related to the Clean Code principle. Now, we could consider two scenarios where a task that could have been simpler became somewhat more complex and modular. Depending on the perspective, this could be either an advantage or a disadvantage. Ultimately, we also need to take into account another well-known principle in the development world (loved by some, disliked by others) called 'KISS' (Keep It Simple, Stupid). When making changes to support one principle, the other can be compromised. However, both of them are ideal in their own ways, as the main purpose, in my view, is related to language proficiency and confidence, whether it's Java, .Net, Python, or any other. We can always find a middle ground.