/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/smart/client.py

  • Committer: Martin Pool
  • Date: 2009-03-13 07:54:48 UTC
  • mfrom: (4144 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4189.
  • Revision ID: mbp@sourcefrog.net-20090313075448-jlz1t7baz7gzipqn
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
            self._headers = dict(headers)
38
38
 
39
39
    def _send_request(self, protocol_version, method, args, body=None,
40
 
                      readv_body=None):
 
40
                      readv_body=None, body_stream=None):
41
41
        encoder, response_handler = self._construct_protocol(
42
42
            protocol_version)
43
43
        encoder.set_headers(self._headers)
45
45
            if readv_body is not None:
46
46
                raise AssertionError(
47
47
                    "body and readv_body are mutually exclusive.")
 
48
            if body_stream is not None:
 
49
                raise AssertionError(
 
50
                    "body and body_stream are mutually exclusive.")
48
51
            encoder.call_with_body_bytes((method, ) + args, body)
49
52
        elif readv_body is not None:
50
 
            encoder.call_with_body_readv_array((method, ) + args,
51
 
                    readv_body)
 
53
            if body_stream is not None:
 
54
                raise AssertionError(
 
55
                    "readv_body and body_stream are mutually exclusive.")
 
56
            encoder.call_with_body_readv_array((method, ) + args, readv_body)
 
57
        elif body_stream is not None:
 
58
            encoder.call_with_body_stream((method, ) + args, body_stream)
52
59
        else:
53
60
            encoder.call(method, *args)
54
61
        return response_handler
59
66
        params = CallHookParams(method, args, body, readv_body, self._medium)
60
67
        for hook in _SmartClient.hooks['call']:
61
68
            hook(params)
62
 
            
 
69
 
63
70
    def _call_and_read_response(self, method, args, body=None, readv_body=None,
64
 
            expect_response_body=True):
 
71
            body_stream=None, expect_response_body=True):
65
72
        self._run_call_hooks(method, args, body, readv_body)
66
73
        if self._medium._protocol_version is not None:
67
74
            response_handler = self._send_request(
68
75
                self._medium._protocol_version, method, args, body=body,
69
 
                readv_body=readv_body)
 
76
                readv_body=readv_body, body_stream=body_stream)
70
77
            return (response_handler.read_response_tuple(
71
78
                        expect_body=expect_response_body),
72
79
                    response_handler)
77
84
                    self._medium._remember_remote_is_before((1, 6))
78
85
                response_handler = self._send_request(
79
86
                    protocol_version, method, args, body=body,
80
 
                    readv_body=readv_body)
 
87
                    readv_body=readv_body, body_stream=body_stream)
81
88
                try:
82
89
                    response_tuple = response_handler.read_response_tuple(
83
90
                        expect_body=expect_response_body)
125
132
 
126
133
    def call_expecting_body(self, method, *args):
127
134
        """Call a method and return the result and the protocol object.
128
 
        
 
135
 
129
136
        The body can be read like so::
130
137
 
131
138
            result, smart_protocol = smart_client.call_expecting_body(...)
165
172
                args[0], args[1:], readv_body=body, expect_response_body=True)
166
173
        return (response, response_handler)
167
174
 
 
175
    def call_with_body_stream(self, args, stream):
 
176
        response, response_handler = self._call_and_read_response(
 
177
                args[0], args[1:], body_stream=stream,
 
178
                expect_response_body=False)
 
179
        return (response, response_handler)
 
180
 
168
181
    def remote_path_from_transport(self, transport):
169
182
        """Convert transport into a path suitable for using in a request.
170
 
        
 
183
 
171
184
        Note that the resulting remote path doesn't encode the host name or
172
185
        anything but path, so it is only safe to use it in requests sent over
173
186
        the medium from the matching transport.
179
192
 
180
193
    def __init__(self):
181
194
        hooks.Hooks.__init__(self)
182
 
        self['call'] = []
183
 
 
184
 
        
 
195
        self.create_hook(hooks.HookPoint('call',
 
196
            "Called when the smart client is submitting a request to the "
 
197
            "smart server. Called with a bzrlib.smart.client.CallHookParams "
 
198
            "object. Streaming request bodies, and responses, are not "
 
199
            "accessible.", None, None))
 
200
 
 
201
 
185
202
_SmartClient.hooks = SmartClientHooks()
186
203
 
187
204
 
188
205
class CallHookParams(object):
189
 
    
 
206
 
190
207
    def __init__(self, method, args, body, readv_body, medium):
191
208
        self.method = method
192
209
        self.args = args