Чтобы построить ответ tbicr's , вот пример, переопределяющий пример метода register
:
from flask import Blueprint
auth = None
class RegisteringExampleBlueprint(Blueprint):
def register(self, app, options, first_registration=False):
global auth
config = app.config
client_id = config.get('CLIENT_ID')
client_secret = config.get('CLIENT_SECRET')
scope = config.get('SCOPE')
callback = config.get('CALLBACK')
auth = OauthAdapter(client_id, client_secret, scope, callback)
super(RegisteringExampleBlueprint,
self).register(app, options, first_registration)
the_blueprint = RegisteringExampleBlueprint('example', __name__)
И пример с использованием record
декоратор :
from flask import Blueprint
from api import api_blueprint as api
auth = None
# Note there's also a record_once decorator
@api.record
def record_auth(setup_state):
global auth
config = setup_state.app.config
client_id = config.get('CLIENT_ID')
client_secret = config.get('CLIENT_SECRET')
scope = config.get('SCOPE')
callback = config.get('CALLBACK')
auth = OauthAdapter(client_id, client_secret, scope, callback)
Свойство «Значение» в OkObjectResult возвращает объект. Вы можете изменить свойства объекта или даже заменить его новым. Надеюсь, что это работает для вас. Если это так, отметьте это как ответ.
Пример кода :
public class Student
{
public string Name { get; set; }
}
[ApiController]
public class TestController : ControllerBase
{
[HttpGet, Route("api/Test/GetString")]
[SampleActionFilter]
public ActionResult<Student> GetString(string name)
{
if(name.StartsWith("s"))
{
return Ok(new Student{ Name = $"This is data {name}" });
}
else
{
return Ok(new Student { Name = $"No Name" });
}
}
}
public class SampleActionFilterAttribute : TypeFilterAttribute
{
public SampleActionFilterAttribute() :
base(typeof(SampleActionFilterImpl))
{
}
private class SampleActionFilterImpl : IActionFilter
{
public void OnActionExecuting(ActionExecutingContext context)
{
// perform some business logic work
}
public void OnActionExecuted(ActionExecutedContext context)
{
// perform some business logic work
var myResult = (OkObjectResult)context.Result;
//Add type checking here... sample code only
//Modiy object values
try
{
Student myVal = (Student)myResult.Value;
myVal.Name = "Johnny";
}
catch { }
}
}
}