Skip to content

Commit

Permalink
Limit scope of OpenAiRestApi providers
Browse files Browse the repository at this point in the history
The previous way they were declared meant
that they would affect other REST Clients
too.
  • Loading branch information
geoand committed Dec 16, 2023
1 parent e23407a commit 952a53d
Showing 1 changed file with 4 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,24 @@
import java.util.regex.Pattern;

import jakarta.annotation.Priority;
import jakarta.ws.rs.ConstrainedTo;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Priorities;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.RuntimeType;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.MessageBodyWriter;
import jakarta.ws.rs.ext.Provider;
import jakarta.ws.rs.ext.ReaderInterceptor;
import jakarta.ws.rs.ext.ReaderInterceptorContext;
import jakarta.ws.rs.ext.WriterInterceptor;
import jakarta.ws.rs.ext.WriterInterceptorContext;

import org.eclipse.microprofile.rest.client.annotation.ClientHeaderParam;
import org.eclipse.microprofile.rest.client.annotation.RegisterProvider;
import org.jboss.logging.Logger;
import org.jboss.resteasy.reactive.RestQuery;
import org.jboss.resteasy.reactive.RestStreamElementType;
Expand Down Expand Up @@ -76,6 +74,9 @@
@ClientHeaderParam(name = "api-key", value = "{apiKey}") // used by AzureAI
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@RegisterProvider(OpenAiRestApi.OpenAiRestApiJacksonProvider.class)
@RegisterProvider(OpenAiRestApi.OpenAiRestApiReaderInterceptor.class)
@RegisterProvider(OpenAiRestApi.OpenAiRestApiWriterInterceptor.class)
public interface OpenAiRestApi {

/**
Expand Down Expand Up @@ -185,8 +186,6 @@ public boolean test(SseEvent<String> event) {
* We need a custom version of the Jackson provider because reading SSE values does not work properly with
* {@code @ClientObjectMapper} due to the lack of a complete context in those requests
*/
@Provider
@ConstrainedTo(RuntimeType.CLIENT)
@Priority(Priorities.USER + 100)
class OpenAiRestApiJacksonProvider extends AbstractJsonMessageBodyReader implements MessageBodyWriter<Object> {

Expand Down Expand Up @@ -230,8 +229,6 @@ public static class ObjectMapperHolder {
/**
* This method validates that the response is not empty, which happens when the API returns an error object
*/
@Provider
@ConstrainedTo(RuntimeType.CLIENT)
class OpenAiRestApiReaderInterceptor implements ReaderInterceptor {

@Override
Expand Down Expand Up @@ -272,8 +269,6 @@ private Object validateResponse(Object result) {
* The point of this is to properly set the {@code stream} value of the request
* so users don't have to remember to set it manually
*/
@Provider
@ConstrainedTo(RuntimeType.CLIENT)
class OpenAiRestApiWriterInterceptor implements WriterInterceptor {
@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
Expand Down

0 comments on commit 952a53d

Please sign in to comment.