Fetching counties and localities using JSON API calls

Posted on

Problem

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?

Solution

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'])

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',...

Leave a Reply

Your email address will not be published. Required fields are marked *