1
# Copyright (C) 2006 Canonical Ltd
 
 
3
# This program is free software; you can redistribute it and/or modify
 
 
4
# it under the terms of the GNU General Public License as published by
 
 
5
# the Free Software Foundation; either version 2 of the License, or
 
 
6
# (at your option) any later version.
 
 
8
# This program is distributed in the hope that it will be useful,
 
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 
11
# GNU General Public License for more details.
 
 
13
# You should have received a copy of the GNU General Public License
 
 
14
# along with this program; if not, write to the Free Software
 
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 
19
from bzrlib.smart import message, protocol
 
 
20
from bzrlib import urlutils
 
 
23
class _SmartClient(object):
 
 
25
    def __init__(self, medium, base):
 
 
28
        :param medium: a SmartClientMedium
 
 
34
    def _build_client_protocol(self):
 
 
35
        version = self._medium.protocol_version()
 
 
36
        request = self._medium.get_request()
 
 
38
            request_encoder = protocol.ProtocolThreeRequester(request)
 
 
39
            response_handler = message.ConventionalResponseHandler()
 
 
40
            response_proto = protocol.ProtocolThreeDecoder(response_handler)
 
 
41
            response_handler.setProtoAndMediumRequest(response_proto, request)
 
 
43
            request_encoder = protocol.SmartClientRequestProtocolTwo(request)
 
 
44
            response_handler = request_encoder
 
 
46
            request_encoder = protocol.SmartClientRequestProtocolOne(request)
 
 
47
            response_handler = request_encoder
 
 
48
        return request_encoder, response_handler
 
 
50
    def call(self, method, *args):
 
 
51
        """Call a method on the remote server."""
 
 
52
        result, protocol = self.call_expecting_body(method, *args)
 
 
53
        protocol.cancel_read_body()
 
 
56
    def call_expecting_body(self, method, *args):
 
 
57
        """Call a method and return the result and the protocol object.
 
 
59
        The body can be read like so::
 
 
61
            result, smart_protocol = smart_client.call_expecting_body(...)
 
 
62
            body = smart_protocol.read_body_bytes()
 
 
64
        request_encoder, response_handler = self._build_client_protocol()
 
 
65
        request_encoder.call(method, *args)
 
 
66
        return (response_handler.read_response_tuple(expect_body=True),
 
 
69
    def call_with_body_bytes(self, method, args, body):
 
 
70
        """Call a method on the remote server with body bytes."""
 
 
71
        if type(method) is not str:
 
 
72
            raise TypeError('method must be a byte string, not %r' % (method,))
 
 
74
            if type(arg) is not str:
 
 
75
                raise TypeError('args must be byte strings, not %r' % (args,))
 
 
76
        if type(body) is not str:
 
 
77
            raise TypeError('body must be byte string, not %r' % (body,))
 
 
78
        request_encoder, response_handler = self._build_client_protocol()
 
 
79
        request_encoder.call_with_body_bytes((method, ) + args, body)
 
 
80
        return response_handler.read_response_tuple()
 
 
82
    def call_with_body_bytes_expecting_body(self, method, args, body):
 
 
83
        """Call a method on the remote server with body bytes."""
 
 
84
        if type(method) is not str:
 
 
85
            raise TypeError('method must be a byte string, not %r' % (method,))
 
 
87
            if type(arg) is not str:
 
 
88
                raise TypeError('args must be byte strings, not %r' % (args,))
 
 
89
        if type(body) is not str:
 
 
90
            raise TypeError('body must be byte string, not %r' % (body,))
 
 
91
        request_encoder, response_handler = self._build_client_protocol()
 
 
92
        request_encoder.call_with_body_bytes((method, ) + args, body)
 
 
93
        return (response_handler.read_response_tuple(expect_body=True),
 
 
96
    def call_with_body_readv_array(self, args, body):
 
 
97
        request_encoder, response_handler = self._build_client_protocol()
 
 
98
        request_encoder.call_with_body_readv_array(args, body)
 
 
99
        return (response_handler.read_response_tuple(expect_body=True),
 
 
102
    def remote_path_from_transport(self, transport):
 
 
103
        """Convert transport into a path suitable for using in a request.
 
 
105
        Note that the resulting remote path doesn't encode the host name or
 
 
106
        anything but path, so it is only safe to use it in requests sent over
 
 
107
        the medium from the matching transport.
 
 
110
        if (base.startswith('bzr+http://') or base.startswith('bzr+https://')
 
 
111
            or base.startswith('http://') or base.startswith('https://')):
 
 
112
            medium_base = self._base
 
 
114
            medium_base = urlutils.join(self._base, '/')
 
 
116
        rel_url = urlutils.relative_url(medium_base, transport.base)
 
 
117
        return urllib.unquote(rel_url)