samedi 3 août 2019

Design & Testing of common flask endpoint code

i'm trying to refactor some of my code that handles endpoint and i struggle to get a good design (that will be easy to test also).

lets say i have this endpoint base class:

class BaseResource(flask.Resource):
def dispatch_request(self, *args, **kwargs):
    try:
        with memory_logged_scope(), time_logged_scope():
            return super(BaseResource, self).dispatch_request(*args, **kwargs)
    except Exception as e:
        handle_endpoint_error(e, self.__class__.__name__)

which give the functionality of log the memory and the time.

Some derived class will be:

class DerivedResource(BaseResource):
    def post(self):
        do_some_logic...

In addition, i have some code which "wrap" (dont know if that the exact word) an endpoint, lets say a file has been sent, so that code does something like that:

file_sent = request.files[0]
temp_file = write_to_temp_file(file_sent)
try:
    call somehow to the endpoint logic
finally:
    close(file_sent, temp_file)

Like this function i have some more "wrappers". I dont want to write explicit this code on the endpoint code so i can test it easily.

I saw @app.before_request, but this will occur to all of the request and i can write it on one request to "whitelist it".

I'll be glad for any design recommendations to this situation, thank you!

Aucun commentaire:

Enregistrer un commentaire