Skip to content

Commit

Permalink
Use a response callback instead
Browse files Browse the repository at this point in the history
  • Loading branch information
di committed Apr 24, 2018
1 parent d59763f commit 31ad71b
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 88 deletions.
60 changes: 0 additions & 60 deletions tests/unit/legacy/api/xmlrpc/test_init.py

This file was deleted.

38 changes: 35 additions & 3 deletions tests/unit/legacy/api/xmlrpc/test_xmlrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,8 +812,18 @@ def test_multicall(self, monkeypatch):
body = pretend.stub()
response = pretend.stub(body=body)

invoke_subrequest = pretend.call_recorder(lambda *a, **kw: response)
request = pretend.stub(invoke_subrequest=invoke_subrequest)
request = pretend.stub(
invoke_subrequest=pretend.call_recorder(lambda *a, **kw: response),
add_response_callback=pretend.call_recorder(
lambda *a, **kw: response),
)

callback = pretend.stub()
monkeypatch.setattr(
xmlrpc,
'response_callback',
pretend.call_recorder(lambda metric_name: callback)
)

args = [
{'methodName': 'search', 'params': [{'name': 'foo'}]},
Expand All @@ -827,10 +837,13 @@ def test_multicall(self, monkeypatch):
pretend.call('/RPC2', headers={'Content-Type': 'text/xml'}),
pretend.call('/RPC2', headers={'Content-Type': 'text/xml'}),
]
assert invoke_subrequest.calls == [
assert request.invoke_subrequest.calls == [
pretend.call(subreq, use_tweens=True),
pretend.call(subreq, use_tweens=True),
]
assert request.add_response_callback.calls == [
pretend.call(callback),
]
assert dumps.calls == [
pretend.call(({'name': 'foo'},), methodname='search'),
pretend.call(({'classifiers': ['bar']},), methodname='browse'),
Expand Down Expand Up @@ -869,3 +882,22 @@ def test_too_many_multicalls_method(self):
assert exc.value.faultString == (
'ValueError: Multicall limit is 20 calls'
)

def test_response_callback(self):
metric_name = 'some_metric_name'
callback = xmlrpc.response_callback(metric_name)

request = pretend.stub(
registry=pretend.stub(
datadog=pretend.stub(
histogram=pretend.call_recorder(lambda *a: None)
)
)
)
response = pretend.stub(content_length=pretend.stub())

callback(request, response)

assert request.registry.datadog.histogram.calls == [
pretend.call(metric_name, response.content_length),
]
1 change: 0 additions & 1 deletion tests/unit/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,6 @@ def __init__(self):
pretend.call("pyramid_retry"),
pretend.call("pyramid_tm"),
pretend.call("pyramid_services"),
pretend.call(".legacy.api.xmlrpc"),
pretend.call(".legacy.api.xmlrpc.cache"),
pretend.call("pyramid_rpc.xmlrpc"),
pretend.call(".legacy.action_routing"),
Expand Down
3 changes: 0 additions & 3 deletions warehouse/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,6 @@ def configure(settings=None):
# Register support for services
config.include("pyramid_services")

# Register our XMLRPC API
config.include(".legacy.api.xmlrpc")

# Register our XMLRPC cache
config.include(".legacy.api.xmlrpc.cache")

Expand Down
17 changes: 0 additions & 17 deletions warehouse/legacy/api/xmlrpc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from pyramid.events import NewResponse

import warehouse.legacy.api.xmlrpc.views # noqa


def on_new_response(new_response_event):
request = new_response_event.request
if not hasattr(request, 'content_length_metric_name'):
return

request.registry.datadog.histogram(
request.content_length_metric_name,
new_response_event.response.content_length
)


def includeme(config):
config.add_subscriber(on_new_response, NewResponse)
19 changes: 15 additions & 4 deletions warehouse/legacy/api/xmlrpc/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,12 +448,18 @@ def browse(request, classifiers):
return [(r.name, r.version) for r in releases]


def response_callback(metric_name):

def _callback(request, response):
request.registry.datadog.histogram(
metric_name, response.content_length
)

return _callback


@xmlrpc_method(method='system.multicall')
def multicall(request, args):
request.content_length_metric_name = (
'warehouse.xmlrpc.system.multicall.content_length'
)

if any(arg.get('methodName') == 'system.multicall' for arg in args):
raise XMLRPCWrappedError(
ValueError('Cannot use system.multicall inside a multicall')
Expand All @@ -478,4 +484,9 @@ def multicall(request, args):
).encode()
response = request.invoke_subrequest(subreq, use_tweens=True)
responses.append(xmlrpc.client.loads(response.body))

request.add_response_callback(
response_callback('warehouse.xmlrpc.system.multicall.content_length')
)

return responses

0 comments on commit 31ad71b

Please sign in to comment.