3
\$\begingroup\$

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?

asked Oct 18, 2019 at 10:44
\$\endgroup\$
6
  • \$\begingroup\$ You are not using the variable session on your code, you are still using request. That should give you some improvement, also you should put the operations that you are making between the print statements so is clear what are you executing. \$\endgroup\$ Commented Oct 18, 2019 at 10:49
  • \$\begingroup\$ @camp0 - the same functions as above. \$\endgroup\$ Commented Oct 18, 2019 at 10:55
  • \$\begingroup\$ You are not using the variable session on your code, you should replace the requests.get and requests.post operations for session.get and session.post \$\endgroup\$ Commented Oct 18, 2019 at 10:59
  • \$\begingroup\$ I've edited my code. Instead of requests.get... I've replace with session.get.... You meant like this? \$\endgroup\$ Commented Oct 18, 2019 at 11:01
  • 1
    \$\begingroup\$ Yes with that change you will at least make just one TLS handshake for all your requests instead the 4, hope it helps \$\endgroup\$ Commented Oct 18, 2019 at 12:38

2 Answers 2

2
\$\begingroup\$

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'])
answered Oct 18, 2019 at 16:13
\$\endgroup\$
4
  • \$\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\$ Commented Oct 18, 2019 at 17:25
  • \$\begingroup\$ @cdrrr, I updated the code \$\endgroup\$ Commented 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\$ Commented 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\$ Commented Oct 19, 2019 at 5:22
3
\$\begingroup\$

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',...
answered Oct 18, 2019 at 16:23
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.