Skip to content
Merged
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
3 changes: 2 additions & 1 deletion AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@

## Contributors

None yet. Why not be the first?
* Brunno Schardosin (https://github.com/scahrd)
* Luciano Camargo Cruz (https://github.com/lccruz)
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# History

## 0.3.2 (2017-10-25)

* Added base Headers in request.

## 0.3.1 (2017-08-07)

* Fixed bug where GET would send content in the request body.
Expand Down
13 changes: 11 additions & 2 deletions python_wpapi/python_wpapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@

class WpApi():

def __init__(self, base_url, user=None, password=None):
def __init__(self, base_url, user=None, password=None, headers_base={}):
self.auth = None
if user:
self.auth = (user, password)
self.base_url = base_url.strip('/') + '/wp-json/wp/v2'
self.headers_base = headers_base

def join_headers(self, headers):
if self.headers_base:
return dict(self.headers_base, **headers)
return headers

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

response = requests.request(method,
headers = self.join_headers(headers)

response = requests.request(
method,
endpoint,
auth=self.auth,
params=params,
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.3.1
current_version = 0.3.2
commit = True
tag = True

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

setup(
name='python_wpapi',
version='0.3.1',
version='0.3.2',
description="Simple wrapper around the Wordpress REST API",
long_description=readme + '\n\n' + history,
author="Lucas Lobosque",
Expand Down
14 changes: 14 additions & 0 deletions tests/test_python_wpapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ def test_auth():
assert api2.auth == ('User', None)
assert api3.auth == ('User', 'Password')

def test_auth_header():
"""
Check self.headers_base combinations
"""
headers_base = {'User-agent': 'Mozilla/5.0 (X11; Linux x86_64)'}
headers = {'Content-Length': '0'}
api = python_wpapi.WpApi(
'http://base.url', user='User',
password='Password', headers_base=headers_base
)
assert api.headers_base == headers_base
assert api.join_headers({}) == headers_base
assert api.join_headers(headers) == dict(headers_base, **headers)

@patch.object(python_wpapi.WpApi, '_request')
def test_get_posts(mock, api):
api.get_posts()
Expand Down