1. Lynn Rees
  2. wire

Source

wire / unfinished / curl_client.py

'''curl client wrapper'''

from subprocess import check_output

from wire.util import lazy
from wire.http.util import Extract
from wire.http.response import CoreResponse
from wire.http.request import CoreRequest, CoreREST


class _CurlBase(object):

    @lazy
    def _factory(self, url, **kw):
        cmd = ['curl', '--include']
        proxy = self._proxy
        if proxy:
            cmd.extend(
                ['--proxy', '{0}:{1}'.format(proxy['server'], proxy['host'])]
            )
        auth = self._auth
        if auth is not None:
            cmd.extend(
                ['--user', '{0}:{1}'.format(['username'], auth['password'])]
            )
        def func(urls=None, method='GET', body=None, headers=None, **kw):
            cmd = cmd
            for k, v in headers:
                cmd.extend(['--header', '"{0}: {1}"'.format(k, v)])
            for url in urls: cmd.extend([])
            cmd.extend(
                ['--url', url, '--request', method, '--data-binary',
                '"{0}"'.format(body)]
            )
            return check_output(cmd, **kw)
        return func

    @lazy
    def _response(self):
        return Response

    def _request(self, **kw):
        if self._randomua: self.headers.user_agent = self.randomua
        url = kw.get('url', self._url)
        data = kw.get('data', self.body)
        headers = kw.get('headers', dict(self.headers))
        method = kw.get('method', 'GET')
        return self._factory(
            url=url, method=method, body=data, headers=headers, **kw
        )


class Request(CoreRequest, _CurlBase):

    '''curl Request'''


class RESTClient(CoreREST, _CurlBase):

    '''curl REST client'''


class Response(CoreResponse):

    def __init__(self, new_response, **kw):
        response = Extract(new_response)
        self._original = response.rfile.read()
        self._headers = response.headers
        self._code = response['status']
        self.url = response['content-location']
        super(Response, self).__init__(**kw)