cdrrr
cdrrr

Reputation: 1112

Python - improve time needed for requesting an API

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': 'b4ed4ab7ebe84f0db79d42633771b741'
    }

    # User's credentials
    body_login = {
        'username': 'user',
        'password': 'pass'
    }

    # Make the login request and return the token
    request_url = requests.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': 'b4ed4ab7ebe84f0db79d42633771b741'
    }
    # Make the request and return the country code
    request_url = requests.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': 'b4ed4ab7ebe84f0db79d42633771b741'
    }
    # Load the country id
    params = {
        'countryId': countries()
    }

    # Make the request and return the county code based on the city
    request_url = requests.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': 'b4ed4ab7ebe84f0db79d42633771b741'
    }
    # 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 = requests.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?

Modified code

# Function - login

session = requests.Session()

print('Process start! ' + str(datetime.now()))


def login():
    # Application header
    header = {
        'Content-Type': 'application/json',
        'Ocp-Apim-Subscription-Key': 'b4ed4ab7ebe84f0db79d42633771b741'
    }

    # 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
    temp_token = 'Bearer ' + request_url.text.strip('"')

    return temp_token


token = login()

# Get country code. Since it's a local product, the request will return only the code for RO


def countries():
    # Application header
    header = {
        'Authorization': token,
        'Ocp-Apim-Subscription-Key': 'b4ed4ab7ebe84f0db79d42633771b741'
    }
    # 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': token,
        'Ocp-Apim-Subscription-Key': 'b4ed4ab7ebe84f0db79d42633771b741'
    }
    # 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': token,
        'Ocp-Apim-Subscription-Key': 'b4ed4ab7ebe84f0db79d42633771b741'
    }
    # 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
    )

    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

Upvotes: 0

Views: 91

Answers (2)

Moshe perez
Moshe perez

Reputation: 1726

You can call every function once, and reuse the output as parameter for the other functions:

import requests
import json
from unidecode import unidecode
from datetime import datetime

temp_county = 'County'
temp_locality = 'Locality'

session = requests.Session()

def login():
    # Application header
    header = {
        'Content-Type': 'application/json',
        'Ocp-Apim-Subscription-Key': 'b4ed4ab7ebe84f0db79d42633771b741'
    }

    # 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
    temp_token = 'Bearer ' + request_url.text.strip('"')

    return temp_token

# Get country code. Since it's a local product, the request will return only the code for RO

def countries(token):
    # Application header
    header = {
        'Authorization': token,
        'Ocp-Apim-Subscription-Key': 'b4ed4ab7ebe84f0db79d42633771b741'
    }
    # 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(token, countryId):
    # Application header
    header = {
        'Authorization': token,
        'Ocp-Apim-Subscription-Key': 'b4ed4ab7ebe84f0db79d42633771b741'
    }
    # Load the country id
    params = {
        'countryId': countryId
    }

    # 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(token, countryId, countyId):
    # Application header
    header = {
        'Authorization': token,
        'Ocp-Apim-Subscription-Key': 'b4ed4ab7ebe84f0db79d42633771b741'
    }
    # Load the country id and county id
    params = {
        'countryId': countryId,
        'countyId': countyId
    }

    # 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
    )

    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

token = login()
countryId = countries(token)
countyId = counties(token, countryId)
localityId = localities(token, countryId, countyId)

Upvotes: 1

fiticida
fiticida

Reputation: 694

Maybe you can change the dictionary iterations in all your dictionaries, by using items dict method.

 for k , v in counties_dictionary.items():
        if k == 'Name':
            countyId = str(countyName['CountyId']) if v == locality else "Whatever you want"

Upvotes: 0

Related Questions