diff --git a/CHANGELOG.md b/CHANGELOG.md index d08df1d..3f7880b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [3.1.0] - 2021-03-19 +### Added +- Optional "headers" keyword argument so that extra headers can be sent with every request + ## [3.0.0] - 2021-03-13 ### Removed - Removed support to legacy Python versions, now supports Python 3.6+. diff --git a/README.rst b/README.rst index 38d507b..cc83692 100644 --- a/README.rst +++ b/README.rst @@ -64,6 +64,8 @@ Options +-----------------------+-------------+----------+-------------------------------------------------------------------------------------------------------+ | ``wp_api`` | ``bool`` | no | Set to ``False`` in order to use the legacy WooCommerce API (deprecated) | +-----------------------+-------------+----------+-------------------------------------------------------------------------------------------------------+ +| ``headers`` | ``dict`` | no | Dictionary of extra request headers to send with every api request | ++-----------------------+-------------+----------+-------------------------------------------------------------------------------------------------------+ Methods ------- diff --git a/test_api.py b/test_api.py index c11759e..fec2c8f 100644 --- a/test_api.py +++ b/test_api.py @@ -104,6 +104,28 @@ def woo_test_mock(*args, **kwargs): status = self.api.get("products", allow_redirects=True).status_code self.assertEqual(status, 200) + def test_get_with_custom_headers(self): + """ Test GET requests w/ optional requests-module kwargs """ + api = woocommerce.API( + url="https://woo.test", + consumer_key=self.consumer_key, + consumer_secret=self.consumer_secret, + timeout=10, + headers={'cache-control': 'no-cache'} + ) + + @all_requests + def woo_test_mock(*args, **kwargs): + return {'status_code': 200, + 'content': 'OK'} + + with HTTMock(woo_test_mock): + # call requests + headers = api.get("products").request.headers + self.assertEqual(headers['user-agent'], api.user_agent) + self.assertEqual(headers['accept'], 'application/json') + self.assertEqual(headers['cache-control'], 'no-cache') + def test_post(self): """ Test POST requests """ @all_requests diff --git a/woocommerce/__init__.py b/woocommerce/__init__.py index 15edcc8..777e0e4 100644 --- a/woocommerce/__init__.py +++ b/woocommerce/__init__.py @@ -10,7 +10,7 @@ """ __title__ = "woocommerce" -__version__ = "3.0.0" +__version__ = "3.1.0" __author__ = "Claudio Sanches @ Automattic" __license__ = "MIT" diff --git a/woocommerce/api.py b/woocommerce/api.py index a97c901..12d9e5a 100644 --- a/woocommerce/api.py +++ b/woocommerce/api.py @@ -5,7 +5,7 @@ """ __title__ = "woocommerce-api" -__version__ = "3.0.0" +__version__ = "3.1.0" __author__ = "Claudio Sanches @ Automattic" __license__ = "MIT" @@ -26,12 +26,23 @@ def __init__(self, url, consumer_key, consumer_secret, **kwargs): self.consumer_secret = consumer_secret self.wp_api = kwargs.get("wp_api", True) self.version = kwargs.get("version", "wc/v3") + self.custom_headers = kwargs.get("headers", {}) self.is_ssl = self.__is_ssl() self.timeout = kwargs.get("timeout", 5) self.verify_ssl = kwargs.get("verify_ssl", True) self.query_string_auth = kwargs.get("query_string_auth", False) self.user_agent = kwargs.get("user_agent", f"WooCommerce-Python-REST-API/{__version__}") + def __headers(self): + """ Build and return a dict of headers for the request """ + headers = {} + headers.update({ + "user-agent": f"{self.user_agent}", + "accept": "application/json" + }) + headers.update(self.custom_headers or {}) + return headers + def __is_ssl(self): """ Check if url use HTTPS """ return self.url.startswith("https") @@ -68,10 +79,7 @@ def __request(self, method, endpoint, data, params=None, **kwargs): params = {} url = self.__get_url(endpoint) auth = None - headers = { - "user-agent": f"{self.user_agent}", - "accept": "application/json" - } + headers = self.__headers() if self.is_ssl is True and self.query_string_auth is False: auth = HTTPBasicAuth(self.consumer_key, self.consumer_secret) diff --git a/woocommerce/oauth.py b/woocommerce/oauth.py index 62557c0..fc31a62 100644 --- a/woocommerce/oauth.py +++ b/woocommerce/oauth.py @@ -5,7 +5,7 @@ """ __title__ = "woocommerce-oauth" -__version__ = "3.0.0" +__version__ = "3.1.0" __author__ = "Claudio Sanches @ Automattic" __license__ = "MIT"