Skip to content

Instantly share code, notes, and snippets.

@SnelleJelle
Created September 1, 2018 22:37
Show Gist options
  • Save SnelleJelle/2ff46b5868580d2ee9a34cfb761d9de8 to your computer and use it in GitHub Desktop.
Save SnelleJelle/2ff46b5868580d2ee9a34cfb761d9de8 to your computer and use it in GitHub Desktop.
he shoots
import json
import requests
class RequestCache:
cache = None
cache_file_name = "request_cache.json"
cache_key = "cachedRequests"
@staticmethod
def load():
try:
cache_file_content = open(RequestCache.cache_file_name).read()
RequestCache.cache = json.loads(cache_file_content)[RequestCache.cache_key]
except FileNotFoundError:
RequestCache.create_cache()
RequestCache.load()
@staticmethod
def get(url: str):
if url in RequestCache.cache:
return CacheObject(RequestCache.cache[url])
else:
response = requests.get(url)
cached_response = RequestCache.add_to_cache(response)
return CacheObject(cached_response)
@staticmethod
def add_to_cache(response) -> any:
url = response.request.url
cached_response = {
"status_code": response.status_code,
"text": response.text
}
RequestCache.cache[url] = cached_response
return cached_response
@staticmethod
def create_cache():
with open(RequestCache.cache_file_name, "w+") as cache_file:
json.dump(RequestCache.get_empty_cache(), cache_file)
@staticmethod
def flush_cache():
full_cache = {RequestCache.cache_key: RequestCache.cache}
with open(RequestCache.cache_file_name, "w+") as cache_file:
json.dump(full_cache, cache_file, indent=4)
@staticmethod
def get_empty_cache():
# {"cachedRequests": [ {"url": "https://jelle.com/file.txt", "response": "<html></html>"} ] }
return {RequestCache.cache_key: {}}
class CacheObject:
def __init__(self, response: dict):
self.__status_code = response["status_code"]
self.__text = response["text"]
@property
def status_code(self):
return self.__status_code
@property
def text(self):
return self.__text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment