Пример #1
0
 def __init__(self, serverURL, dbname, username, pwd):
     self.serverURL = serverURL
     self.dbname = dbname
     self.username = username
     self.pwd = pwd
     self.server = Server(self.serverURL, filters=[BasicAuth(self.username, self.pwd)])
     set_logging("info")  # suppress DEBUG output of the couchdbkit/restkit
Пример #2
0
 def __init__(self, serverURL, dbname, username, pwd):
     self.serverURL = serverURL
     self.dbname = dbname
     self.username = username
     self.pwd = pwd
     self.server = Server(self.serverURL,
                          filters=[BasicAuth(self.username, self.pwd)])
     set_logging('info')  # suppress DEBUG output of the couchdbkit/restkit
Пример #3
0
def _dispatch(args):
    conf = Config()

    # update commands
    for mod in conf.extensions:
        cmdtable = getattr(mod, 'cmdtable', {})
        commands.table.update(cmdtable)
        
    cmd, globalopts, opts, args = _parse(args)
        
    if globalopts["help"]:
        del globalopts["help"]
        return commands.usage(conf, *args, **globalopts)
    elif globalopts["version"]:
        del globalopts["version"]
        return commands.version(conf, *args, **globalopts)
    
    verbose = 2
    if globalopts["debug"]:
        verbose = 1
        import restkit
        restkit.set_logging("debug")
    elif globalopts["verbose"]:
        verbose = 1
    elif globalopts["quiet"]:
        verbose = 0
        
    set_logging_level(verbose)
    if cmd is None:
        raise CommandLineError("unknown command")

    fun = commands.table[cmd][0]
    if cmd in commands.incouchapp:
         return fun(conf, conf.app_dir, *args, **opts)    
    
    return fun(conf, *args, **opts)    
Пример #4
0
def _dispatch(args):
    conf = Config()

    # update commands
    for mod in conf.extensions:
        cmdtable = getattr(mod, 'cmdtable', {})
        commands.table.update(cmdtable)

    cmd, globalopts, opts, args = _parse(args)

    if globalopts["help"]:
        del globalopts["help"]
        return commands.usage(conf, *args, **globalopts)
    elif globalopts["version"]:
        del globalopts["version"]
        return commands.version(conf, *args, **globalopts)

    verbose = 2
    if globalopts["debug"]:
        verbose = 1
        import restkit
        restkit.set_logging("debug")
    elif globalopts["verbose"]:
        verbose = 1
    elif globalopts["quiet"]:
        verbose = 0

    set_logging_level(verbose)
    if cmd is None:
        raise CommandLineError("unknown command")

    fun = commands.table[cmd][0]
    if cmd in commands.incouchapp:
        return fun(conf, conf.app_dir, *args, **opts)

    return fun(conf, *args, **opts)
Пример #5
0
def main():
    """ function to manage restkit command line """
    parser = op.OptionParser(usage=__usage__, option_list=options(),
                    version="%prog " + __version__)

    opts, args = parser.parse_args()
    args_len = len(args)

    if opts.shell:
        try:
            from restkit.contrib import ipython_shell as shell
            shell.main(options=opts, *args)
        except Exception as e:
            print(str(e), file=sys.stderr)
            sys.exit(1)
        return

    if args_len < 1:
        return parser.error('incorrect number of arguments')

    if opts.log_level is not None:
        set_logging(opts.log_level)

    body = None
    headers = []
    if opts.input:
        if opts.input == '-':
            body = sys.stdin.read()
            headers.append(("Content-Length", str(len(body))))
        else:
            fname = os.path.normpath(os.path.join(os.getcwd(),opts.input))
            body = open(fname, 'r')
    
    if opts.headers:
        for header in opts.headers:
            try:
                k, v = header.split(':')
                headers.append((k, v))
            except ValueError:
                pass


    try:
        if len(args) == 2:
            if args[1] == "-" and not opts.input:
                body = sys.stdin.read()
                headers.append(("Content-Length", str(len(body))))

        if not opts.method and opts.input:
            method = 'POST'
        else:
            method=opts.method.upper()
            
        resp = request(args[0], method=method, body=body,
                    headers=headers, follow_redirect=opts.follow_redirect)
                        
        if opts.output and opts.output != '-':
            with open(opts.output, 'wb') as f:
                if opts.server_response:
                    f.write("Server response from %s:\n" % resp.final_url)
                    for k, v in resp.headerslist:
                        f.write( "%s: %s" % (k, v))
                else:
                    with resp.body_stream() as body:
                        for block in body:
                            f.write(block)
        else:
            if opts.server_response:
                if opts.prettify:
                    print("\n\033[0m\033[95mServer response from %s:\n\033[0m" % (
                                                                    resp.final_url))
                    for k, v in resp.headerslist:
                        print("\033[94m%s\033[0m: %s" % (k, v))
                    print("\033[0m")
                else:
                    print("Server response from %s:\n" % (resp.final_url))
                    for k, v in resp.headerslist:
                        print("%s: %s" % (k, v))
                    print("")

                if opts.output == '-':
                    if opts.prettify:
                        print(prettify(resp))
                    else:
                        print(resp.body_string())
            else:
                if opts.prettify:
                    print(prettify(resp))
                else:
                    print(resp.body_string())
        
    except Exception as e:
        sys.stderr.write("An error happened: %s" % str(e))
        sys.stderr.flush()
        sys.exit(1)

    sys.exit(0)
Пример #6
0
# -*- coding: utf-8 -
#
# This file is part of restkit released under the MIT license.
# See the NOTICE for more information.

from six.moves.urllib import parse as urlparse

from webob import Request
from restkit.contrib.wsgi_proxy import HostProxy

import restkit
from restkit.conn import Connection
from socketpool import ConnectionPool

restkit.set_logging("debug")

pool = ConnectionPool(factory=Connection, max_size=10, backend="thread")
proxy = HostProxy("http://127.0.0.1:5984", pool=pool)


def application(environ, start_response):
    req = Request(environ)
    if 'RAW_URI' in req.environ:
        # gunicorn so we use real path non encoded
        u = urlparse.urlparse(req.environ['RAW_URI'])
        req.environ['PATH_INFO'] = u.path

    # do smth like adding oauth headers ..
    resp = req.get_response(proxy)

    # rewrite response
Пример #7
0
    args_len = len(args)

    if opts.shell:
        try:
            from restkit.contrib import ipython_shell as shell
            shell.main(options=opts, *args)
        except Exception, e:
            print >> sys.stderr, str(e)
            sys.exit(1)
        return

    if args_len < 1:
        return parser.error('incorrect number of arguments')

    if opts.log_level is not None:
        set_logging(opts.log_level)

    body = None
    headers = []
    if opts.input:
        if opts.input == '-':
            body = sys.stdin.read()
            headers.append(("Content-Length", str(len(body))))
        else:
            fname = os.path.normpath(os.path.join(os.getcwd(), opts.input))
            body = open(fname, 'r')

    if opts.headers:
        for header in opts.headers:
            try:
                k, v = header.split(':')
Пример #8
0
    opts, args = parser.parse_args()
    args_len = len(args)

    if opts.shell:
        try:
            from restkit.contrib import ipython_shell as shell
            shell.main(options=opts, *args)
        except Exception, e:
            print >>sys.stderr, str(e)
            sys.exit(1)
        return

    if args_len < 1:
        return parser.error('incorrect number of arguments')

    set_logging(opts.log_level)

    body = None
    headers = []
    if opts.input:
        if opts.input == '-':
            body = sys.stdin.read()
            headers.append(("Content-Length", str(len(body))))
        else:
            fname = os.path.normpath(os.path.join(os.getcwd(),opts.input))
            body = open(fname, 'r')
    
    if opts.headers:
        for header in opts.headers:
            try:
                k, v = header.split(':')
Пример #9
0
def main():
    """ function to manage restkit command line """
    parser = op.OptionParser(usage=__usage__, option_list=options(),
                             version="%prog " + __version__)

    opts, args = parser.parse_args()
    args_len = len(args)

    if opts.shell:
        try:
            from restkit.contrib import ipython_shell as shell
            shell.main(options=opts, *args)
        except Exception as e:
            print(str(e), file=sys.stderr)
            sys.exit(1)
        return

    if args_len < 1:
        return parser.error('incorrect number of arguments')

    if opts.log_level is not None:
        set_logging(opts.log_level)

    body = None
    headers = []
    if opts.input:
        if opts.input == '-':
            body = sys.stdin.read()
            headers.append(("Content-Length", str(len(body))))
        else:
            fname = os.path.normpath(os.path.join(os.getcwd(), opts.input))
            body = open(fname, 'r')

    if opts.headers:
        for header in opts.headers:
            try:
                k, v = header.split(':')
                headers.append((k, v))
            except ValueError:
                pass

    try:
        if len(args) == 2:
            if args[1] == "-" and not opts.input:
                body = sys.stdin.read()
                headers.append(("Content-Length", str(len(body))))

        if not opts.method and opts.input:
            method = 'POST'
        else:
            method = opts.method.upper()

        resp = request(args[0], method=method, body=body,
                       headers=headers, follow_redirect=opts.follow_redirect)

        if opts.output and opts.output != '-':
            with open(opts.output, 'wb') as f:
                if opts.server_response:
                    f.write("Server response from %s:\n" % resp.final_url)
                    for k, v in resp.headerslist:
                        f.write("%s: %s" % (k, v))
                else:
                    with resp.body_stream() as body:
                        for block in body:
                            f.write(block)
        else:
            if opts.server_response:
                if opts.prettify:
                    print("\n\033[0m\033[95mServer response from %s:\n\033[0m" % (
                        resp.final_url))
                    for k, v in resp.headerslist:
                        print("\033[94m%s\033[0m: %s" % (k, v))
                    print("\033[0m")
                else:
                    print("Server response from %s:\n" % (resp.final_url))
                    for k, v in resp.headerslist:
                        print("%s: %s" % (k, v))
                    print("")

                if opts.output == '-':
                    if opts.prettify:
                        print(prettify(resp))
                    else:
                        print(resp.body_string())
            else:
                if opts.prettify:
                    print(prettify(resp))
                else:
                    print(resp.body_string())

    except Exception as e:
        sys.stderr.write("An error happened: %s" % str(e))
        sys.stderr.flush()
        sys.exit(1)

    sys.exit(0)
Пример #10
0
from __future__ import with_statement
import uuid

from django.conf import settings
from django.views.decorators.csrf import csrf_exempt
from django.core.servers.basehttp import is_hop_by_hop
from django.http import HttpResponse, Http404, HttpResponsePermanentRedirect
import restkit
from restkit.globals import set_manager
from restkit.manager import Manager
from utils.utils import *
import sys

#'critical': 50,'error': 40, 'warning': 30, 'info': 20, 'debug': 10
restkit.set_logging("info")

from .util import absolute_uri, header_name, coerce_put_post, \
rewrite_location, import_conn_manager, absolute_uri
from .filters import RewriteBase

_conn_manager = None
def set_conn_manager():
    global _conn_manager

    nb_connections = getattr(settings, 'REVPROXY_NB_CONNECTIONS', 10)
    timeout = getattr(settings, 'REVPROXY_TIMEOUT', 150)

    
    conn_manager_uri = getattr(settings, 'REVPROXY_CONN_MGR', None)
    if not conn_manager_uri:
Пример #11
0
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
################################################################################

from restkit import Resource, BasicAuth, set_logging

from django.conf import settings

#set_logging("debug")
set_logging("error")
#set_logging("info")


class Connection(Resource):
    # def __init__(self,  url, pool_instance=None, **kwargs):
    def __init__(self, url, **kwargs):
        super(Connection, self).__init__(
            url,
            follow_redirect=True,
            max_follow_redirect=10,
            # pool_instance=pool_instance,
            timeout=settings.CONNECTION_TIMEOUT,
            **kwargs)

    def post(self,
Пример #12
0
from heymoose.utils.workers import app_logger, app_error
from restkit import request, set_logging
from restkit import Resource
from restkit import forms
from functools import  partial
from restkit.errors import RequestFailed, RequestError
from heymoose import config
import sys
from lxml import etree

URL_BASE = config.get('RESTAPI_SERVER')
TIMEOUT = config.get('RESTKIT_TIMEOUT', 5)
MAX_TRIES = config.get('RESTKIT_MAX_TRIES', 5)
LOG_LEVEL = config.get('RESTKIT_LOG_LEVEL', 'info')
TEST_PORT = 9345
set_logging(LOG_LEVEL)

def create_resource(base=URL_BASE):
	return Resource(base, timeout=TIMEOUT, max_tries=MAX_TRIES)

def exec_request(http_call):
	try:
		return http_call()
	except (RequestFailed, RequestError) as inst:
		# We need to know url for debug info, but the system has broken, so raise it to the higher level
		if getattr(inst, 'response', False):
			app_logger.error(inst.response.final_url, exc_info=True)
		raise 

def get(path, base=URL_BASE, params_dict={}, renderer=etree.fromstring):
	app_logger.debug("get: base={0} path={1} params_dict={2}".format(base, path, str(params_dict)))
Пример #13
0
from django.http import HttpResponse, Http404, HttpResponsePermanentRedirect
import restkit


_hop_headers = {
    'connection':1, 'keep-alive':1, 'proxy-authenticate':1,
    'proxy-authorization':1, 'te':1, 'trailers':1, 'transfer-encoding':1,
    'upgrade':1
}

def is_hop_by_hop(header_name):
    """Return true if 'header_name' is an HTTP/1.1 "Hop-by-Hop" header"""
    return header_name.lower() in _hop_headers

# TODO: should set this as configurable by caller
restkit.set_logging("error")

from .util import absolute_uri, header_name, coerce_put_post, \
rewrite_location, import_conn_manager, absolute_uri
from .filters import RewriteBase
from .store import RequestStore


class HttpResponseBadGateway(HttpResponse):
    status_code = 502


@csrf_exempt
def proxy_request(request, **kwargs):
    """ generic view to proxy a request.