[tor-commits] [oonib/master] Refactor the bouncer handler
art at torproject.org
art at torproject.org
Wed Sep 11 09:13:52 UTC 2013
commit 0089a0eda583b7b14cda255ab10b80a137380a72
Author: Arturo Filastò <art at fuffa.org>
Date: Mon Aug 19 14:03:31 2013 +0200
Refactor the bouncer handler
---
oonib/bouncer/handlers.py | 64 +++++++++++++++++++++++++++------------------
1 file changed, 39 insertions(+), 25 deletions(-)
diff --git a/oonib/bouncer/handlers.py b/oonib/bouncer/handlers.py
index 162f61d..bc81ddb 100644
--- a/oonib/bouncer/handlers.py
+++ b/oonib/bouncer/handlers.py
@@ -4,37 +4,51 @@ import yaml
from oonib.handlers import OONIBHandler
from oonib import config
-#knownHelpers = {'helper-name': ['collector-address1', 'collector-address2', ... 'collector-addressn']}
-knownHelpers = {}
+class BouncerQueryHandler(OONIBHandler):
+ def initialize(self):
+ self.knownHelpers = {}
+ self.updateKnownHelpers()
-def updateKnownHelpers():
- with open(config.main.bouncer_file) as f:
- bouncerFile = yaml.safe_load(f)
- for collectorName, helpers in bouncerFile['collector'].items():
- for helperName, helperAddress in helpers['test-helper'].items():
- if helperName not in knownHelpers.keys():
- knownHelpers[helperName] = []
-
- knownHelpers[helperName].append(
- {'collector-name': collectorName,
- 'helper-address': helperAddress
- })
-updateKnownHelpers()
+ def updateKnownHelpers(self):
+ with open(config.main.bouncer_file) as f:
+ bouncerFile = yaml.safe_load(f)
+ for collectorName, helpers in bouncerFile['collector'].items():
+ for helperName, helperAddress in helpers['test-helper'].items():
+ if helperName not in self.knownHelpers.keys():
+ self.knownHelpers[helperName] = []
+
+ self.knownHelpers[helperName].append({
+ 'collector-name': collectorName,
+ 'helper-address': helperAddress
+ })
-class BouncerQueryHandler(OONIBHandler):
def get(self):
#XXX unused
pass
def post(self):
- helpers = json.loads(self.request.body)['test-helpers']
- a = {}
- a['collector'] = {}
+ try:
+ query = json.loads(self.request.body)
+ except ValueError:
+ self.set_status(400)
+ self.write(json.dumps({'error': 'invalid-request'}))
+ return
+
+ try:
+ helpers = query['test-helpers']
+ except KeyError:
+ self.set_status(400)
+ self.write(json.dumps({'error': 'test-helpers-key-missing'}))
+ return
+
+ response = {}
+ response['collector'] = {}
for helperName in helpers:
- if helperName in knownHelpers.keys():
- chosen = random.choice(knownHelpers[helperName])
+ if helperName in self.knownHelpers.keys():
+ chosen = random.choice(self.knownHelpers[helperName])
collectorName, helperAddress = chosen['collector-name'], chosen['helper-address']
- if not collectorName in a['collector'].keys():
- a['collector'][collectorName] = {'test-helper': {}}
- a['collector'][collectorName]['test-helper'][helperName] = helperAddress
- self.write(json.dumps(a))
+ if not collectorName in response['collector'].keys():
+ response['collector'][collectorName] = {'test-helper': {}}
+ response['collector'][collectorName]['test-helper'][helperName] = helperAddress
+
+ self.write(response)
More information about the tor-commits
mailing list