~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/client.py

  • Committer: Aaron Bentley
  • Date: 2008-02-24 16:42:13 UTC
  • mfrom: (3234 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3235.
  • Revision ID: aaron@aaronbentley.com-20080224164213-eza1lzru5bwuwmmj
Merge with bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
16
16
 
 
17
import urllib
17
18
from urlparse import urlparse
18
19
 
19
20
from bzrlib.smart import protocol
20
 
from bzrlib.urlutils import unescape
 
21
from bzrlib import urlutils
21
22
 
22
23
 
23
24
class _SmartClient(object):
24
25
 
25
 
    def __init__(self, shared_medium):
26
 
        self._shared_medium = shared_medium
 
26
    def __init__(self, shared_connection):
 
27
        """Constructor.
 
28
 
 
29
        :param shared_connection: a bzrlib.transport._SharedConnection
 
30
        """
 
31
        self._shared_connection = shared_connection
27
32
 
28
33
    def get_smart_medium(self):
29
 
        return self._shared_medium.connection
 
34
        return self._shared_connection.connection
30
35
 
31
36
    def call(self, method, *args):
32
37
        """Call a method on the remote server."""
43
48
            body = smart_protocol.read_body_bytes()
44
49
        """
45
50
        request = self.get_smart_medium().get_request()
46
 
        smart_protocol = protocol.SmartClientRequestProtocolOne(request)
 
51
        smart_protocol = protocol.SmartClientRequestProtocolTwo(request)
47
52
        smart_protocol.call(method, *args)
48
53
        return smart_protocol.read_response_tuple(expect_body=True), smart_protocol
49
54
 
61
66
        smart_protocol.call_with_body_bytes((method, ) + args, body)
62
67
        return smart_protocol.read_response_tuple()
63
68
 
 
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,))
 
73
        for arg in args:
 
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
 
82
 
64
83
    def remote_path_from_transport(self, transport):
65
84
        """Convert transport into a path suitable for using in a request.
66
85
        
68
87
        anything but path, so it is only safe to use it in requests sent over
69
88
        the medium from the matching transport.
70
89
        """
71
 
        return unescape(urlparse(transport.base)[2]).encode('utf8')
 
90
        if self._shared_connection.base.startswith('bzr+http://'):
 
91
            medium_base = self._shared_connection.base
 
92
        else:
 
93
            medium_base = urlutils.join(self._shared_connection.base, '/')
 
94
            
 
95
        rel_url = urlutils.relative_url(medium_base, transport.base)
 
96
        return urllib.unquote(rel_url)