Skip to content

Add crypto_market.py: refactor duplicated CoinGecko API patterns into shared helpers#4

Open
Copilot wants to merge 3 commits intomainfrom
copilot/refactor-duplicated-code
Open

Add crypto_market.py: refactor duplicated CoinGecko API patterns into shared helpers#4
Copilot wants to merge 3 commits intomainfrom
copilot/refactor-duplicated-code

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 13, 2026

The repository had no source code. This PR introduces crypto_market.py, a CoinGecko API utility module structured from the ground up to avoid duplication.

Refactoring highlights

  • _get(endpoint, params) — single HTTP call + error handling helper; replaces the repeated requests.get / raise_for_status / .json() triad that would otherwise appear in every function
  • _format_price(coin_id, price, currency) — single formatting definition instead of inline f-strings scattered across display functions
  • Parameterised fetchget_coin_price(coin_id) and get_prices(coin_ids) replace per-coin duplicate functions with identical bodies:
# Instead of:
def get_bitcoin_price():
    url = "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd"
    response = requests.get(url); response.raise_for_status()
    return response.json()["bitcoin"]["usd"]

def get_ethereum_price():   # identical structure, different strings
    ...

# Refactored to:
def get_coin_price(coin_id, vs_currency="usd"):
    data = _get("/simple/price", params={"ids": coin_id, "vs_currencies": vs_currency})
    ...
    return data[coin_id][vs_currency]
  • Loop-based displaydisplay_prices / display_market_summary iterate over a coin list rather than per-coin copy-paste blocks

Error handling

_get emits a descriptive error including the endpoint URL and HTTP status code; get_coin_price and get_prices raise ValueError with the specific coin/currency missing from the response rather than an opaque KeyError.


📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.

@marcobar324 marcobar324 marked this pull request as ready for review March 13, 2026 07:30
Copilot AI and others added 2 commits March 13, 2026 07:30
…unctions

Co-authored-by: marcobar324 <246922463+marcobar324@users.noreply.github.com>
Co-authored-by: marcobar324 <246922463+marcobar324@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor duplicated code in project Add crypto_market.py: refactor duplicated CoinGecko API patterns into shared helpers Mar 13, 2026
Copilot AI requested a review from marcobar324 March 13, 2026 07:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants