def innerResourceInstanceMethod(*args,**kwargs) : resourceInstance = args[0] completeResponse = None if logRequest : log.prettyJson( resourceInstanceMethod, 'bodyRequest', json.loads(Serializer.jsonifyIt(args[1:])), condition = logRequest, logLevel = log.DEBUG ) try : FlaskManager.validateKwargs( kwargs, resourceInstance, innerResourceInstanceMethod, requestHeaderClass = requestHeaderClass, requestParamClass = requestParamClass ) FlaskManager.validateArgs(args, requestClass, innerResourceInstanceMethod) completeResponse = resourceInstanceMethod(*args,**kwargs) FlaskManager.validateResponseClass(responseClass, completeResponse) except Exception as exception : log.warning(innerResourceInstanceMethod, 'Not posssible to complete request', exception=exception) raise exception controllerResponse = completeResponse[0] if ObjectHelper.isNotNone(completeResponse[0]) else {'message' : completeResponse[1].enumName} if logResponse : log.prettyJson( resourceInstanceMethod, 'bodyResponse', json.loads(Serializer.jsonifyIt(controllerResponse)), condition = logResponse, logLevel = log.DEBUG ) return completeResponse[0]
def jsonifyIt() : assert '{"myString": "myValue", "myInteger": 0, "myFloat": 0.099}' == Serializer.jsonifyIt(MY_DICTIONARY) myGenerator = generatorFunction() assert myGenerator == Serializer.jsonifyIt(myGenerator) assert '{"myAttribute": null, "myNeutralAttribute": "someString"}' == Serializer.jsonifyIt(MyClass()) assert '{"id": null}' == Serializer.jsonifyIt(MyEntityClass()) father = Father() child = Child() brother = Brother() otherFather = Father() otherBrother = Brother() otherChild = Child() father.id = 1 child.id = 2 brother.id = 3 otherFather.id = 4 otherBrother.id = 5 otherChild.id = 6 father.childList = [child, otherChild] father.brotherList = [otherBrother] child.father = father child.fatherId = father.id child.brother = brother child.brotherId = brother.id brother.father = otherFather brother.fatherId = otherFather.id brother.child = child otherFather.childList = [] otherFather.brotherList = [brother] otherBrother.father = father otherBrother.fatherId = father.id otherBrother.child = otherChild otherChild.father = father otherChild.fatherId = father.id otherChild.brother = otherBrother otherChild.brotherId = otherBrother.id assert '{"brother": {"child": null, "father": {"brotherList": [], "childList": [], "id": 4}, "fatherId": 4, "id": 3}, "brotherId": 3, "father": {"brotherList": [{"child": {"brother": null, "brotherId": 5, "father": null, "fatherId": 1, "id": 6}, "father": null, "fatherId": 1, "id": 5}], "childList": [{"brother": {"child": null, "father": null, "fatherId": 1, "id": 5}, "brotherId": 5, "father": null, "fatherId": 1, "id": 6}], "id": 1}, "fatherId": 1, "id": 2}' == Serializer.jsonifyIt(child) assert '{"brotherList": [{"child": {"brother": null, "brotherId": 5, "father": null, "fatherId": 1, "id": 6}, "father": null, "fatherId": 1, "id": 5}], "childList": [{"brother": {"child": null, "father": {"brotherList": [], "childList": [], "id": 4}, "fatherId": 4, "id": 3}, "brotherId": 3, "father": null, "fatherId": 1, "id": 2}, {"brother": {"child": null, "father": null, "fatherId": 1, "id": 5}, "brotherId": 5, "father": null, "fatherId": 1, "id": 6}], "id": 1}' == Serializer.jsonifyIt(father) myClass = MyClass() myAttributeClass = MyAttributeClass(myClass=myClass) myClass.myAttribute = myAttributeClass assert '{"myAttribute": {"myClass": null, "myNeutralClassAttribute": "someOtherString"}, "myNeutralAttribute": "someString"}' == Serializer.jsonifyIt(myClass) assert '{"myClass": {"myAttribute": null, "myNeutralAttribute": "someString"}, "myNeutralClassAttribute": "someOtherString"}' == Serializer.jsonifyIt(myClass.myAttribute)
def innerResourceInstanceMethod(*args, **kwargs): # r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" # r.headers["Pragma"] = "no-cache" # r.headers["Expires"] = "0" # r.headers['Cache-Control'] = 'public, max-age=0' resourceInstance = args[0] completeResponse = None try: if ObjectHelper.isNotEmptyCollection(roleRequired): completeResponse = securedControllerMethod( args, kwargs, consumes, resourceInstance, resourceInstanceMethod, roleRequired, requestHeaderClass, requestParamClass, requestClass, logRequest) else: completeResponse = publicControllerMethod( args, kwargs, consumes, resourceInstance, resourceInstanceMethod, requestHeaderClass, requestParamClass, requestClass, logRequest) # print(f'completeResponse: {completeResponse}') validateResponseClass(responseClass, completeResponse) except Exception as exception: # print(exception) completeResponse = getCompleteResponseByException( exception, resourceInstance, resourceInstanceMethod) ###- request.method: GET ###- request.url: http://127.0.0.1:5000/alert/dingding/test?x=y ###- request.base_url: http://127.0.0.1:5000/alert/dingding/test ###- request.url_charset: utf-8 ###- request.url_root: http://127.0.0.1:5000/ ###- str(request.url_rule): /alert/dingding/test ###- request.host_url: http://127.0.0.1:5000/ ###- request.host: 127.0.0.1:5000 ###- request.script_root: ###- request.path: /alert/dingding/test ###- request.full_path: /alert/dingding/test?x=y ###- request.args: ImmutableMultiDict([('x', 'y')]) ###- request.args.get('x'): y controllerResponse = completeResponse[0] if ObjectHelper.isNotNone( completeResponse[0]) else { 'message': completeResponse[1].enumName } status = completeResponse[1] if logResponse: log.prettyJson(resourceInstanceMethod, 'bodyResponse', json.loads( Serializer.jsonifyIt(controllerResponse)), condition=logResponse, logLevel=log.DEBUG) return jsonifyResponse(controllerResponse, produces, status)
def convertFromObjectToObject_whenTargetClassIsList() : # arrange a = RandomHelper.string() b = RandomHelper.string() c = RandomHelper.string() otherA = MyOtherDto(RandomHelper.string()) otherB = MyOtherDto(RandomHelper.string()) otherC = MyOtherDto(RandomHelper.string()) myFirst = MyDto(RandomHelper.string(), otherA, [MyThirdDto(MyDto(RandomHelper.string(), MyOtherDto(RandomHelper.string()), [MyThirdDto(MyDto(RandomHelper.string(), MyOtherDto(RandomHelper.string()), [MyThirdDto(MyDto(RandomHelper.string(), MyOtherDto(RandomHelper.string()), [MyThirdDto(MyDto(RandomHelper.string(), MyOtherDto(RandomHelper.string()), None), RandomHelper.integer())]), RandomHelper.integer())]), RandomHelper.integer())]), RandomHelper.integer())]) mySecond = MyDto(RandomHelper.string(), otherB, [MyThirdDto(MyDto(RandomHelper.string(), MyOtherDto(RandomHelper.string()), [MyThirdDto(MyDto(RandomHelper.string(), MyOtherDto(RandomHelper.string()), [MyThirdDto(MyDto(RandomHelper.string(), MyOtherDto(RandomHelper.string()), [MyThirdDto(MyDto(RandomHelper.string(), MyOtherDto(RandomHelper.string()), None), RandomHelper.integer())]), RandomHelper.integer())]), RandomHelper.integer())]), RandomHelper.integer())]) myThird = MyDto(RandomHelper.string(), otherC, [MyThirdDto(MyDto(RandomHelper.string(), MyOtherDto(RandomHelper.string()), [MyThirdDto(MyDto(RandomHelper.string(), MyOtherDto(RandomHelper.string()), [MyThirdDto(MyDto(RandomHelper.string(), MyOtherDto(RandomHelper.string()), [MyThirdDto(MyDto(RandomHelper.string(), MyOtherDto(RandomHelper.string()), None), RandomHelper.integer())]), RandomHelper.integer())]), RandomHelper.integer())]), RandomHelper.integer())]) thirdOne = RandomHelper.integer() thirdTwo = RandomHelper.integer() thirdThree = RandomHelper.integer() myThirdOne = [MyThirdDto(myFirst, thirdOne)] myThirdTwo = [MyThirdDto(mySecond, thirdTwo)] myThirdThree = [MyThirdDto(myThird, thirdThree)] expected = [MyDto(a, otherA, myThirdOne), MyDto(b, otherB, myThirdTwo), MyDto(c, otherC, myThirdThree)] null = 'null' inspectEquals = False # act toAssert = Serializer.convertFromObjectToObject(expected, [[MyDto]]) another = Serializer.convertFromObjectToObject([MyDto(a, otherA, [MyThirdDto(myFirst, thirdOne)]), MyDto(b, otherB, myThirdTwo), MyDto(c, otherC, myThirdThree)], [[MyDto]]) another[0].myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my = MyDto( MyDto(RandomHelper.string(), None, None), expected[0].myThirdList[0].my.myOther, expected[0].myThirdList[0].my.myThirdList ) # assert assert expected == toAssert, f'{expected} == {toAssert}: {expected == toAssert}' assert ObjectHelper.equals(expected, toAssert) assert ObjectHelper.isNotNone(expected[0].myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my) assert expected[0].myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my == toAssert[0].myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my assert ObjectHelper.equals(expected[0].myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my, toAssert[0].myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my) assert ObjectHelper.isNone(expected[0].myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList) assert ObjectHelper.equals(expected[0].myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList, toAssert[0].myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList[0].my.myThirdList) assert ObjectHelper.equals(json.loads(Serializer.jsonifyIt(expected)), json.loads(Serializer.jsonifyIt(toAssert))), f'ObjectHelper.equals({json.loads(Serializer.jsonifyIt(expected))},\n\n{json.loads(Serializer.jsonifyIt(toAssert))})' assert False == (expected == another), f'False == ({expected} == {another}): False == {(expected == another)}' assert False == ObjectHelper.equals(expected, another, muteLogs=not inspectEquals) assert False == ObjectHelper.equals(another, expected, muteLogs=not inspectEquals) assert False == ObjectHelper.equals(another, toAssert, muteLogs=not inspectEquals) assert False == ObjectHelper.equals(toAssert, another, muteLogs=not inspectEquals) assert str(expected) == str(toAssert), f'str({str(expected)}) == str({str(toAssert)}): {str(expected) == str(toAssert)}'
def getOnActuatorHealth(self, dto, headers=None, params=None): # print(dto.__dict__) # print(headers.__dict__) # print(params.__dict__) import requests from python_framework.api.src.helper import Serializer response = requests.put( f'{self.url}{self.getOnActuatorHealth.url}', data=Serializer.jsonifyIt(dto), params=Serializer.getObjectAsDictionary(params), headers={ 'Content-Type': self.getOnActuatorHealth.consumes, **Serializer.getObjectAsDictionary(headers) }) # print(response.text) return TestRequestDto.TestResponseDto( status=Serializer.convertFromJsonToObject( response.json(), [[ActuatorHealthDto.ActuatorHealthResponseDto]])[0].status, first=params.first, second=params.second, firstHeader=headers.firstHeader, secondHeader=headers.secondHeader), HttpStatus.map( response.status_code)
def jsonifyResponse(response, contentType, status): return Response(Serializer.jsonifyIt(response), mimetype=contentType, status=status)
def jsonifyResponse(object, contentType, status): return Response(Serializer.jsonifyIt(object), mimetype=contentType, status=status)