Skip to content

Instantly share code, notes, and snippets.

@elialbert
Created April 19, 2012 14:42
Show Gist options
  • Save elialbert/2421382 to your computer and use it in GitHub Desktop.
Save elialbert/2421382 to your computer and use it in GitHub Desktop.
python avro message receiver and parser
logging.info("INCOMING XCOM MESSAGE: %s"%message)
logging.info("INCOMING XCOM HEADERS: %s"%str(request.META))
logging.info("INCOMING XCOM REQUEST: %s"%request.POST.copy().__dict__)
logging.info("INCOMING XCOM REQUEST params: %s"%request.POST.copy())
authorization = request.META.get('HTTP_AUTHORIZATION')
if authorization != settings.XCOM_RECEIVER_FABRIC_CREDENTIALS:
return HttpResponse("Incorrect authorization!",status=403)
params = dict(request.POST.copy())
try:
params = params.keys()[0]
logging.info("extracted params as %s"%params)
except IndexError:
pass
try:
params = params.encode("utf-8")
logging.info("encoded params as %s"%params)
except Exception as exc:
logging.error("encoding failed! %s"%str(exc))
params = str(params)
pass
logging.info("params are %s"%params)
schema_header = request.META.get('HTTP_X_XC_SCHEMA_URI')
logging.info("schema header is %s"%schema_header)
resp = requests.get(schema_header)
logging.info("schema resp is %s"%resp.content)
schema_body = schema.parse(resp.content)
# this part is for parsing binary avro data
logging.info("schema body is %s"%schema_body)
reader = io.DatumReader(schema_body,schema_body)
read_io = StringIO(params)
decoder = io.BinaryDecoder(read_io)
result = reader.read(decoder)
logging.info("data result is %s"%result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment