简体   繁体   English

Flask:如何从使用@ app.route装饰器的函数中获取返回值?

[英]Flask: How do I get the returned value from a function that uses @app.route decorator?

So I'm pretty new to Flask and I'm trying to make my mind around one thing. 所以我对Flask很新,我试图让我的想法绕过一件事。 So, if I understand well when you write a function within a Flask app and you use the @app.route decorator in that function, it only runs when you hit that path/url. 所以,如果我在Flask应用程序中编写函数并且在该函数中使用@ app.route装饰器时我理解得很好,那么它只会在您触发该路径/ url时运行。

I have a small oauth app written in Flask that goes through all the authorization flow and then it return the token. 我有一个用Flask编写的小型oauth应用程序,它遍历所有授权流程,然后返回令牌。

My question is how do I get that token from the @decorated function? 我的问题是如何从@decorated函数中获取该令牌? For example, lets say I have something like this: 例如,假设我有这样的事情:

@app.route(/token/)
def getToken(code): #code from the callback url.
    #/Stuff to get the Token/
    #/**********************/
    return token

If I hit the (/token/) url-path the function returns the token. 如果我点击(/ token /)url-path,该函数将返回令牌。 But now I need to get that token and use it in another function to write and read from the API I just got the token from. 但是现在我需要获取该令牌并在另一个函数中使用它来编写和读取我刚刚获得令牌的API。 My initial thought was doing this: 我最初的想法是这样做:

token = getToken(code)

But if I do that, I get this error: 但如果我这样做,我会收到此错误:

RuntimeError: working outside of request context

So again, my question is, how do I get the token so I can pass it as a parameter to other functions. 所以,我的问题是,如何获取令牌,以便将其作为参数传递给其他函数。

Extract the token generation code into a separate function, so that you can call it from anywhere, including the view function. 将令牌生成代码提取到单独的函数中,以便您可以从任何地方调用它,包括视图函数。 It's a good practice to keep the application logic away from the view, and it also helps with unit testing. 将应用程序逻辑远离视图是一种很好的做法,它也有助于单元测试。

I assume your route includes a placeholder for code, which you skipped: 我假设您的路由包含代码的占位符,您跳过了:

def generateToken(code):
    #/Stuff to get the Token/
    #/**********************/
    return token

@app.route('/token/<string:code>')
def getToken(code):
    return generateToken(code)

Just keep in mind that generateToken shouldn't depend on the request object. 请记住, generateToken不应该依赖于request对象。 If you need any request data (eg HTTP header), you should pass it explicitly in arguments. 如果您需要任何请求数据(例如HTTP标头),则应在参数中显式传递。 Otherwise you will get the "working outside of request context" exception you mentioned. 否则,您将获得您提到的“在请求上下文之外工作”异常。

It is possible to call request-dependent views directly, but you need to mock the request object, which is a bit tricky. 可以直接调用依赖于请求的视图,但是您需要模拟请求对象,这有点棘手。 Read the request context documentation to learn more. 阅读请求上下文文档以了解更多信息。

not sure what the context is. 不确定上下文是什么。 You could just call the method. 你可以调用这个方法。

from yourmodule import get_token

def yourmethod():
  token = get_token()

Otherwise, you could use the requests library in order to retrieve the data from the route 否则,您可以使用requests库以从路径中检索数据

>>> import requests
>>> response = requests.get('www.yoursite.com/yourroute/')
>>> print response.text

If you're looking for unittests, Flask comes with a mock client 如果你正在寻找单元测试,Flask带有一个模拟客户端

def test_get_token():
  resp = self.app.get('/yourroute')
  # do something with resp.data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM