import requests
import json


def get_conv_rate(apikey, from_curr, to_curr):
    """Gets conversion rate between currencies by using API call to service

    Args:
        apikey (str): API key for free.currconv.com
        from_curr (str): 3 letter abbreviation of currency to convert from
        to_curr (str): 3 letter abbreviation of currency to convert to

    Returns:
        float: conversion rate, else 0 if invalid conversion
    """
    # string required for API in determining conversion
    conv_string = f"{from_curr}_{to_curr}".upper()
    # Attempt to get conversion rate of args specified
    conv_rate = requests.get('https://free.currconv.com/api/v7/convert',
        params={
            'apiKey':apikey,
            'compact':'ultra',
            'q':conv_string
        }
    )

    # Return 0 if the conversion rate was invalid, else parse and return float
    if conv_rate.text == "{}":
        return 0
    else:
        return float(json.loads(conv_rate.text)[conv_string])