commit 2c18faefe506a019764ed2211f30498e1fbd7531 Author: Jon Michael Aanes Date: Mon Mar 11 20:39:12 2024 +0100 Initial requests_util diff --git a/README.md b/README.md new file mode 100644 index 0000000..58aa73a --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# Requests utilities + +A set of small utilities that attempts to standardize and ease the experience +of working with `requests_cache` and `requests_ratelimiter`. diff --git a/requests_util/__init__.py b/requests_util/__init__.py new file mode 100644 index 0000000..e92f09f --- /dev/null +++ b/requests_util/__init__.py @@ -0,0 +1,25 @@ +'''Utility module for setting up ratelimiting on third-party sessions.''' + +import logging + +logger = logging.getLogger(__name__) + +try: + import requests_ratelimiter +except ImportError: + requests_ratelimiter = None + logger.warning( + 'Requests ratelimiter could not be loaded. Automatic ratelimit configuration have not be set up.', + ) + + +def setup_limiter(session: requests.Session, url_prefix: str, **limiter_args): requests.Session: + ''' + Initializes limiter on session for the given domain with the given + settings. + + Will always return the given session itself. + ''' + if requests_ratelimiter: + session.mount(url_prefix, requests_ratelimiter.LimiterAdapter(**limiter_args)) + return session diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..72a8231 --- /dev/null +++ b/setup.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python +from setuptools import setup +import re + +PACKAGE_NAME = 'requests_util' + +with open('README.md', 'r') as f: + readme = f.read() + +with open(PACKAGE_NAME+'/_version.py', 'r') as f: + text = f.read() + match = re.match(r'^__version__\s*=\s*(["\'])([\d\.]+)\1$', text) + version = match.group(2) + del match, text + +def get_short_description(readme: str): + readme = re.sub(r'#+[^\n]*\n+', '', readme) + m = re.search(r'^\s*(\w+[\w\s,]+\.)', readme) + readme = m.group(1) + return readme + +setup( + name=PACKAGE_NAME, + version=version, + description=get_short_description(readme), + long_description=readme, + long_description_content_type='text/markdown', + author='Jmaa', + author_email='jonjmaa@gmail.com', + url='https://gitfub.space/Jmaa/'+PACKAGE_NAME, + packages=[PACKAGE_NAME], + install_requires=['requests'], + extras_require = { + 'Request ratelimiting': ['requests_ratelimiter'], + 'Request caching': ['requests_cache'], + }, +)