Skip to content

feat: Instance-Aware Networking (InstanceHostInterceptor) - #515

Merged
volodymyr-chekyrta merged 6 commits into
openedx:developfrom
raccoongang:infra/05-instance-aware-networking
Oct 27, 2026
Merged

volodymyr-chekyrta merged 6 commits into
openedx:developfrom
raccoongang:infra/05-instance-aware-networking

Conversation

@IvanStepanok

Copy link
Copy Markdown
Contributor

Base branch
#514

Description

Routes API requests to the selected instance's host and refreshes tokens against that instance. Port of openedx-app-ios#677. This is where the instance model and selection start affecting network traffic.

What's in this PR

InstanceHostInterceptor

  • Requests built against the app-level API_HOST_URL (Retrofit's base URL) are re-targeted at the selected instance: scheme, host and port change; path and query stay.
  • Requests to any other host (CDNs, third-party SDKs), and every request while no instance is selected, pass through unmodified.
  • Each request is tagged with the instance it was sent for.

Token refresh (OauthRefreshTokenAuthenticator)

  • Calls the selected instance's /oauth2/access_token/ with its OAUTH_CLIENT_ID, falling back to the app-level values when nothing is selected. TOKEN_TYPE stays app-level here; the launch-sequencing PR makes it instance-aware.
  • A 401 for a request sent before a site switch neither refreshes nor logs out: it says nothing about the site selected now. A retry is never built with the new site's token.

HeadersInterceptor

  • The access token is only attached to requests for the selected site's host. Android-specific: the iOS interceptor has no such check, but here one OkHttp client serves every request, so the token could otherwise reach another host.

Tests

  • InstanceAwareNetworkingTest (9 cases): host rewrite with path and query preserved, pass-through with no selection, untouched third-party hosts and ports, the token sent only to the active site, refresh against the selected host and client, a stale 401 that neither refreshes nor logs out, and tokens returned after a switch.
  • org.json:json is added as a test dependency: the Android stub returns defaults, and the authenticator parses error bodies with it.

Out of scope

  • Instance-scoped preferences, database and downloads.
  • Session lifecycle across switches.

Testing

  • ./gradlew detektAll testProdDebugUnitTest :app:assembleProdDebug

Introduces Instance and InstancesConfig: what one Open edX platform is and how it is
parsed from the INSTANCES block of the bundled config. Nothing reads it yet.

Required fields are NAME, API_HOST_URL and OAUTH_CLIENT_ID; a malformed entry is dropped.
KEY falls back to a slug of NAME, INSTANCE_NAME is localized with an English fallback,
and PLATFORM_NAME defaults to the display name. The schema matches iOS (#672).
InstanceStore holds the instance catalog and the selected instance, persists the
selection by key and publishes it as a StateFlow. A single-instance catalog selects its
only instance automatically. Registered in Koin as InstanceStore and InstanceProvider;
nothing consumes it yet. Mirrors iOS #673.
InstanceTheme derives a palette from an instance's accent color and its optional
THEME.LIGHT/THEME.DARK overrides: only the fields an instance sets change, the rest keep
the compiled default, and a missing dark value falls back to the light one. Palette field
names are the ones iOS uses (#675), mapped onto AppColors.

OpenEdXTheme observes InstanceThemeManager, which holds the selected instance's theme.
Nothing applies a theme yet, so every screen still renders the default palette.
InstanceConfigLoader resolves the catalog the app runs with: the last cached remote
catalog, else the bundled INSTANCES list, else nothing. A live fetch with at least one
instance replaces that baseline and becomes the cache; a failed, malformed or empty
response leaves it untouched.

The catalog is fetched from INSTANCES_CATALOG_URL with a plain OkHttp client: no app
interceptors, no credentials, a 10 second timeout. Remote snake_case keys are normalized
with the same map iOS uses (#676). Nothing calls the loader at launch yet.

Unlike iOS, the YAML build configuration stays: the Gradle build already turns it into
the bundled config.json the loader reads.
InstanceHostInterceptor re-targets requests built against the app's base URL at the
selected instance: scheme, host and port change, path and query stay. Other hosts, and
every request made with nothing selected, pass through. Each request is tagged with the
instance it was sent for.

Token refresh calls the selected instance's /oauth2/access_token with its OAuth client.
A 401 for a request sent before a site switch neither refreshes nor logs out, and tokens
returned after the learner switched are discarded. The access token is only attached to
requests for the active site's host. Mirrors iOS #677.
@openedx-webhooks openedx-webhooks added open-source-contribution PR author is not from Axim or 2U core contributor PR author is a Core Contributor (who may or may not have write access to this repo). labels Oct 19, 2026
@openedx-webhooks

Copy link
Copy Markdown

Thanks for the pull request, @IvanStepanok!

This repository is currently maintained by @openedx/openedx-mobile-maintainers.

Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review.

🔘 Get product approval

If you haven't already, check this list to see if your contribution needs to go through the product review process.

  • If it does, you'll need to submit a product proposal for your contribution, and have it reviewed by the Product Working Group.
    • This process (including the steps you'll need to take) is documented here.
  • If it doesn't, simply proceed with the next step.
🔘 Provide context

To help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:

  • Dependencies

    This PR must be merged before / after / at the same time as ...

  • Blockers

    This PR is waiting for OEP-1234 to be accepted.

  • Timeline information

    This PR must be merged by XX date because ...

  • Partner information

    This is for a course on edx.org.

  • Supporting documentation
  • Relevant Open edX discussion forum threads
🔘 Get a green build

If one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green.

Details
Where can I find more information?

If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources:

When can I expect my changes to be merged?

Our goal is to get community contributions seen and reviewed as efficiently as possible.

However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:

  • The size and impact of the changes that it introduces
  • The need for product review
  • Maintenance status of the parent repository

💡 As a result it may take up to several weeks or months to complete a review and merge your PR.

RawanMatar89
RawanMatar89 previously approved these changes Oct 21, 2026
@mphilbrick211 mphilbrick211 moved this from Needs Triage to In Eng Review in Contributions Oct 22, 2026
@IvanStepanok

Copy link
Copy Markdown
Contributor Author

@RawanMatar89 I merged develop into this branch to clear the conflicts left by squashing #514. This PR's own changes didn't move: the six networking files are byte-identical to the commit you approved, and the two theme files now match develop. Unit tests, detekt and the prod debug build pass locally. GitHub will drop your approval because of the new commit, so could you re-approve when you have a minute?

@volodymyr-chekyrta
volodymyr-chekyrta merged commit cd20d1f into openedx:develop Oct 27, 2026
6 checks passed
@github-project-automation github-project-automation Bot moved this from In Eng Review to Done in Contributions Oct 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core contributor PR author is a Core Contributor (who may or may not have write access to this repo). open-source-contribution PR author is not from Axim or 2U

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants