def sendMessage(self, command, keyLength, extLength, status, opaque, cas, extra=None, body=None): bodyLength = 0 if body: bodyLength = len(body) + 4 # flags else: bodyLength = len(status['message']) log.msg('Sending message: %s' % \ status['message'] if not body else body) args = [self.HEADER_STRUCT + '%ds' % bodyLength, self.MAGIC['response'], command, keyLength, extLength, 0x00, status['code'], bodyLength, opaque, cas] if not body: args.append(status['message']) else: args += ['%s%s' % (struct.pack('!L', extra), body)] bin = struct.pack(*args) self.transport.write(bin)
def sendMessage(self, command, keyLength, extLength, status, opaque, cas, extra=None, body=None): bodyLength = 0 if body: bodyLength = len(body) + 4 # flags else: bodyLength = len(status['message']) log.msg('Sending message: %s' % \ status['message'] if not body else body) args = [ self.HEADER_STRUCT + '%ds' % bodyLength, self.MAGIC['response'], command, keyLength, extLength, 0x00, status['code'], bodyLength, opaque, cas ] if not body: args.append(status['message']) else: args += ['%s%s' % (struct.pack('!L', extra), body)] bin = struct.pack(*args) self.transport.write(bin)
def marketRisk(self, id): node = self.element_objects.get(id, None) if node: log.msg("Calculating market risk for %s" % node) self.refresh() return node.mktrisk else: return None
def marketRisk(self, id): node = self.element_objects.get(id,None) if node: log.msg("Calculating market risk for %s" % node) self.refresh() return node.mktrisk else: return None
def generate_discount_codes_view(): try: user_id = request.json['user_id'] number_of_codes = request.json['number_of_codes'] code_length = request.json['code_length'] except KeyError as e: log.msg(f'Validation Failed: {e}') abort(400) return {'message': f'Creation of {number_of_codes} codes was initiated'}, 202
def handleHeader(self, header): if len(header) != self.HEADER_SIZE: log.msg('Invalid header') return False (magic, command, keyLength, extLength, dataType, status, bodyLength, opaque, cas) = struct.unpack(self.HEADER_STRUCT, header) if magic != self.MAGIC['request']: log.msg('Invalid magic code 0x%0.2x' % magic) return False return (magic, command, keyLength, extLength, dataType, status, bodyLength, opaque, cas)
def handleCommand(self, magic, command, keyLength, extLength, dataType, status, bodyLength, opaque, cas, extra): log.msg('Trying to handle command 0x%0.2x' % command) commands = dict([(c[1]['command'], c[0]) for c in \ self.COMMANDS.items()]) if command not in commands.keys(): self.sendMessage(command, 0, 0, self.STATUSES['unknown_command'], 0, 0) return False log.msg('Handling %s' % commands[command]) commandName = 'handle%sCommand' % commands[command].capitalize() if hasattr(self, commandName): getattr(self, commandName)(magic, command, keyLength, extLength, dataType, status, bodyLength, opaque, cas, extra) return
def main(): client = EventBusClient() event_consumer = GenerateDiscountCodesEventConsumer(client=client) log.msg('Start Consuming events') event_consumer.consume()
def connectionMade(self): log.msg('Yay one client!')
from flask import Flask, request, abort from producer import generate_discount_codes_producer from logger import log app = Flask(__name__) log.msg("Server is just started") @app.route('/generate_discount_codes', methods=['POST']) def generate_discount_codes_view(): try: user_id = request.json['user_id'] number_of_codes = request.json['number_of_codes'] code_length = request.json['code_length'] except KeyError as e: log.msg(f'Validation Failed: {e}') abort(400) return {'message': f'Creation of {number_of_codes} codes was initiated'}, 202
def create_code(user_id: int, code: str): log.msg(f'Creating code {code} for user {user_id}')