apicontroller
apicontroller
is a Python package to manage REST API requests.
We provide examples to use it.
$ git clone https://github.com/francois-le-ko4la/rest-api-controller.git
$ cd rest-api-controller
$ make install
$ make test
- Import the package
from apicontroller import RestAPIController
- API wo authentication using tests
import time
#init the Class
my_api = RestAPIController(host="http://api.open-notify.org",
DEBUG=True)
# send the request
result = my_api.request("GET", "/iss-now.json")
# test result
if result is None:
exit()
# use the result
print(result)
print(time.ctime(int(result['timestamp'])))
print(result['iss_position']['longitude'] + ", " +
result['iss_position']['latitude'])
- API OAuth1 (username / token) - GITHUB
my_github_api = RestAPIController(
host = "https://api.github.com",
auth = ("*** USERNAME ***", "*** TOKEN ***"),
DEBUG= True
)
my_github_api.request("GET", "/user")
- OAuth Token - Facebook
my_fb_api = RestAPIController(
token=("OAuth", "*** TOKEN ***"),
host = "https://graph.facebook.com",
DEBUG = True
)
my_fb_api.request("GET", "/v2.12/me/taggable_friends",
{'fields':'id, name, picture.width(500).height(500).type(large)',
'limit':'5000'})
.
├── apicontroller
│ ├── __about__.py
│ ├── controller.py
│ ├── exceptions.py
│ └── __init__.py
├── last_check.log
├── LICENSE
├── Makefile
├── pictures
│ ├── classes_apicontroller.png
│ └── packages_apicontroller.png
├── README.md
├── runtime.txt
├── setup.cfg
├── setup.py
└── tests
├── test_doctest.py
├── test_pycodestyle.py
└── use_it.py
- Create the project
- Write code and tests
- Test installation and requirements (setup.py and/or Makefile)
- Test code
- Validate features
- Write Doc/stringdoc
- Run PEP8 validation
- Clean & last check
- Release
- improve (un)install
- manage global var
- Release: 0.2.0
- improve package var
- improve exceptions
- Release: 0.3.0
This package is distributed under the GPLv3 license
python-3.6.x
RestAPIController()
@Property RestAPIController.host
RestAPIController.isconnected()
RestAPIController.request()
APIError()
APIConnectionError()
APIHTTPError()
APIBadHostnameError()
class RestAPIController(object):
My REST API Controller
Use:
>>> # oups
>>> my_api = RestAPIController(host="http://pz.g")
>>> print(my_api.request("GET", "/iss-now.json"))
Traceback (most recent call last):
...
apicontroller.exceptions.APIConnectionError: "http://pz.g" unreachable!
>>> # oups 2
>>> my_api = RestAPIController(host="http://api.open-notify.org")
>>> result = my_api.request("GREP", "/iss-now.json")
Traceback (most recent call last):
...
apicontroller.exceptions.APIHTTPError: Client Error - {'method': 'GREP', 'url': 'http://api.open-notify.org/iss-now.json'}.
>>> # oups 3
>>> my_api = RestAPIController(host="api.open-notify.org")
Traceback (most recent call last):
...
apicontroller.exceptions.APIBadHostnameError: "api.open-notify.org" is not validated!
>>> my_api = RestAPIController(host="http://api.open-notify.org")
>>> result = my_api.request("GET", "/iss-now.json")
>>> result['message']
'success'
@property
def RestAPIController.host(self):
None
@__isconnected
def RestAPIController.isconnected(self):
Provide a link status between this script & REST API server
This function is decorated by @__isconnected.
If the link is down, then decorator will change the value.
Args:
None
Returns:
True
@__loadjson
@__sendrequest
@__isconnected
@__enable_debug
def RestAPIController.request(self, cur_method, cur_path, cur_args=None):
Provide all arguments to request.
This function is decorated by __enable_debug,
__isconnected, __sendrequest, and __loadjson
Args:
cur_method (str): "GET", "PUT" ...
cur_path (str): url = host+path
cur_args (dict): HTTP args
Returns:
dict(): request's args send to decorators
class APIError(Exception):
Generic exception for apicontroller
class APIConnectionError(APIError):
None
class APIHTTPError(APIError):
None
class APIBadHostnameError(APIError):
None