Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
amadison
committed
Apr 19, 2012
1 parent
d318b48
commit 2657835
Showing
6 changed files
with
166 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
Copyright (c) 2012, Aaron Madison | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without modification, | ||
are permitted provided that the following conditions are met: | ||
|
||
1. Redistributions of source code must retain the above copyright notice, | ||
this list of conditions and the following disclaimer. | ||
|
||
2. Redistributions in binary form must reproduce the above copyright | ||
notice, this list of conditions and the following disclaimer in the | ||
documentation and/or other materials provided with the distribution. | ||
|
||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | ||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
apyclient | ||
========= | ||
|
||
A Python Api Client | ||
|
||
Overview | ||
-------- | ||
|
||
Allows you to easily create client APIs in a highly customizable way. | ||
|
||
|
||
Installation | ||
------------ | ||
Only requirement is python 2.6 or greater. Tests require 'mock' package. | ||
|
||
:: | ||
|
||
pip install apyclient | ||
|
||
|
||
Usage | ||
----- | ||
|
||
You are able to easily create a client Api class. The only requirement of | ||
the Api class is that it must have a "HOST_NAME" attribute declared. The API | ||
uses this host name to prepend to the endpoint when building the request. | ||
|
||
:: | ||
|
||
class MyApiClient(object): | ||
HOST_NAME = "http://www.example.com/api | ||
|
||
@api_request("/api-endpoint/") | ||
def fetch_some_stuff(some_var): | ||
return {"the_variable": some_var} | ||
|
||
my_client = MyApiClient() | ||
my_client.fetch_some_stuff(3) | ||
|
||
And that's it. The client will make an Http GET request by default with the | ||
data provided by the decorated method. | ||
|
||
You can also do a POST request by declaring ``method="POST"`` in the api_request. | ||
|
||
:: | ||
|
||
@api_request("/api-endpoint/", method="POST") | ||
def fetch_some_stuff(some_var): | ||
return {"the_variable": some_var} | ||
|
||
|
||
And finally, you are able to return a custom response class if you so desire. | ||
Just either provide a ``RESPONSE_CLASS`` on the api client class, or a | ||
``response_class`` on the api_request decorator. If you have a custom response | ||
class declared both on the API client and on the api_request decorator, the | ||
decorator will win because it is more specific. The response class must take | ||
one argument on initialization, the original response. | ||
|
||
:: | ||
|
||
class MyApiClient(object): | ||
HOST_NAME = "http://www.example.com/api | ||
RESPONSE_CLASS = MyDefaultResponseClass | ||
|
||
@api_request("/api-endpoint/") | ||
def fetch_some_stuff(some_var): | ||
return {"the_variable": some_var} | ||
|
||
@api_request("/api-endpoint/", response_class=SpecializedResponseClass) | ||
def fetch_some_stuff(some_var): | ||
return {"the_variable": some_var} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
|
||
import os | ||
|
||
readme = os.path.join(os.path.dirname(__file__), 'README.rst') | ||
LONG_DESCRIPTION = open(readme, 'r').read() | ||
|
||
params = dict( | ||
name='apyclient', | ||
version='0.0.1', | ||
packages=[''], | ||
url='https://github.com/madisona/apyclient', | ||
license='BSD', | ||
author='Aaron Madison', | ||
author_email='aaron.l.madison@gmail.com', | ||
description='A Python Api Client', | ||
long_description=LONG_DESCRIPTION, | ||
py_modules=['apyclient'], | ||
|
||
zip_safe=False, | ||
classifiers = [ | ||
"Development Status :: 4 - Beta", | ||
"Intended Audience :: Developers", | ||
"License :: OSI Approved :: BSD License", | ||
"Operating System :: OS Independent", | ||
"Programming Language :: Python", | ||
"Programming Language :: Python :: 2.7", | ||
"Topic :: Software Development", | ||
"Topic :: Software Development :: Libraries :: Application Frameworks", | ||
"Topic :: Software Development :: Libraries :: Python Modules", | ||
], | ||
) | ||
|
||
|
||
try: | ||
from setuptools import setup | ||
except ImportError: | ||
from distutils.core import setup | ||
else: | ||
params['tests_require'] = ['unittest2', 'mock'] | ||
params['test_suite'] = 'unittest2.collector' | ||
|
||
setup(**params) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters