The document discusses using aspect oriented programming (AOP) in Python to design APIs. It describes how AOP can help separate concerns like security, logging, and serialization into distinct aspects to avoid scattering code across multiple functions. Decorators are proposed as a way to implement aspects for a bioenergy application API. Specific decorator aspects are presented for security, statistics, serialization, and dispatching API calls to core functions. The implementation applies the aspects as decorators to API functions to cleanly separate the concerns.
3. Case
● BioBench:
○ Application for biogas installations
● Enter measurements
● Normalize data
● Calculate plant performance
4. Problem description
● Large code-base
● A lot of calculations
● Django web-interface
● Make calculations available via an API
5. Problem description
● Is there a library/framework?
● Are the alternatives?
● What is the actual impact of the feature?
● What aspects to take into account?
26. Aspect Oriented Programming
● Aspect:
○ Pointcuts
○ Join points
○ Advices
■ Before advices
■ After advices
■ Around advices
27. Aspect Oriented Programming
● Before advice
○ Must execute the function (no side-effects)
def aspect(function):
def advice(*args, **kwargs):
do something here
return function(*args, **kwargs)
return advice
28. Aspect Oriented Programming
● After advice
○ Must execute the function (no side-effects)
def aspect(function):
def advice(*args, **kwargs):
result = function(*args, **kwargs)
do something here
return result
return advice
29. Aspect Oriented Programming
● Around advice
○ Allowed to bypass the function
def aspect(function):
def advice(*args, **kwargs):
do something here
result = function(*args, **kwargs)
do something here
return result
return advice
32. Implementation
● Security aspect
○ Around advice
def secure(function):
def advice(*args, **kwargs):
if valid token in request object:
return function(*args, **kwargs)
raise Exception('No valid token provided')
return advice
33. Implementation
● Statistics aspect
○ Before advice
def statistics(function):
def advice(*args, **kwargs):
increase API usage count for the user logged in
return function(*args, **kwargs)
return advice
34. Implementation
● Serialization aspect
○ Around advice
def serialize(function):
def advice(format, *args, **kwargs):
if not format in ['html', 'xml', 'json']:
raise exception
result = function(*args, **kwargs)
make a http response of 'result' in the right format
return advice
35. Implementation
● Dispatcher aspect
○ Around advice
def dispatch(function):
def advice(*args, **kwargs):
proxy the API call to a call to the core system
return advice
40. Conclusion
● AOP offers some brilliant concepts in
software engineering
● Separate your concerns / aspects
○ Avoid classical scattering and tangling