PK ~C\kG rieapie-latest/license.html
Copyright (c) 2013 Ali-Akber Saifee
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Please activate JavaScript to enable the search functionality.
From here you can search these documents. Enter your search words into the box below and click "search". Note that the search function will automatically search for all of the words. Pages containing fewer words won't appear in the result list.
The top level interfaces to rieapie is the Api object.
Every subsequent access to a path component from the Api returns a rieapie.trickery.Component object.
Bases: object
An example for accessing google maps rest api using rieapie.
import rieapie
gmap = rieapie.Api("http://maps.googleapis.com/maps/api")
args = {
"origin" : "Toronto"
,"destination" : "Montreal"
,"avoid" : "highways"
,"mode" : "bicycling"
,"sensor" : "false"
}
directions = gmap.directions.json.get(**args)
print directions["routes"][0]["bounds"]
An example for accessing the twitter api with the provided twitter wrapper.
import rieapie
params = {
"consumer_key" : "..."
,"consumer_secret" : "..."
}
# or if you want to provide an access token
params = {
"consumer_key" : "..."
,"consumer_secret" : "..."
,"access_token" : "..",
,"access_token_secret" : ".."
}
twitter = rieapie.wrappers.Twitter( **params )
timeline = twitter.statuses.user_timeline(ext="json")
# or if you prefer this syntax
timeline = twitter.statuses["user_timeline.json"]
for status in timeline.get(count=10, screen_name="mybestfriend"):
print status['text']
"""
"""
version = "0.0.7.1"
try:
from rieapie.trickery import Api, pre_request,post_request
import rieapie.wrappers
except ImportError as e:
print("error initializing rieapie")
import requests
import logging
import functools
import inspect
import json
GET = "GET"
PUT = "PUT"
POST = "POST"
DELETE = "DELETE"
[docs]class Component(object):
def __init__(self, name, api_object, parent=None):
self.name = str(name)
self.parent = parent
self.api_object = api_object
def __getattribute__(self, key):
try:
return object.__getattribute__(self, key)
except AttributeError:
return Component(key, self.api_object, self)
def __full_path(self):
return "/".join([self.api_object.base_url, self.__path()])
def __path(self):
path = []
cur = self
while cur.parent:
path += [cur.name]
cur = cur.parent
path += [cur.name]
return "/".join(reversed(path))
def __repr__(self):
return self.__path().replace("/", ".")
def __call__(self, ext=""):
if ext:
return Component("%s.%s" % (self.name, ext), self.api_object,
self.parent)
return self
def __getitem__(self, key):
return Component(key, self.api_object, self)
[docs] def get(self, **kwargs):
url, params, _, headers = self.api_object.execute_pre_request(
GET, self.__full_path(), kwargs, None,
self.api_object.headers)
resp = self.api_object.session.get(url, params=params, headers=headers)
return self.api_object.execute_post_request(resp.status_code,
resp.text)
[docs] def delete(self, **kwargs):
url, params, _, headers = self.api_object.execute_pre_request(
DELETE, self.__full_path(), kwargs, None,
self.api_object.headers)
resp = self.api_object.session.delete(url, params=params,
headers=headers)
return self.api_object.execute_post_request(resp.status_code,
resp.text)
[docs] def create(self, **kwargs):
url, params, data, headers = self.api_object.execute_pre_request(
PUT, self.__full_path(), {}, kwargs,
self.api_object.headers)
resp = self.api_object.session.put(url, params=params, data=data,
headers=headers)
return self.api_object.execute_post_request(resp.status_code,
resp.text)
[docs] def update(self, **kwargs):
url, params, data, headers = self.api_object.execute_pre_request(
POST, self.__full_path(), {}, kwargs,
self.api_object.headers)
resp = self.api_object.session.post(url, params=params, data=data,
headers=headers)
return self.api_object.execute_post_request(resp.status_code,
resp.text)
def pre_request(fn):
fn.is_pre_request = True
@functools.wraps(fn)
def __inner(*args, **kwargs):
return fn(*args, **kwargs)
return __inner
def post_request(fn):
fn.is_post_request = True
@functools.wraps(fn)
def __inner(*args, **kwargs):
return fn(*args, **kwargs)
return __inner
class Api(object):
def __init__(self, base_url, request_headers={}, debug=False, pool_size=10,
connect_timeout=5, response_timeout=10):
self.base_url = base_url.rstrip("/")
self.headers = request_headers
if debug:
logging.basicConfig(level=logging.DEBUG)
self.pre_request_chain = []
self.post_request_chain = []
for name, method in inspect.getmembers(self, inspect.ismethod):
if hasattr(method, "is_pre_request"):
self.pre_request_chain.append(method)
if hasattr(method, "is_post_request"):
self.post_request_chain.append(method)
self.session = requests.Session()
adapter = requests.adapters.HTTPAdapter(pool_maxsize=pool_size,
max_retries=2)
self.session.mount("http://", adapter)
self.session.mount("https://", adapter)
self.root = Component("", self, None)
def __getattribute__(self, key):
try:
return object.__getattribute__(self, key)
except AttributeError:
return Component(key, self, None)
@pre_request
def default_pre_request(self, method, url, params, data, headers):
return url, params, data, headers
@post_request
def default_post_request(self, status, body):
return json.loads(body)
@post_request
def fallback_post_request(self, status, body):
return body
def execute_pre_request(self, method, url, params, data, headers):
for fn in self.pre_request_chain:
url, params, data, headers = fn(method, url, params, data, headers)
return url, params, data, headers
def execute_post_request(self, status, body):
last_error = None
num_errors = 0
for fn in self.post_request_chain:
try:
body = fn(status, body)
except Exception as e:
num_errors += 1
last_error = e
if num_errors == len(self.post_request_chain):
raise last_error
else:
return body