Пример #1
0
def main():
    from Common.config import TopHatConfig

    # setup the config
    TopHatConfig(path="/home/specialk/Dev/tophat/config.py")

    from Networking.statuscodes import StatusCodes
    from Request.requestcontroller import RequestController
    from Request.response import Response
    from Request.requesterrors import RequestError

    opcode = 1  # post
    uri = "/players/"
    apikey = "73640de25b7d656733ce2f808a330f18cfc26385c2003a3674b57cab555aa1b2"

    data = {}
    data["name"] = "The Mechanic"
    data["game"] = {}
    data["game"]["id"] = 5
    data["qrcode"] = "G12345"

    rc = RequestController(opcode, uri, apikey, data)
    rc.run()

    print rc.response.code

    import pprint

    pprint.pprint(rc.response.data)
Пример #2
0
    def handleIt(self, opcode, uri, key, data):

        response = None

        try:
            if opcode is 0:
                RC = RequestController(opcode, uri, key, None)
            elif opcode is 1:
                RC = RequestController(opcode, uri, key,
                                       JsonParser.getObject(data))
            elif opcode is 2:
                RC = RequestController(opcode, uri, key,
                                       JsonParser.getObject(data))
            elif opcode is 3:
                RC = RequestController(opcode, uri, key, None)

            RC.run()

            if RC.response is not None:
                response = RC.response
            else:
                response = Response(
                    "No data returned from requests controller.",
                    StatusCodes.SERVER_ERROR)
        except RequestError as e:
            # Handles Errors raised in requests. Errors contain a message and errorcode.
            response = Response(e.message, e.code)
        except ValueError:
            traceback.print_exc(file=sys.stdout)
            response = Response("JSON Data was invalid.",
                                StatusCodes.BAD_REQUEST)
        except:
            # Return Server error message with the stacktrace
            response = Response(traceback.format_exc(),
                                StatusCodes.SERVER_ERROR)

        if response.code is StatusCodes.OK or response.code is StatusCodes.CREATED:
            response.json = JsonEncoder.toJson(response.data)
        else:
            response.json = JsonEncoder.toJson({
                "error_code": response.code,
                "error_message": response.data
            })

        return response
	def handleIt(self, opcode, uri, key, data, depth=0):
	
		response = None
		#print data
		try:
			# Max Depth is 5 as mission control requires this level of depth.
			if depth > 5:
				raise AssertionError("Depth cannot be greater than 5")

			if opcode is 0:
				RC = RequestController(opcode, uri, key, None, depth)
			elif opcode is 1:
				RC = RequestController(opcode, uri, key, JsonParser.getObject(data), depth)
			elif opcode is 2:
				RC = RequestController(opcode, uri, key, JsonParser.getObject(data), depth)
			elif opcode is 3:
				RC = RequestController(opcode, uri, key, None, depth)

			RC.run()

			if RC.response is not None:
				response = RC.response
			else:
				response = Response("No data returned from requests controller.", StatusCodes.SERVER_ERROR)

		except RequestError as e:
			# Handles Errors raised in requests. Errors contain a message and errorcode.
			traceback.print_exc()
			response = Response(e.message, e.code)

		except ValueError as e:
			traceback.print_exc()
			response = Response("JSON Data was invalid.", StatusCodes.BAD_REQUEST)

		except Exception as e:
			# Return Server error message with the stacktrace
			traceback.print_exc()
			response = Response(e.message, StatusCodes.SERVER_ERROR)

		if response.code is StatusCodes.OK or response.code is StatusCodes.CREATED:
			response.json = JsonEncoder.toJson(response.data)
		else:
			response.json = JsonEncoder.toJson({"error_code": response.code, "error_message": response.data})

		return response
Пример #4
0
	def handleIt(self, opcode, uri, key, data):

		response = None

		try:
			if opcode is 0:
				RC = RequestController(opcode, uri, key, None)
			elif opcode is 1:
				RC = RequestController(opcode, uri, key, JsonParser.getObject(data))
			elif opcode is 2:
				RC = RequestController(opcode, uri, key, JsonParser.getObject(data))
			elif opcode is 3:
				RC = RequestController(opcode, uri, key, None)

			RC.run()

			if RC.response is not None:
				response = RC.response
			else:
				response = Response("No data returned from requests controller.", StatusCodes.SERVER_ERROR)
		except RequestError as e:
			# Handles Errors raised in requests. Errors contain a message and errorcode.
			response = Response(e.message, e.code)
		except ValueError:
			traceback.print_exc(file=sys.stdout)
			response = Response("JSON Data was invalid.", StatusCodes.BAD_REQUEST)
		except:
			# Return Server error message with the stacktrace
			response = Response(traceback.format_exc(), StatusCodes.SERVER_ERROR)

		if response.code is StatusCodes.OK or response.code is StatusCodes.CREATED:
			response.json = JsonEncoder.toJson(response.data)
		else:
			response.json = JsonEncoder.toJson({"error_code": response.code, "error_message": response.data})

		return response