Skip to content

Commit

Permalink
Draft: refactor ModelAuthProvider to async
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego Ramp committed Jan 13, 2025
1 parent e0f1362 commit 7ed84d8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import jakarta.enterprise.inject.spi.CDI;

import io.quarkiverse.langchain4j.ModelName;
import io.smallrye.mutiny.Uni;

/**
* Model authentication providers can be used to supply credentials such as access tokens, API keys, and other type of
Expand All @@ -27,6 +28,10 @@ public interface ModelAuthProvider {
*/
String getAuthorization(Input input);

default Uni<String> getAuthorizationAsync(Input input) {
return Uni.createFrom().item(getAuthorization(input));
}

/*
* Representation of an HTTP request to the model provider
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,18 +185,28 @@ public boolean test(SseEvent<String> event) {

class OpenAIRestAPIFilter implements ResteasyReactiveClientRequestFilter {
ModelAuthProvider authorizer;
Logger log = Logger.getLogger(OpenAIRestAPIFilter.class);

public OpenAIRestAPIFilter(ModelAuthProvider authorizer) {
this.authorizer = authorizer;
}

@Override
public void filter(ResteasyReactiveClientRequestContext requestContext) {
String authValue = authorizer.getAuthorization(new AuthInputImpl(requestContext.getMethod(),
requestContext.getUri(), requestContext.getHeaders()));
if (authValue != null) {
requestContext.getHeaders().putSingle("Authorization", authValue);
}
requestContext.suspend();
authorizer
.getAuthorizationAsync(
new AuthInputImpl(
requestContext.getMethod(), requestContext.getUri(), requestContext.getHeaders()))
.subscribe().with(authValue -> {
if (authValue != null) {
requestContext.getHeaders().putSingle("Authorization", authValue);
}
requestContext.resume();
}, t -> {
log.warnf(t, "failed on aquiring authorization, proceeding without it");
requestContext.resume(t);
});
}

private record AuthInputImpl(
Expand Down

0 comments on commit 7ed84d8

Please sign in to comment.