Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace key in dynamic endpoint #209

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ferbercedo
Copy link

Added functionality to replace '{key}' with registry key to single records.

For example, url: https://example.com/{key}

Record with Key 11, new url: https://example.com/11

@ferbercedo ferbercedo requested review from a team as code owners November 27, 2023 16:11
Copy link
Contributor

@jeqo jeqo left a comment

Choose a reason for hiding this comment

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

@ferbercedo thanks for this PR. This is a beneficial feature for other users, I appreciate you taking the initiative to contribute it.

I have added a few comments to have a more complete solution to this, let's discuss. Also, please consider adding tests and docs to your proposal.

Copy link
Contributor

Choose a reason for hiding this comment

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

This sender needs to know if the URL is configured to inject a key or not. Otherwise, it will wastefully transform a key in to a string, and eventually not use it.
Consider adding a flag that is passed from the configuration to have this condition.

@@ -32,13 +32,13 @@ protected SingleRecordSender(final HttpSender httpSender) {
public void send(final Collection<SinkRecord> records) {
for (final SinkRecord record : records) {
final String body = recordValueConverter.convert(record);
httpSender.send(body);
httpSender.send(body, record.key().toString());
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider having a recordKeyConverter -- it should only convert known types (e.g. String, Numbers) into String to avoid messing with complex/unexpected conversions.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, what happens when the key is null? Should it use an empty string? fail? use a fallback value?

Comment on lines +52 to +59
final var configOriginalMap = this.config.originalsStrings();
final String genericUrl = configOriginalMap.get("http.url");
final String newUrl = genericUrl.replace("{key}", key);
configOriginalMap.replace("http.url", newUrl);
log.debug("Key replaced in URL. New URL: {}", newUrl);

final var newConfig = new HttpSinkConfig(configOriginalMap);
log.debug("Sending request with body: {}", body);
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of modifying the configs on every execution, I'd prefer a more structured approach to flag this.
Consider:

  • adding a new config, e.g. http.url.inject.key.enable=true/false defaults: false.
  • maybe have a fallback value to use it when key is null or cannot be converted, e.g. http.url.inject.key.fallback=string defaults: null -- if value cannot be converted or null, then fail at runtime.
  • delegate to the config on how to replace the key with the url by adding a new config.httpUri(keySupplier.get())

@@ -20,4 +20,6 @@

public interface HttpSender {
HttpResponse<String> send(final String body);

HttpResponse<String> send(final String body, String key);
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider passing the key as a function instead of a value:

Suggested change
HttpResponse<String> send(final String body, String key);
HttpResponse<String> send(final String body, Supplier<String> keySupplier);

@adaliszk
Copy link

adaliszk commented Nov 7, 2024

Awesome work on this!
Question though: How would this work with a JSON key?

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.

3 participants