Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
fatboystring wants to merge1 commit intowoocommerce:trunk
base:trunk
Choose a base branch
Loading
fromfatboystring:custom-headers
Open
Show file tree
Hide file tree
Changes fromall 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 deletionsCHANGELOG.md
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsREADME.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionstest_api.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionwoocommerce/__init__.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionswoocommerce/api.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletionwoocommerce/oauth.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp