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

Commitab3f21a

Browse files
author
Dan Stringer
committed
Allow custom headers to be specified for all api requests
1 parent003e299 commitab3f21a

File tree

6 files changed

+43
-7
lines changed

6 files changed

+43
-7
lines changed

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
##[Unreleased]
88

9+
##[3.1.0] - 2021-03-19
10+
###Added
11+
- Optional "headers" keyword argument so that extra headers can be sent with every request
12+
913
##[3.0.0] - 2021-03-13
1014
###Removed
1115
- Removed support to legacy Python versions, now supports Python 3.6+.

‎README.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ Options
6464
+-----------------------+-------------+----------+-------------------------------------------------------------------------------------------------------+
6565
| ``wp_api``| ``bool``| no| Set to ``False`` in order to use the legacy WooCommerce API (deprecated)|
6666
+-----------------------+-------------+----------+-------------------------------------------------------------------------------------------------------+
67+
| ``headers``| ``dict``| no| Dictionary of extra request headers to send with every api request|
68+
+-----------------------+-------------+----------+-------------------------------------------------------------------------------------------------------+
6769

6870
Methods
6971
-------

‎test_api.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,28 @@ def woo_test_mock(*args, **kwargs):
104104
status=self.api.get("products",allow_redirects=True).status_code
105105
self.assertEqual(status,200)
106106

107+
deftest_get_with_custom_headers(self):
108+
""" Test GET requests w/ optional requests-module kwargs """
109+
api=woocommerce.API(
110+
url="https://woo.test",
111+
consumer_key=self.consumer_key,
112+
consumer_secret=self.consumer_secret,
113+
timeout=10,
114+
headers={'cache-control':'no-cache'}
115+
)
116+
117+
@all_requests
118+
defwoo_test_mock(*args,**kwargs):
119+
return {'status_code':200,
120+
'content':'OK'}
121+
122+
withHTTMock(woo_test_mock):
123+
# call requests
124+
headers=api.get("products").request.headers
125+
self.assertEqual(headers['user-agent'],api.user_agent)
126+
self.assertEqual(headers['accept'],'application/json')
127+
self.assertEqual(headers['cache-control'],'no-cache')
128+
107129
deftest_post(self):
108130
""" Test POST requests """
109131
@all_requests

‎woocommerce/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"""
1111

1212
__title__="woocommerce"
13-
__version__="3.0.0"
13+
__version__="3.1.0"
1414
__author__="Claudio Sanches @ Automattic"
1515
__license__="MIT"
1616

‎woocommerce/api.py‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""
66

77
__title__="woocommerce-api"
8-
__version__="3.0.0"
8+
__version__="3.1.0"
99
__author__="Claudio Sanches @ Automattic"
1010
__license__="MIT"
1111

@@ -26,12 +26,23 @@ def __init__(self, url, consumer_key, consumer_secret, **kwargs):
2626
self.consumer_secret=consumer_secret
2727
self.wp_api=kwargs.get("wp_api",True)
2828
self.version=kwargs.get("version","wc/v3")
29+
self.custom_headers=kwargs.get("headers", {})
2930
self.is_ssl=self.__is_ssl()
3031
self.timeout=kwargs.get("timeout",5)
3132
self.verify_ssl=kwargs.get("verify_ssl",True)
3233
self.query_string_auth=kwargs.get("query_string_auth",False)
3334
self.user_agent=kwargs.get("user_agent",f"WooCommerce-Python-REST-API/{__version__}")
3435

36+
def__headers(self):
37+
""" Build and return a dict of headers for the request """
38+
headers= {}
39+
headers.update({
40+
"user-agent":f"{self.user_agent}",
41+
"accept":"application/json"
42+
})
43+
headers.update(self.custom_headersor {})
44+
returnheaders
45+
3546
def__is_ssl(self):
3647
""" Check if url use HTTPS """
3748
returnself.url.startswith("https")
@@ -68,10 +79,7 @@ def __request(self, method, endpoint, data, params=None, **kwargs):
6879
params= {}
6980
url=self.__get_url(endpoint)
7081
auth=None
71-
headers= {
72-
"user-agent":f"{self.user_agent}",
73-
"accept":"application/json"
74-
}
82+
headers=self.__headers()
7583

7684
ifself.is_sslisTrueandself.query_string_authisFalse:
7785
auth=HTTPBasicAuth(self.consumer_key,self.consumer_secret)

‎woocommerce/oauth.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""
66

77
__title__="woocommerce-oauth"
8-
__version__="3.0.0"
8+
__version__="3.1.0"
99
__author__="Claudio Sanches @ Automattic"
1010
__license__="MIT"
1111

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp