Skip to content

Allow custom headers to be specified for all api requests #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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+.
Expand Down
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------
Expand Down
22 changes: 22 additions & 0 deletions test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion woocommerce/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"""

__title__ = "woocommerce"
__version__ = "3.0.0"
__version__ = "3.1.0"
__author__ = "Claudio Sanches @ Automattic"
__license__ = "MIT"

Expand Down
18 changes: 13 additions & 5 deletions woocommerce/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""

__title__ = "woocommerce-api"
__version__ = "3.0.0"
__version__ = "3.1.0"
__author__ = "Claudio Sanches @ Automattic"
__license__ = "MIT"

Expand All @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion woocommerce/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""

__title__ = "woocommerce-oauth"
__version__ = "3.0.0"
__version__ = "3.1.0"
__author__ = "Claudio Sanches @ Automattic"
__license__ = "MIT"

Expand Down