I'm using the below code to call different methods from an API:
import requests
import json
from unidecode import unidecode
from datetime import datetime
temp_county = 'County'
temp_locality = 'Locality'
# Function - login
session = requests.Session()
def login():
# Application header
header = {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': '{KEY}'
}
# User's credentials
body_login = {
'username': 'user',
'password': 'pass'
}
# Make the login request and return the token
request_url = session.post(
'https://urgentcargus.azure-api.net/api/LoginUser', json=body_login, headers=header)
# Parse the token and remove double quotes
token = 'Bearer ' + request_url.text.strip('"')
return token
# Get country code. Since it's a local product, the request will return only the code for RO
def countries():
# Application header
header = {
'Authorization': login(),
'Ocp-Apim-Subscription-Key': '{KEY}'
}
# Make the request and return the country code
request_url = session.get(
'https://urgentcargus.azure-api.net/api/Countries', headers=header
)
countries_dictionary = request_url.json()
for country in countries_dictionary:
if country['Abbreviation'] == 'RO':
countryId = str(country['CountryId'])
return countryId
def counties():
# Application header
header = {
'Authorization': login(),
'Ocp-Apim-Subscription-Key': '{KEY}'
}
# Load the country id
params = {
'countryId': countries()
}
# Make the request and return the county code based on the city
request_url = session.get(
'https://urgentcargus.azure-api.net/api/Counties', params=params, headers=header
)
counties_dictionary = request_url.json()
# Parse the city description and transliterate it to ASCII
county = unidecode(temp_county)
# print(county)
# Return the county code
for countyName in counties_dictionary:
if countyName['Name'] == county:
countyId = str(countyName['CountyId'])
return countyId
def localities():
# Application header
header = {
'Authorization': login(),
'Ocp-Apim-Subscription-Key': '{KEY}'
}
# Load the country id and county id
params = {
'countryId': countries(),
'countyId': counties()
}
# Make the request and return the county code based on the city
request_url = session.get(
'https://urgentcargus.azure-api.net/api/Localities', params=params, headers=header, stream=True
)
localities_dictionary = request_url.json()
# Parse the city description and transliterate it to ASCII
locality = unidecode(temp_locality)
# print(county)
# Return the locality code
for localityName in localities_dictionary:
if localityName['Name'] == locality:
localityId = str(localityName['LocalityId'])
return localityId
#
print(login())
print(countries())
print(counties())
print(localities())
The functions are working well, with no errors or something. Problem is that it require (in my opinion) lot of time to complete all functions and return what it has to return.
I have used requests.Session()
in order to create one persistent session in order to save time but somehow is the same behavior.
I've monitored how much time is required and for instance, it takes about 5 - 6 seconds to complete:
print('Process start! ' + str(datetime.now()))
# here are all the functions
print('Process ended! ' + str(datetime.now()))
Terminal response:
Process start! 2019年10月18日 13:26:09.796132
Bearer 8JCAOoSSevSpcNDydLHSAmZORL0RGgDXV110IUhxIRWABX0TNj
1
26
163
Process ended! 2019年10月18日 13:26:14.663092
Is there any way to improve it?
2 Answers 2
You needlessly loop through the whole collection in all the methods, instead of returning as soon as you find what you need. If the thing you look for is right at the start, you still go thru all other entries.
for country in countries_dictionary:
if country['Abbreviation'] == 'something':
countryId = str(country['CountryId'])
return countryId
Instead do:
for country in countries_dictionary:
if country['Abbreviation'] == 'something':
return str(country['CountryId'])
-
\$\begingroup\$ can you be more specific with my examples? If I just remove where I parse it, I get an error saying that
countryId
is not defined. \$\endgroup\$cdrrr– cdrrr2019年10月18日 17:25:04 +00:00Commented Oct 18, 2019 at 17:25 -
\$\begingroup\$ @cdrrr, I updated the code \$\endgroup\$TomG– TomG2019年10月18日 18:25:21 +00:00Commented Oct 18, 2019 at 18:25
-
2\$\begingroup\$ First of all, you misquoted the code — the original code involved
country['Abbreviation']
. Secondly, you can get rid of the loop altogether:return next(str(c['CountryId']) for c in countries_dictionary if c['Abbreviation'] == 'RO')
\$\endgroup\$200_success– 200_success2019年10月18日 21:12:08 +00:00Commented Oct 18, 2019 at 21:12 -
\$\begingroup\$ Thanks 200_success, misquote fixed. I didn't want to change OPs code too much, just explain the concept of early return. \$\endgroup\$TomG– TomG2019年10月19日 05:22:55 +00:00Commented Oct 19, 2019 at 5:22
Basically you need to reuse your session variable on the code, for example:
print(login(session))
print(countries(session))
print(counties(session))
print(localities(session))
And inside that functions change the calls that referrers to "requests" to the "session" variable like:
request_url = session.get(
'https://urgentcargus.azure-api.net/api/Localities',...
Explore related questions
See similar questions with these tags.
requests.get...
I've replace withsession.get...
. You meant like this? \$\endgroup\$