Skip to content

Commit e4f74c1

Browse files
committed
Created base header
1 parent 6bdd39f commit e4f74c1

6 files changed

Lines changed: 33 additions & 5 deletions

File tree

AUTHORS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66

77
## Contributors
88

9-
None yet. Why not be the first?
9+
* Brunno Schardosin (https://github.com/scahrd)
10+
* Luciano Camargo Cruz (https://github.com/lccruz)

HISTORY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# History
22

3+
## 0.3.2 (2017-10-25)
4+
5+
* Added base Headers in request.
6+
37
## 0.3.1 (2017-08-07)
48

59
* Fixed bug where GET would send content in the request body.

python_wpapi/python_wpapi.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@
55

66
class WpApi():
77

8-
def __init__(self, base_url, user=None, password=None):
8+
def __init__(self, base_url, user=None, password=None, headers_base={}):
99
self.auth = None
1010
if user:
1111
self.auth = (user, password)
1212
self.base_url = base_url.strip('/') + '/wp-json/wp/v2'
13+
self.headers_base = headers_base
14+
15+
def join_headers(self, headers):
16+
if self.headers_base:
17+
return dict(self.headers_base, **headers)
18+
return headers
1319

1420
def _request(self, endpoint, method='GET', files=None, headers={}, **kwargs):
1521
params = {}
@@ -20,7 +26,10 @@ def _request(self, endpoint, method='GET', files=None, headers={}, **kwargs):
2026
else:
2127
data = kwargs
2228

23-
response = requests.request(method,
29+
headers = self.join_headers(headers)
30+
31+
response = requests.request(
32+
method,
2433
endpoint,
2534
auth=self.auth,
2635
params=params,

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 0.3.1
2+
current_version = 0.3.2
33
commit = True
44
tag = True
55

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
setup(
2121
name='python_wpapi',
22-
version='0.3.1',
22+
version='0.3.2',
2323
description="Simple wrapper around the Wordpress REST API",
2424
long_description=readme + '\n\n' + history,
2525
author="Lucas Lobosque",

tests/test_python_wpapi.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ def test_auth():
4444
assert api2.auth == ('User', None)
4545
assert api3.auth == ('User', 'Password')
4646

47+
def test_auth_header():
48+
"""
49+
Check self.headers_base combinations
50+
"""
51+
headers_base = {'User-agent': 'Mozilla/5.0 (X11; Linux x86_64)'}
52+
headers = {'Content-Length': '0'}
53+
api = python_wpapi.WpApi(
54+
'http://base.url', user='User',
55+
password='Password', headers_base=headers_base
56+
)
57+
assert api.headers_base == headers_base
58+
assert api.join_headers({}) == headers_base
59+
assert api.join_headers(headers) == dict(headers_base, **headers)
60+
4761
@patch.object(python_wpapi.WpApi, '_request')
4862
def test_get_posts(mock, api):
4963
api.get_posts()

0 commit comments

Comments
 (0)