-
-
Save jsam/d20770339ac0bcb8ab65d42223213aae to your computer and use it in GitHub Desktop.
example api
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
cache = redis.Redis(host='localhost', port=6379, db=0) | |
def check_view_refresh(func): | |
@wraps(func) | |
def wrapper(self, key): | |
views = key.views.split(",") | |
query = (View.name == views.pop(0)) | |
for vw in views: | |
query |= (View.name == vw) | |
view = View.find(name=view) | |
if view.expired: # NOTE: you need to calculate this and update the field in save() | |
view.fetch() | |
return result | |
return wrapper | |
class TranslationKey(HashModel): | |
... | |
@check_view_refresh | |
def get(self, key:str) -> "TranslationKey": | |
return TranslationKey.find( | |
# NOTE: first by snake_name | |
# NOTE: second by id_name | |
language=self.language, | |
) | |
class View(HashModel): | |
name: str = Field(index=True) | |
language: str | |
updated_at: int | |
page_size: int | |
... | |
def save(self, objects): | |
# TODO: implement / override this so that updated_at field always get's updated when we touch the model in DB | |
return | |
def expired(self): | |
# TODO: check if expired | |
return | |
def pages(self) -> List[str]: | |
"""""" | |
urls = [ ... ] | |
return urls | |
def get_page(self, url: str) | |
return requests.get(url) | |
def fetch(self, name: str): | |
urls = self.pages() | |
with ThreadPool(max_workers=12) as pool: | |
result = [for result in pool.map(self.get_page, urls)] | |
self.save(result) | |
... | |
class Client: | |
... | |
def view(self, language, keys_number: int = 500, page_size: int = 500) -> View: | |
return View(language, keys_number, page_size) | |
def by_key(self, language: str) -> TranslationKey: | |
return TranslationKey(language) | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment