14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18
from urlparse import urlparse
19
20
from bzrlib.smart import protocol
20
from bzrlib.urlutils import unescape
21
from bzrlib import urlutils
23
24
class _SmartClient(object):
25
def __init__(self, medium):
26
def __init__(self, shared_connection):
29
:param shared_connection: a bzrlib.transport._SharedConnection
31
self._shared_connection = shared_connection
33
def get_smart_medium(self):
34
return self._shared_connection.connection
28
36
def call(self, method, *args):
29
37
"""Call a method on the remote server."""
39
47
result, smart_protocol = smart_client.call_expecting_body(...)
40
48
body = smart_protocol.read_body_bytes()
42
request = self._medium.get_request()
43
smart_protocol = protocol.SmartClientRequestProtocolOne(request)
50
request = self.get_smart_medium().get_request()
51
smart_protocol = protocol.SmartClientRequestProtocolTwo(request)
44
52
smart_protocol.call(method, *args)
45
53
return smart_protocol.read_response_tuple(expect_body=True), smart_protocol
53
61
raise TypeError('args must be byte strings, not %r' % (args,))
54
62
if type(body) is not str:
55
63
raise TypeError('body must be byte string, not %r' % (body,))
56
request = self._medium.get_request()
64
request = self.get_smart_medium().get_request()
57
65
smart_protocol = protocol.SmartClientRequestProtocolOne(request)
58
66
smart_protocol.call_with_body_bytes((method, ) + args, body)
59
67
return smart_protocol.read_response_tuple()
69
def call_with_body_bytes_expecting_body(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 = self.get_smart_medium().get_request()
79
smart_protocol = protocol.SmartClientRequestProtocolTwo(request)
80
smart_protocol.call_with_body_bytes((method, ) + args, body)
81
return smart_protocol.read_response_tuple(expect_body=True), smart_protocol
61
83
def remote_path_from_transport(self, transport):
62
84
"""Convert transport into a path suitable for using in a request.
65
87
anything but path, so it is only safe to use it in requests sent over
66
88
the medium from the matching transport.
68
return unescape(urlparse(transport.base)[2]).encode('utf8')
90
if self._shared_connection.base.startswith('bzr+http://'):
91
medium_base = self._shared_connection.base
93
medium_base = urlutils.join(self._shared_connection.base, '/')
95
rel_url = urlutils.relative_url(medium_base, transport.base)
96
return urllib.unquote(rel_url)