[英]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.