diff --git a/README.md b/README.md index c9a5f73..fbcc405 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ For a comprehensive list of examples, check out the [API documentation](https:// ## Jump to: * [Posts](#posts) +* [Pages](#pages) * [Authors](#authors) * [Categories](#categories) * [Feeds](#feeds) @@ -68,7 +69,20 @@ client.posts.get('hello-world') ```python -client.posts.search('query', page=1, page_size=10) +client.posts.search('query', {'page': 1, 'page_size': 10}) +``` + +#### Pages + +The Page's `.list()` and `.get()` methods accept an optional `params` to add additional data to the response. + +```python +client.pages.list('news') +``` + + +```python +client.pages.get('news', 'hello-world') ``` @@ -76,7 +90,7 @@ client.posts.search('query', page=1, page_size=10) #### Authors -The Author's `.all()` and `.get()` method accept an optional `params` to add additional data to the repsonse. +The Author's `.all()` and `.get()` methods accept an optional `params` to add additional data to the response. * `{'include':'recent_posts'}`: Adds each author's posts under the `recent_posts` key in that author's returned dictionary @@ -96,7 +110,7 @@ client.authors.get('jennifer-smith', {'include':'recent_posts'}) #### Categories -The Category's `.all()` and `.get()` methods accept an optional `params` to add additional data to the repsonse. +The Category's `.all()` and `.get()` methods accept an optional `params` to add additional data to the response. * `{'include':'recent_posts'}`: Adds posts tagged with that category under the `recent_posts` key in that category's returned dictionary @@ -117,7 +131,7 @@ client.categories.get('product-updates', {'include':'recent_posts'}) #### Tags -The Tag's `.all()` and `.get()` methods accept an optional `params` to add additional data to the repsonse. +The Tag's `.all()` and `.get()` methods accept an optional `params` to add additional data to the response. * `{'include':'recent_posts'}`: Adds posts tagged with that tag under the `recent_posts` key in that tag's returned dictionary @@ -156,6 +170,8 @@ client.feeds.get('sitemap') #### Content Fields +The Content Field's .get() method accepts an optional `params` to add additional data to the response. + ```python client.content_fields.get(['homepage_headline', 'homepage_title']) ``` @@ -167,3 +183,11 @@ client.content_fields.get(['homepage_headline', 'homepage_title']) ### Other View Python [Blog engine](https://buttercms.com/python-blog-engine/) and [Full CMS](https://buttercms.com/python-cms/) for other examples of using ButterCMS with Python. + +### Tests + +To run tests: + +```python +python -m unittest butter_cms/unit_tests.py +``` diff --git a/butter_cms/__init__.py b/butter_cms/__init__.py index 41dc60f..c4a19ff 100644 --- a/butter_cms/__init__.py +++ b/butter_cms/__init__.py @@ -2,6 +2,7 @@ from .category import Category from .content_field import ContentField from .feed import Feed +from .page import Page from .post import Post from .tag import Tag @@ -14,4 +15,5 @@ def __init__(self, auth_token): self.tags = Tag(auth_token) self.content_fields = ContentField(auth_token) self.feeds = Feed(auth_token) + self.pages = Page(auth_token) self.posts = Post(auth_token) diff --git a/butter_cms/__version__.py b/butter_cms/__version__.py new file mode 100644 index 0000000..376df7c --- /dev/null +++ b/butter_cms/__version__.py @@ -0,0 +1 @@ +__version__ = '0.8' diff --git a/butter_cms/client.py b/butter_cms/client.py index dd18e73..352c662 100644 --- a/butter_cms/client.py +++ b/butter_cms/client.py @@ -1,5 +1,7 @@ import requests +from .__version__ import __version__ + class Client(object): """Client""" @@ -13,9 +15,15 @@ def api_get(self, slug='', params=None): } if params: payload.update(params) + + headers = { + 'X-Butter-Client': 'Python/{}'.format(__version__), + } + response = requests.get( url=self.url + self.path + str(slug), - params=payload + params=payload, + headers=headers, ) return response.json() diff --git a/butter_cms/content_field.py b/butter_cms/content_field.py index a51029a..f6d57d2 100644 --- a/butter_cms/content_field.py +++ b/butter_cms/content_field.py @@ -7,5 +7,8 @@ def __init__(self, auth_token): Client.__init__(self, auth_token) self.path = 'content/' - def get(self, keys=None): - return self.api_get(params={'keys': ','.join(keys)}) + def get(self, keys=None, params=None): + if not params: + params = {} + params['keys'] = ','.join(keys) + return self.api_get(params=params) diff --git a/butter_cms/page.py b/butter_cms/page.py new file mode 100644 index 0000000..065a460 --- /dev/null +++ b/butter_cms/page.py @@ -0,0 +1,16 @@ +from .client import Client + + +class Page(Client): + """Page""" + def __init__(self, auth_token): + Client.__init__(self, auth_token) + self.path = 'pages/' + + def list(self, page_type, params=None): + full_slug = '{}/'.format(page_type) + return self.api_get(slug=full_slug, params=params) + + def get(self, page_type, page_slug, params=None): + full_slug = '{}/{}/'.format(page_type, page_slug) + return self.api_get(slug=full_slug, params=params) diff --git a/butter_cms/post.py b/butter_cms/post.py index 34a38fb..ff12849 100644 --- a/butter_cms/post.py +++ b/butter_cms/post.py @@ -10,10 +10,15 @@ def __init__(self, auth_token): def all(self, params=None): return self.api_get(params=params) - def search(self, query='', page=1, page_size=10): - params = { - 'query': query, - 'page': page, - 'page_size': page_size, - } + def get(self, slug): + full_slug = '{}/'.format(slug) + return self.api_get(slug=full_slug) + + def search(self, query, params=None): + if not params: + params = { + 'query': query, + } + else: + params['query'] = query return self.api_get(params=params) diff --git a/butter_cms/unit_tests.py b/butter_cms/unit_tests.py index 01f8e00..bfedda5 100644 --- a/butter_cms/unit_tests.py +++ b/butter_cms/unit_tests.py @@ -1,10 +1,10 @@ import unittest from .author import Author -from .butter_cms import ButterCMS from .category import Category from .tag import Tag from .content_field import ContentField from .feed import Feed +from .page import Page from .post import Post auth_token = 'f97d131d955f48af0769a4c827bb47728cbd5d05' @@ -121,19 +121,31 @@ def test_get_sitemap(self): self.is_ok_request(response) +class TestPage(TestAPI): + def test_list(self): + page = Page(auth_token) + response = page.list('news') + self.is_ok_request(response) + + def test_get(self): + page = Page(auth_token) + response = page.get('news', 'hello-world') + self.is_ok_request(response) + + class TestPost(TestAPI): def test_all(self): post = Post(auth_token) response = post.all() self.is_ok_request(response) - response = post.all(page=1, page_size=10) + response = post.all({'page': 1, 'page_size': 10}) self.is_ok_request(response) def test_search(self): post = Post(auth_token) response = post.search('test') self.is_ok_request(response) - response = post.search('test', page=1, page_size=10) + response = post.search('test', {'page': 1, 'page_size': 10}) self.is_ok_request(response) def test_get(self): diff --git a/setup.py b/setup.py index b56d330..9a00911 100644 --- a/setup.py +++ b/setup.py @@ -1,5 +1,6 @@ import sys +from butter_cms.__version__ import __version__ try: from setuptools import setup @@ -25,7 +26,7 @@ setup( name = 'buttercms-python', packages = ['butter_cms'], - version = '0.7', + version = __version__, description = 'API First Blogging and CMS platform built for developers', long_description=readme, author = 'Adam Yala',