简体   繁体   English

我可以将函数结果转换为python / FLASK中另一个函数的变量吗?

[英]Can I transform a function result into a variable for another function in python/FLASK?

I'm learning Python 2.7 and FLASK. 我正在学习Python 2.7和FLASK。 I have an exercise consuming the Spotify API, so with a function I get the ID of an artist. 我有一个消耗Spotify API的练习,所以使用一个函数我得到一个艺术家的ID。

Can I use the result of that function (the id) and store it as a variable so I can use it inside another API endpoint? 我可以使用该函数的结果(id)并将其存储为变量,以便我可以在另一个API端点中使用它吗? Or how can I use that result? 或者我该如何使用该结果?

Here's my code: 这是我的代码:

@app.route("/api/artist/<artist>")
def api_artist(artist):
    params = get_id(artist)
    return jsonify(params)

def get_id(artist):

    headers = {
        "client_id": "XxXXxxXXX",
        "cliente_secret": "XXXxxxxXXX"
    }

    response = requests.get("https://api.spotify.com/v1/search?q=" + artist +"&type=artist", headers=headers)


    if response.status_code == 200:
        print(response.text)
        lista=[]
        response_dict = response.json()
        results = response_dict["artists"]
        items = results ["items"]
        for value in items:
            lista.append(value["id"])




    params = {
        "id": lista[0]
    }

    return params

This is the Spotify endpoint I trying to use: https://api.spotify.com/v1/artists/{id}/top-tracks 这是我尝试使用的Spotify端点: https://api.spotify.com/v1/artists/{id}/top-tracks ://api.spotify.com/v1/artists/{id}/top-tracks

REST API should be "stateless" and hence not a good idea as pointed out by @ cricket_007 REST API应该是“无状态的”,因此不像@ cricket_007所指出的那样

So here's what you do: Make 2 REST API Calls and handle the personalization of the user artist preference on the client side 所以这就是你做的:做2个REST API调用并在客户端处理用户艺术家偏好的个性化

1.) Search the artist and get the ID and return it to the user(client) application 1.)搜索艺术家并获取ID并将其返回给用户(客户端)应用程序

2.) Make a second REST API Call with the acquired id and fetch the top tracks 2.)使用获取的id进行第二次REST API调用并获取顶部曲目

Hope this helps. 希望这可以帮助。 Cheers! 干杯!

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

相关问题 Python的一个奇怪函数的结果,我无法用其他语言复制 - Python result of a strange function, that I can't reproduce in another language 如何将某些内容附加到函数的结果中并将其存储在python中的变量中 - How can I append something to a result of a function and store that in a Variable in python 我可以在Python Flask中调用带有相同文件并带有url请求的函数吗? - Can I call a function which calls another function with the same file with a url request in Python Flask? 如何在python中的函数内给变量另一个值? - how can I give a variable another value inside a function in python? 如何在另一个 python 脚本中访问函数内的变量 - How can I access a variable inside a function in another python script 如何将变量从一个 function 导入 Python 中的另一个变量? - How can I import a variable from one function to another in Python? 在另一个 function 中使用来自 function 的变量,与 flask - Use a variable from a function in another function, with flask 如何在主程序中打印 Python 中另一个 function 中的 function 的结果? - How can I print in the main program the result of a function within another function in Python? Python Nuke-如何使用另一个函数的变量结果 - Python Nuke - How to use variable result from another function 如何在另一个函数中使用函数的结果? - How can I use the result from a function in another function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM