Skip to content

Comments

[FHIR Connector] Add a general proxy function to forward requests to FHIR server with url rewrite#55

Merged
anjuchamantha merged 5 commits intoballerina-platform:mainfrom
anjuchamantha:fhirConnector-export
Feb 24, 2026
Merged

[FHIR Connector] Add a general proxy function to forward requests to FHIR server with url rewrite#55
anjuchamantha merged 5 commits intoballerina-platform:mainfrom
anjuchamantha:fhirConnector-export

Conversation

@anjuchamantha
Copy link
Contributor

@anjuchamantha anjuchamantha commented Feb 23, 2026

$subject

This pull request adds a general-purpose proxy facility to the FHIR connector to forward incoming HTTP requests to a configured FHIR server, with optional URL rewriting, consistent error handling, and improved observability.

Changes

  • Files modified:
    • fhir/fhir_connector.bal
    • fhir/utils.bal
  • New public method:
    • FHIRConnector.proxy(requestUrl: string, request: http:Request) returns http:Response|FHIRError
  • New utility function:
    • generalRewriteServerUrl(http:Response, string baseUrl, string? replacementUrl = ()) in fhir/utils.bal

Behavior and outcomes

  • Forwards incoming HTTP requests to the configured FHIR server and returns the upstream http:Response or FHIRError.
  • Enriches forwarded requests with configured headers and PKJWT when applicable.
  • Logs incoming request URLs and proxy-related errors to improve observability.
  • Applies optional URL rewriting to upstream responses (headers and payload) when a replacement URL is configured, centralizing rewrite logic in the new utility.
  • Error handling follows existing connector patterns: existing FHIRError values are passed through; other errors are logged and wrapped consistently.

Other notes

  • The change introduces an additional forwarding path without modifying existing request-handling logic.
  • Approx. lines changed: fhir/fhir_connector.bal +25/-0; fhir/utils.bal +64/-1.

@coderabbitai
Copy link

coderabbitai bot commented Feb 23, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 98156c3 and f0b015c.

📒 Files selected for processing (1)
  • fhir/fhir_connector.bal

📝 Walkthrough

Walkthrough

Added a public proxy(requestUrl: string, request: http:Request) returns http:Response|FHIRError to FHIRConnector to forward incoming HTTP requests to the configured FHIR server (with header enrichment and optional response URL rewrite). Added generalRewriteServerUrl utility to rewrite base URLs in responses.

Changes

Cohort / File(s) Summary
FHIRConnector — proxy
fhir/fhir_connector.bal
Added public `proxy(requestUrl: string, request: http:Request) returns http:Response
Utilities — URL rewrite
fhir/utils.bal
Added isolated function generalRewriteServerUrl(http:Response, string baseUrl, string? replacementUrl = ()) that rewrites base URLs in response headers and payloads (JSON, XML, text) and returns modified http:Response or FHIRError. Minor formatting adjustment in getBundleResponse.

Sequence Diagram(s)

sequenceDiagram
    participant Client as Client
    participant Connector as FHIRConnector
    participant HTTPClient as HTTP Client
    participant FHIRServer as FHIR Server

    Client->>Connector: proxy(requestUrl, request)
    Note over Connector: Log incoming request URL
    Connector->>Connector: Enrich headers (add PKJWT if configured)
    Connector->>HTTPClient: forward(requestUrl, enriched request)
    HTTPClient->>FHIRServer: Forwarded HTTP request
    FHIRServer-->>HTTPClient: Upstream response
    HTTPClient-->>Connector: Return response
    Note over Connector: Optionally call generalRewriteServerUrl to rewrite URLs
    Connector-->>Client: Return http:Response or FHIRError
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐇 I hop your packets through the glen,
I tuck in headers, thread again,
I nudge the URLs to fit the view,
Return the answer swift and true,
A rabbit's patch — a tiny mend.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and clearly describes the main changes: adding a general proxy function to forward requests to a FHIR server with URL rewrite capability, which matches the core functionality additions.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@fhir/fhir_connector.bal`:
- Line 991: Update the `@return` doc comment for the FHIRResponse to remove the
contradiction by rephrasing it to something like "The upstream FHIRResponse,
with URL rewriting applied when enabled." Locate the `@return` entry in the doc
comment block that references FHIRResponse in fhir_connector.bal (the line
containing "@return - The upstream FHIRResponse as-is (with URL rewriting
applied when enabled)") and replace that text so it clearly states the response
may be rewritten when enabled.
- Around line 997-1002: The code currently assigns the result of
self.httpClient->forward(...) directly to a FHIRResponse, which bypasses
parsing; change the flow to call getFhirResourceResponse() with the
http:Response returned by self.httpClient->forward(enrichRequest(...)) so the
http response is converted into a proper FHIRResponse (preserving
httpStatusCode, resource, and serverResponseHeaders), then pass that parsed
FHIRResponse into rewriteServerUrl(...) when self.urlRewrite is true and return
the parsed/possibly-rewritten FHIRResponse; also update the method docstring to
remove the "as-is" phrasing and note that URL rewriting may be applied.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@fhir/fhir_connector.bal`:
- Line 987: Update the function-level summary that currently reads "Proxies an
incoming HTTP request to the FHIR server and returns the upstream response
as-is" to accurately reflect that URL rewriting may be applied when
self.urlRewrite is true; find the function whose docstring contains that
sentence (the method handling the incoming HTTP proxy and referencing
self.urlRewrite) and change the summary to something like "Proxies an incoming
HTTP request to the FHIR server and returns the upstream response (URLs may be
rewritten when self.urlRewrite is true)" so the description no longer claims the
response is returned "as-is."

In `@fhir/utils.bal`:
- Around line 608-610: The current isolated function rewrite uses re
`${baseUrl}` which treats baseUrl as a regex and causes false-positive matches;
implement a fix by creating a utility to escape regex metacharacters in baseUrl
(or alternatively reconstruct the URL and replace parts without regex), then use
the escaped string when building the regex pattern or use a literal string
replacement routine; update the rewrite function (and the other occurrence where
baseUrl is interpolated into a regex) to call that escape utility (or use the
non-regex replacement approach) and then perform the replace so matches are
exact for replacementUrl.

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3c2ae4c and 98156c3.

📒 Files selected for processing (2)
  • fhir/fhir_connector.bal
  • fhir/utils.bal

@anjuchamantha anjuchamantha merged commit 1291b74 into ballerina-platform:main Feb 24, 2026
5 checks passed
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