简体   繁体   中英

Django views - Using requests

If I have a views.py function like this one right here:

#imports

env = environ.Env()
environ.Env.read_env()

 def stocks(request):
    input_ticker = request.POST['ticker']
    response = requests.get(f"https://cloud.iexapis.com/v1/stock/" + 
    input_ticker + "/quote?displayPercent=true&token=" + env('API_IEX'))

    parsed = response.json()

return render(request, 'result_api.html', {'api': parsed})

How can I scale up the flexibility and efficienty of my code if I want to add like 10-15 similar requests?

(...)
response2 = requests.get(f"https://cloud.iexapis.com/anything/anything/" + 
    input_ticker + "/anything=true&token=" + env('API_IEX'))

response3 = requests.get(f"https://cloud.iexapis.com/anything/anything2/" + 
         input_ticker + "/anything2=true&token=" + env('API_IEX'))
(...)

parsed2 = response2.json()
parsed3 = response2.json()
(...)

return render(request, 'result_api.html', {'api': parsed,
                                           'api2': parsed2,
                                           'api3': parsed3 ,            })

It would be pretty munch repeated, so I think there need to be a better way to solve this here.

PS: I am more into Django than Python atm. Probably I miss something obvious out here :D

How can I scale up the flexibility and efficienty of my code if I want to add like 10-15 similar requests?

I don't know much about eficciency but there's a little trick you can do to "automate" coding requests instead of copy-pasting them all it envolves arrays and string_replacements

max_requests=15 #change it to N

response_list=[]
#note that this loop goes 0-14 but you can change it if you like
for i in range(0,max_requests):
    response_list.append( requests.get(f"https://cloud.iexapis.com/anything/anything{i}/" +
         input_ticker + "/anything{i}=true&token=" + env('API_IEX')) )
    

parsed_list=[]
for i in range(0,max_requests):
    parsed_list.append( response_list[i].json() )

return_dict={}
for i in range(0, max_requests):
    return_dict[f"api{i}"]=parsed_list[i]
    #the end result will have api0,api1 instead of api,api1
    
return render(request, 'result_api.html',return_dict)

There are more way to make this code more readable and stuff, but this is what I could come up with before leaving.

In regard of efficiency, it looks like you're using DJango, there should be some libs to optimize lots of requests, BUT if you dont want to lib-hunt you can always think of paralelism of asyncronous request, however I am still a noob on this area.

EDIT: another try:

max_requests=15 #change it to whatever you like
return_dict=dict(zip([f"api{i}" for i in range(0,max_requests)],[request.json() for request in [requests.get(f"https://cloud.iexapis.com/anything/anything{i}/" +
     input_ticker + "/anything{i}=true&token=" + env('API_IEX')) for i in range(0,max_requests)]]))
return render(request, 'result_api.html',return_dict)

if it works it would be nice (but i'm not sure if request.get() works over list comprehension)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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