Skip to content

Commit a514b2d

Browse files
committed
Merge branch 'feature/http-cache' into dev
2 parents b5722e0 + 662097d commit a514b2d

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

.idea/plusclouds-cli.iml

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plusclouds/gateway/http_cache.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
def cache_constructor(cache_dict: dict):
2+
def cache_decorator(func):
3+
4+
def callback(*args, **kwargs):
5+
url = kwargs.get("url", "")
6+
if url == "":
7+
url = "_"
8+
9+
if len(args) > 1:
10+
url = args[1]
11+
12+
if url in cache_dict.keys():
13+
print("returning cached dir")
14+
return cache_dict[url]
15+
16+
resp = func(*args, **kwargs)
17+
cache_dict[url] = resp
18+
print("new cache on {}".format(url))
19+
20+
return resp
21+
22+
return callback
23+
24+
return cache_decorator

plusclouds/gateway/http_client.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22

33
from plusclouds.settings import plusclouds_url
44
from plusclouds.util.token_util import get_token
5+
from plusclouds.gateway.http_cache import cache_constructor
6+
7+
get_dict = {}
8+
get_cache = cache_constructor(get_dict)
9+
10+
post_dict = {}
11+
post_cache = cache_constructor(post_dict)
12+
13+
delete_dict = {}
14+
delete_cache = cache_constructor(delete_dict)
15+
16+
options_dict = {}
17+
options_cache = cache_constructor(options_dict)
518

619

720
class HttpGateway:
@@ -11,11 +24,13 @@ def __init__(self, base_url: str = plusclouds_url):
1124
self.latest_request = None
1225
self.option_request = None
1326

27+
@get_cache
1428
def get(self, url: str = "") -> requests.Response:
1529
full_url = plusclouds_url + url
1630
self.latest_request = requests.get(full_url, headers={"Authorization": "Bearer " + self.token.strip()})
1731
return self.latest_request
1832

33+
@post_cache
1934
def post(self, url: str = "", body=None) -> requests.Response:
2035
if body is None:
2136
body = {}
@@ -25,6 +40,7 @@ def post(self, url: str = "", body=None) -> requests.Response:
2540
headers={"Authorization": "Bearer " + self.token.strip()})
2641
return self.latest_request
2742

43+
@options_cache
2844
def options(self, url: str = "") -> requests.Response:
2945
self.option_request = requests.request("OPTIONS", self.base_url + url)
3046
return self.option_request

0 commit comments

Comments
 (0)