Skip to content

feat: add builder to OtpApiClient#131

Merged
leonardehrenfried merged 3 commits intoopentripplanner:mainfrom
jeeeesper:add-client-builder
Feb 20, 2026
Merged

feat: add builder to OtpApiClient#131
leonardehrenfried merged 3 commits intoopentripplanner:mainfrom
jeeeesper:add-client-builder

Conversation

@jeeeesper
Copy link
Contributor

This, for me, came up because I wanted to specify the full URL and an authentication token. For example, the Routing API for the Helsinki region is available at https://api.digitransit.fi/routing/v2/hsl/gtfs/v1, where the path is not the default path configured in the client.

I'm happy to change the implementation or discuss about other approaches, this seemed like the most obvious solution to me.

this((BuilderImpl) OtpApiClient.builder().baseUri(baseUrl).timeZone(timezone));
}

private OtpApiClient(final BuilderImpl b) {
Copy link
Member

Choose a reason for hiding this comment

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

If this method is private, how do you use the builder then?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Through OtpApiClient::builder, so the usage would be
OtpApiClient client = OtpApiClient.builder().baseUri(...).timeZone(...).build()

}

private OtpApiClient(final BuilderImpl b) {
final var zoneId = Objects.requireNonNullElse(b.zoneId, ZoneId.systemDefault());
Copy link
Member

Choose a reason for hiding this comment

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

The zone id must be provided otherwise we get confusing results.

Having said that, i actually want to get rid of it completely because in newer OTP versions, it's no longer needed. The zone is part of the response.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree, I added it as a required parameter

@leonardehrenfried
Copy link
Member

Maybe write a test or some Markdown as a bit of documentation.

@jeeeesper
Copy link
Contributor Author

jeeeesper commented Feb 20, 2026

I wrote some (very basic) tests and a small addition to the README file, and addressed your comments in code. I'm not sure whether my tests are good where they are, through, or whether I should move them to IntegrationTests as well. They do test against the dev API endpoint, however, they are not testing any actual functionality of it.

@leonardehrenfried leonardehrenfried merged commit 4d26bfa into opentripplanner:main Feb 20, 2026
1 check passed
@jeeeesper jeeeesper deleted the add-client-builder branch February 20, 2026 11:03
@leonardehrenfried
Copy link
Member

Thanks for this.

I think having custom headers are a frequent use case. If you want you can add a special method to the builder for it.

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