stud1234
stud1234

Reputation: 91

Update global variable in an asychrounouus way in Django

I have a tacky problem. On my website, you can find current price tracker for crypto coins as a purely content filler element. I get my data from coin API, you are allowed 100 request's per day.

def crypto_api_prices(request):
    
    api_base_url = 'https://rest.coinapi.io/v1/exchangerate/'
    api_key = '==============='

    headers = {
        'X-CoinAPI-Key': api_key,
    }

    crypto_tickers = ["BTC","ETH","ETC","RVN"]
    crypto_api_links = [api_base_url+x+"/"+"USD" for x in crypto_tickers ]
    crypto_api_prices = [requests.get(x, headers=headers).json()['rate'] for x in crypto_api_links]
    context = {
        "cryptoPrices":crypto_api_prices
        }
    return context

I'm calling this function in my home view. Unfortunately, if I have couple of visitors daily this generates 100 requests pretty fast and my element isn't working anymore.

What I want to achieve is set crypto_api_prices as a global variable and update it every hour, so no matter how many users will be on my page I will generate only 24 requests.

Is this a good idea? Are there alternatives? Of course I can buy more requests but this website is a "passion" project so I want to keep costs down

Upvotes: 0

Views: 112

Answers (1)

furas
furas

Reputation: 143032

I would do two things:

  1. I would use

    /v1/exchangerate/USD?invert=true
    

    to get rates for all tickers in one request

    (doc: https://docs.coinapi.io/?python#get-all-current-rates-get)

    and later I would filter needed tickers. And it would works even if I change list of tickers

  2. I would use requests-cache to get results from cache, and make real request only every 15 minutes - so it should give 100 requests a day


Minimal working code

import os
import requests
import requests_cache
from datetime import timedelta

# --- constants ---  # PEP8: `UPPER_CASE_NAMES` for constants

API_KEY = os.getenv('COINAPI_TOKEN')

# --- session: cache and API key ---

s = requests_cache.CachedSession(expire_after=timedelta(minutes=15))
#s.cache.clear()  # manually clear cache

s.headers.update({'X-CoinAPI-Key': API_KEY})

# --- every rate separatelly ---

url = 'https://rest.coinapi.io/v1/exchangerate/{}/USD'

tickers = ["BTC", "ETC", "ETH", "RVN"]

prices = {coin: s.get(url.format(coin)).json()['rate']
            for coin in tickers}

context = {"cryptoPrices": prices}

print(context["cryptoPrices"])

# --- all rates ---

url = 'https://rest.coinapi.io/v1/exchangerate/USD?invert=True'

response = s.get(url)
data = response.json()

# filter data

tickers = ["BTC", "ETH", "ETC", "RVN"]

prices = {}

for item in data['rates']:
    if item['asset_id_quote'] in tickers:
        key = item['asset_id_quote']
        val = item['rate']
        prices[key] = val

context = {"cryptoPrices": prices}

print(context["cryptoPrices"])

Upvotes: 2

Related Questions