Skip to content

fix: update geolocation fallback API and use static country code mapping#1104

Open
ucodia wants to merge 2 commits intomlco2:masterfrom
ucodia:fix-geolocation-fallback
Open

fix: update geolocation fallback API and use static country code mapping#1104
ucodia wants to merge 2 commits intomlco2:masterfrom
ucodia:fix-geolocation-fallback

Conversation

@ucodia
Copy link
Contributor

@ucodia ucodia commented Mar 13, 2026

Description

Fixes geolocation fallback API call

Issue 1

The fallback API will always fail when using the HTTPS endpoint

curl https://ip-api.com/json/
{"status":"fail","message":"SSL unavailable for this endpoint, order a key at https://members.ip-api.com/"}%

Using the HTTP endpoint is not great because this could expose the application location to eavesdropper and be vulnerable to man-in-the-middle.

Issue 2

If the fallback API call fails, it reports on the wrong URL on the exception: https://github.com/ucodia/codecarbon/blob/ebc8d1a3f5dcae9013dcbe6883d05e711ad6ce28/codecarbon/external/geography.py#L136

Non-issue 1

The current fallback relies on another API to map the 2 letters country code to a 3 letters code. This is static data that almost never changes, by switching to a static mapping provider we can make the fallback more resilient.

Related Issue

Fixes #1101

Motivation and Context

Certain IP ranges are not covered by the primary API and therefore a fallback is necessary otherwise CodeCarbon client will estimate on the wrong energy mix.

How Has This Been Tested?

  • Unit testing
  • Tested in production environment

Screenshots (if appropriate):

N/A

Types of changes

What types of changes does your code introduce? Put an x in all the boxes that apply:

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

Go over all the following points, and put an x in all the boxes that apply.

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING.md document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@ucodia ucodia marked this pull request as ready for review March 14, 2026 00:20
@ucodia ucodia requested a review from a team as a code owner March 14, 2026 00:20
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.

Geolocation fallback provider is not working

1 participant