etag-response-cache
Etag cache implementation for HTTP requests, to save request bandwidth for a non-modified response. Returns high-speed accessed dictionary data as cache.
Concept
Store Etag value(as hash key:value pair) in the user's home directory os.path.join(os.getenv("HOME"), ".pyapp") into etag and successful response data into cache DBM files.
Add If-None-Match header for all GET request.
Update etag and cache for required request url, when none 304 http status is received i.e if response is modified.
Return cache data as dictionary for non-modified response.
Sample GET request header with If-None-Match:
{'X-Kite-Version': '3', 'User-Agent': 'Kiteconnect-python/3.9.4',
'If-None-Match': 'W/"i51p01GqP6TRPWsM"',
'Authorization': 'token api_key:access_token'}
Installation
pip install etag_cache
Usage
import requests
from etag_cache import EtagCache
# dir_path defaults to $HOME/.pyapp, if not given
cache_object = EtagCache(dir_path='your_defined_cache_directory_path')
url = "https://api.kite.trade/portfolio/holdings"
payload = ""
headers = {
'x-kite-version': "3",
'authorization': "token api_key:access_token"
}
# Add etag to request header
headers = cache_object.add_etag("GET", headers, url)
response = requests.request("GET", url, data=payload, headers=headers)
# Store etag from response header
cache_object.save_etag(response)
# Store response data as cache
response_dict = cache_object.add_read_cache(response)
print(response_dict)
Response
DEBUG:urllib3.connectionpool:https://api.kite.trade:443 "GET /portfolio/holdings HTTP/1.1" 304 0
{'tradingsymbol': 'APOLLOPIPE', 'exchange': 'NSE', 'instrument_token': 3676417, 'isin': 'INE126J01016',
'product': 'CNC', 'price': 0, 'quantity': 3, 'used_quantity': 0, 't1_quantity': 0, 'realised_quantity': 3,
'authorised_quantity': 0, 'authorised_date': '2021-12-28 00:00:00', 'opening_quantity': 3, 'short_quantity':
0, 'collateral_quantity': 0, 'collateral_type': '', 'discrepancy': False, 'average_price': 582.666667,
'last_price': 539.8, 'close_price': 539.65, 'pnl': -128.60000100000002, 'day_change': 0.14999999999997726,
'day_change_percentage': 0.027795793569902208} .....