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
32 changes: 28 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -68,15 +69,28 @@ 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')
```


[To Top](#buttercms-python)

#### 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

Expand All @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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'])
```
Expand All @@ -167,3 +183,11 @@ client.content_fields.get(['homepage_headline', 'homepage_title'])
### Other

@jakelumetta jakelumetta Nov 3, 2017

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update README to be current + accurate based on the changes in this PR


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
```
2 changes: 2 additions & 0 deletions butter_cms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
1 change: 1 addition & 0 deletions butter_cms/__version__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = '0.8'

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the version is now referenced in setup.py and the request header, I put it in this file. The ideal way to setup the __version__ attribute would be in __init__.py per https://www.python.org/dev/peps/pep-0396/ but I've also seen plenty of packages put it in __version__.py. Doing this per the spec caused some import issues.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine. Stripe puts there's into a VERSION file: https://github.com/stripe/stripe-python/blob/master/VERSION

10 changes: 9 additions & 1 deletion butter_cms/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import requests

from .__version__ import __version__


class Client(object):
"""Client"""
Expand All @@ -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()

Expand Down
7 changes: 5 additions & 2 deletions butter_cms/content_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
16 changes: 16 additions & 0 deletions butter_cms/page.py
Original file line number Diff line number Diff line change
@@ -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)
17 changes: 11 additions & 6 deletions butter_cms/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ def __init__(self, auth_token):
def all(self, params=None):

@jakelumetta jakelumetta Nov 3, 2017

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does all() result in a trailing slash? /posts/?...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
18 changes: 15 additions & 3 deletions butter_cms/unit_tests.py
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys

from butter_cms.__version__ import __version__

try:
from setuptools import setup
Expand All @@ -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',
Expand Down