Skip to content

Commit 9fa8f7c

Browse files
authored
Merge pull request #10 from Dannyj1/cleanup
Fix & Cleanup of Several SonarQube Issues
2 parents 77daba6 + a7ffe61 commit 9fa8f7c

File tree

11 files changed

+50
-65
lines changed

11 files changed

+50
-65
lines changed

src/main/java/nl/dannyj/mistral/MistralClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ public void createChatCompletionStream(@NonNull ChatCompletionRequest request, @
251251
* @return A new instance of MistralService
252252
*/
253253
private MistralService buildMistralService() {
254-
return new MistralService(this, new HttpService(this));
254+
return new MistralService(new HttpService(this.httpClient), this.objectMapper);
255255
}
256256

257257
/**
@@ -260,7 +260,7 @@ private MistralService buildMistralService() {
260260
* @return A new instance of OkHttpClient
261261
*/
262262
private OkHttpClient buildHttpClient(int readTimeoutSeconds, int connectTimeoutSeconds, int writeTimeoutSeconds) {
263-
MistralHeaderInterceptor mistralInterceptor = new MistralHeaderInterceptor(this);
263+
MistralHeaderInterceptor mistralInterceptor = new MistralHeaderInterceptor(this.getApiKey());
264264

265265
return new OkHttpClient.Builder()
266266
.readTimeout(readTimeoutSeconds, TimeUnit.SECONDS)

src/main/java/nl/dannyj/mistral/builders/MessageListBuilder.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import jakarta.annotation.Nullable;
2020
import jakarta.validation.constraints.NotEmpty;
21-
import jakarta.validation.constraints.NotNull;
21+
import lombok.NonNull;
2222
import nl.dannyj.mistral.models.completion.message.AssistantMessage;
2323
import nl.dannyj.mistral.models.completion.message.ChatMessage;
2424
import nl.dannyj.mistral.models.completion.message.SystemMessage;
@@ -59,7 +59,7 @@ public MessageListBuilder(List<ChatMessage> messages) {
5959
* @param content The text content of the system message. Cannot be null.
6060
* @return This builder instance.
6161
*/
62-
public MessageListBuilder system(@NotNull String content) {
62+
public MessageListBuilder system(@NonNull String content) {
6363
this.messages.add(new SystemMessage(content));
6464
return this;
6565
}
@@ -70,7 +70,7 @@ public MessageListBuilder system(@NotNull String content) {
7070
* @param content The text content of the assistant message. Cannot be null.
7171
* @return This builder instance.
7272
*/
73-
public MessageListBuilder assistant(@NotNull String content) {
73+
public MessageListBuilder assistant(@NonNull String content) {
7474
this.messages.add(new AssistantMessage(content));
7575
return this;
7676
}
@@ -81,7 +81,7 @@ public MessageListBuilder assistant(@NotNull String content) {
8181
* @param toolCalls The list of tool calls. Cannot be null or empty.
8282
* @return This builder instance.
8383
*/
84-
public MessageListBuilder assistant(@NotNull @NotEmpty List<ToolCall> toolCalls) {
84+
public MessageListBuilder assistant(@NonNull @NotEmpty List<ToolCall> toolCalls) {
8585
this.messages.add(new AssistantMessage(toolCalls));
8686
return this;
8787
}
@@ -92,7 +92,7 @@ public MessageListBuilder assistant(@NotNull @NotEmpty List<ToolCall> toolCalls)
9292
* @param content The text content of the user message. Cannot be null.
9393
* @return This builder instance.
9494
*/
95-
public MessageListBuilder user(@NotNull String content) {
95+
public MessageListBuilder user(@NonNull String content) {
9696
this.messages.add(new UserMessage(content));
9797
return this;
9898
}
@@ -104,7 +104,7 @@ public MessageListBuilder user(@NotNull String content) {
104104
* @param toolCallId The ID of the tool call this message responds to. Can be null.
105105
* @return This builder instance.
106106
*/
107-
public MessageListBuilder tool(@NotNull String content, @Nullable String toolCallId) {
107+
public MessageListBuilder tool(@NonNull String content, @Nullable String toolCallId) {
108108
this.messages.add(new ToolMessage(content, toolCallId));
109109
return this;
110110
}
@@ -117,7 +117,7 @@ public MessageListBuilder tool(@NotNull String content, @Nullable String toolCal
117117
* @param message The ChatMessage object to be added. Cannot be null.
118118
* @return This builder instance.
119119
*/
120-
public MessageListBuilder message(@NotNull ChatMessage message) {
120+
public MessageListBuilder message(@NonNull ChatMessage message) {
121121
this.messages.add(message);
122122
return this;
123123
}

src/main/java/nl/dannyj/mistral/interceptors/MistralHeaderInterceptor.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package nl.dannyj.mistral.interceptors;
1818

1919
import lombok.NonNull;
20-
import nl.dannyj.mistral.MistralClient;
2120
import okhttp3.Interceptor;
2221
import okhttp3.Request;
2322
import okhttp3.Response;
@@ -27,22 +26,22 @@
2726

2827
public class MistralHeaderInterceptor implements Interceptor {
2928

30-
private final MistralClient client;
29+
private final String apiKey;
3130

32-
public MistralHeaderInterceptor(@NonNull MistralClient client) {
33-
this.client = client;
31+
public MistralHeaderInterceptor(@NonNull String apiKey) {
32+
if (apiKey.isBlank()) {
33+
throw new IllegalArgumentException("No API key provided");
34+
}
35+
36+
this.apiKey = apiKey;
3437
}
3538

3639
@NotNull
3740
@Override
38-
public Response intercept(@NotNull Chain chain) throws IOException {
41+
public Response intercept(@NonNull Chain chain) throws IOException {
3942
Request request = chain.request();
4043
Request.Builder newRequestBuilder = request.newBuilder();
4144

42-
if (client.getApiKey() == null || client.getApiKey().isBlank()) {
43-
throw new IllegalArgumentException("No API key provided in MistralClient");
44-
}
45-
4645
if (request.header("Content-Type") == null) {
4746
newRequestBuilder.addHeader("Content-Type", "application/json");
4847
}
@@ -52,7 +51,7 @@ public Response intercept(@NotNull Chain chain) throws IOException {
5251
}
5352

5453
if (request.header("Authorization") == null) {
55-
newRequestBuilder.addHeader("Authorization", "Bearer " + client.getApiKey());
54+
newRequestBuilder.addHeader("Authorization", "Bearer " + this.apiKey);
5655
}
5756

5857
Request newRequest = newRequestBuilder.build();

src/main/java/nl/dannyj/mistral/models/completion/message/AssistantMessage.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
import com.fasterxml.jackson.annotation.JsonProperty;
2121
import jakarta.annotation.Nullable;
2222
import jakarta.validation.constraints.NotEmpty;
23-
import jakarta.validation.constraints.NotNull;
2423
import lombok.EqualsAndHashCode;
2524
import lombok.Getter;
2625
import lombok.NoArgsConstructor;
26+
import lombok.NonNull;
2727
import lombok.Setter;
2828
import nl.dannyj.mistral.models.completion.content.ContentChunk;
2929
import nl.dannyj.mistral.models.completion.content.TextChunk;
@@ -69,7 +69,7 @@ public class AssistantMessage extends ChatMessage {
6969
*
7070
* @param textContent The text content.
7171
*/
72-
public AssistantMessage(@NotNull String textContent) {
72+
public AssistantMessage(@NonNull String textContent) {
7373
this.content = Collections.singletonList(new TextChunk(textContent));
7474
this.toolCalls = null;
7575
}
@@ -79,7 +79,7 @@ public AssistantMessage(@NotNull String textContent) {
7979
*
8080
* @param toolCalls The list of tool calls. Cannot be null or empty.
8181
*/
82-
public AssistantMessage(@NotNull @NotEmpty List<ToolCall> toolCalls) {
82+
public AssistantMessage(@NonNull @NotEmpty List<ToolCall> toolCalls) {
8383
this.content = null;
8484
this.toolCalls = toolCalls;
8585
}

src/main/java/nl/dannyj/mistral/models/completion/message/ChatMessage.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public abstract class ChatMessage {
5252
/**
5353
* The content of the message. Can be null or a list of content chunks.
5454
*
55-
* @param content The list of content chunks, or null.
5655
* @return The list of content chunks, or null.
5756
*/
5857
@Nullable

src/main/java/nl/dannyj/mistral/models/completion/message/ToolMessage.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
import com.fasterxml.jackson.annotation.JsonInclude;
2020
import com.fasterxml.jackson.annotation.JsonProperty;
2121
import jakarta.annotation.Nullable;
22-
import jakarta.validation.constraints.NotNull;
2322
import lombok.EqualsAndHashCode;
2423
import lombok.Getter;
2524
import lombok.NoArgsConstructor;
25+
import lombok.NonNull;
2626
import lombok.Setter;
2727
import nl.dannyj.mistral.models.completion.content.ContentChunk;
2828
import nl.dannyj.mistral.models.completion.content.TextChunk;
@@ -68,7 +68,7 @@ public class ToolMessage extends ChatMessage {
6868
* @param textContent The text content (result) of the tool call. Cannot be null.
6969
* @param toolCallId The ID of the tool call this message responds to. Can be null.
7070
*/
71-
public ToolMessage(@NotNull String textContent, @Nullable String toolCallId) {
71+
public ToolMessage(@NonNull String textContent, @Nullable String toolCallId) {
7272
this.content = Collections.singletonList(new TextChunk(textContent));
7373
this.toolCallId = toolCallId;
7474
}
@@ -79,7 +79,7 @@ public ToolMessage(@NotNull String textContent, @Nullable String toolCallId) {
7979
* @param contentChunks The list of content chunks representing the tool result. Cannot be null or empty.
8080
* @param toolCallId The ID of the tool call this message responds to. Can be null.
8181
*/
82-
public ToolMessage(@NotNull @jakarta.validation.constraints.NotEmpty List<ContentChunk> contentChunks, @Nullable String toolCallId) {
82+
public ToolMessage(@NonNull @jakarta.validation.constraints.NotEmpty List<ContentChunk> contentChunks, @Nullable String toolCallId) {
8383
this.content = contentChunks;
8484
this.toolCallId = toolCallId;
8585
}

src/main/java/nl/dannyj/mistral/models/completion/message/UserMessage.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
package nl.dannyj.mistral.models.completion.message;
1818

1919
import jakarta.validation.constraints.NotEmpty;
20-
import jakarta.validation.constraints.NotNull;
2120
import lombok.EqualsAndHashCode;
2221
import lombok.NoArgsConstructor;
22+
import lombok.NonNull;
2323
import nl.dannyj.mistral.models.completion.content.ContentChunk;
2424
import nl.dannyj.mistral.models.completion.content.TextChunk;
2525

@@ -39,7 +39,7 @@ public class UserMessage extends ChatMessage {
3939
*
4040
* @param textContent The text content for the user message. Cannot be null or empty.
4141
*/
42-
public UserMessage(@NotNull String textContent) {
42+
public UserMessage(@NonNull String textContent) {
4343
if (textContent.isEmpty()) {
4444
throw new IllegalArgumentException("User message text content cannot be empty.");
4545
}
@@ -51,7 +51,7 @@ public UserMessage(@NotNull String textContent) {
5151
*
5252
* @param contentChunks The list of content chunks. Cannot be null or empty.
5353
*/
54-
public UserMessage(@NotNull @NotEmpty List<ContentChunk> contentChunks) {
54+
public UserMessage(@NonNull @NotEmpty List<ContentChunk> contentChunks) {
5555
this.content = contentChunks;
5656
}
5757

src/main/java/nl/dannyj/mistral/serialization/ContentChunkListDeserializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
public class ContentChunkListDeserializer extends StdDeserializer<List<ContentChunk>> implements ContextualDeserializer {
3636

37-
private JsonDeserializer<?> defaultDeserializer;
37+
private transient JsonDeserializer<?> defaultDeserializer;
3838

3939
public ContentChunkListDeserializer() {
4040
this(null);

src/main/java/nl/dannyj/mistral/serialization/ToolChoiceOptionDeserializer.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,8 @@ public ToolChoiceOption deserialize(JsonParser jp, DeserializationContext ctxt)
4141

4242
if (token == JsonToken.VALUE_STRING) {
4343
String enumValue = jp.getText().toUpperCase();
44-
try {
45-
return ToolChoiceEnum.valueOf(enumValue);
46-
} catch (IllegalArgumentException e) {
47-
if ("ANY".equalsIgnoreCase(enumValue)) return ToolChoiceEnum.ANY;
48-
if ("AUTO".equalsIgnoreCase(enumValue)) return ToolChoiceEnum.AUTO;
49-
if ("NONE".equalsIgnoreCase(enumValue)) return ToolChoiceEnum.NONE;
50-
if ("REQUIRED".equalsIgnoreCase(enumValue)) return ToolChoiceEnum.REQUIRED;
51-
52-
throw ctxt.weirdStringException(enumValue, ToolChoiceEnum.class, "Not a valid ToolChoiceEnum value");
53-
}
44+
45+
return ToolChoiceEnum.valueOf(enumValue);
5446
} else if (token == JsonToken.START_OBJECT) {
5547
return mapper.readValue(jp, SpecificToolChoice.class);
5648
}

src/main/java/nl/dannyj/mistral/services/HttpService.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package nl.dannyj.mistral.services;
1818

1919
import lombok.NonNull;
20-
import nl.dannyj.mistral.MistralClient;
2120
import nl.dannyj.mistral.exceptions.MistralAPIException;
2221
import okhttp3.Callback;
2322
import okhttp3.MediaType;
@@ -37,15 +36,15 @@ public class HttpService {
3736

3837
private static final String API_URL = "https://api.mistral.ai/v1";
3938

40-
private final MistralClient client;
39+
private final OkHttpClient httpClient;
4140

4241
/**
43-
* Constructor that initializes the HttpService with a provided MistralClient.
42+
* Constructor that initializes the HttpService with a provided OkHttpClient.
4443
*
45-
* @param client The MistralClient to be used for making requests to the Mistral AI API
44+
* @param httpClient The OkHttpClient to be used for making requests to the Mistral AI API
4645
*/
47-
public HttpService(@NonNull MistralClient client) {
48-
this.client = client;
46+
public HttpService(@NonNull OkHttpClient httpClient) {
47+
this.httpClient = httpClient;
4948
}
5049

5150
/**
@@ -91,7 +90,6 @@ public void streamPost(@NonNull String urlPath, @NonNull String body, Callback c
9190
.url(API_URL + urlPath)
9291
.post(RequestBody.create(body, MediaType.parse("application/json")))
9392
.build();
94-
OkHttpClient httpClient = client.getHttpClient();
9593

9694
httpClient.newCall(request).enqueue(callBack);
9795
}
@@ -104,8 +102,6 @@ public void streamPost(@NonNull String urlPath, @NonNull String body, Callback c
104102
* @throws MistralAPIException If the response is not successful, the response body is null or an IOException occurs in the objectmapper
105103
*/
106104
private String executeRequest(Request request) {
107-
OkHttpClient httpClient = client.getHttpClient();
108-
109105
try (Response response = httpClient.newCall(request).execute()) {
110106
if (!response.isSuccessful()) {
111107
throw new MistralAPIException("Received unexpected response code " + response.code() + ": " + (response.body() != null ? response.body().string() : response));

0 commit comments

Comments
 (0)