Skip to main content

Python package for caching HTTP response based on etag

Project description

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

To-do: Release it as pypi package

Usage

import requests
from etag_package import EtagCache


cache_object = EtagCache()
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} .....

Project details


Release history Release notifications | RSS feed

This version

0.2

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

etag_cache-0.2-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file etag_cache-0.2-py3-none-any.whl.

File metadata

  • Download URL: etag_cache-0.2-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.3.1 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.6.9

File hashes

Hashes for etag_cache-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2951a86f8bdfa9e2f2a64da1bb97cc86385f036016c5b52bcb64b83bcf708d80
MD5 392791165d2f73d03f15b0469594b4d1
BLAKE2b-256 084611d04d4dd72888663c40e9959b59417f2e2f628058f5a7e5125895d6b584

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page