Skip to content

Commit 5d3c533

Browse files
bors[bot]brunoocasalialallema
authored
Merge #503
503: Feature/Analytics r=alallema a=brunoocasali What this PR does: - Add a new item to be changed when we create a new release of this SDK (sorry about that `@alallema).` - Add the user-agent headers through the Client/Config. Add this disclaimer to the release: ## 🚀 Enhancements * Add User-Agent header to have analytics in every HTTP request (#503) `@brunoocasali` Analytics is enabled by default in the server, but you can disable them by following [this guide](https://docs.meilisearch.com/learn/what_is_meilisearch/telemetry.html#how-to-disable-data-collection) Also, of course, every analytics data we collect are **ANONYMOUS** [read the guide for more information](https://docs.meilisearch.com/learn/what_is_meilisearch/telemetry.html). Thanks again to `@brunoocasali!` 🎉 Co-authored-by: Bruno Casali <[email protected]> Co-authored-by: Amélie <[email protected]>
2 parents 0d3428e + c8c63e9 commit 5d3c533

File tree

7 files changed

+97
-12
lines changed

7 files changed

+97
-12
lines changed

CONTRIBUTING.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ _[Read more about this](https://github.com/meilisearch/integration-guides/blob/m
115115

116116
Make a PR modifying the following files with the right version:
117117

118+
- [`Version.java`](/src/main/java/com/meilisearch/sdk/Version.java)
119+
120+
```java
121+
VERSION = "X.X.X";
122+
```
123+
118124
- [`build.gradle`](/build.gradle)
119125

120126
```java

src/main/java/com/meilisearch/sdk/Config.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
import com.meilisearch.sdk.json.GsonJsonHandler;
44
import com.meilisearch.sdk.json.JsonHandler;
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.HashMap;
8+
import java.util.List;
9+
import java.util.Map;
510
import lombok.Getter;
611
import lombok.Setter;
712

@@ -12,6 +17,7 @@ public class Config {
1217
protected final String hostUrl;
1318
protected final String apiKey;
1419
protected final HttpClient httpClient;
20+
protected final Map<String, String> headers;
1521
protected JsonHandler jsonHandler;
1622

1723
/**
@@ -24,14 +30,30 @@ public Config(String hostUrl) {
2430
}
2531

2632
/**
27-
* Creates a configuration with an API key
33+
* Creates a configuration with an API key and no customized headers.
2834
*
2935
* @param hostUrl URL of the Meilisearch instance
3036
* @param apiKey API key to pass to the header of requests sent to Meilisearch
3137
*/
3238
public Config(String hostUrl, String apiKey) {
3339
this.hostUrl = hostUrl;
3440
this.apiKey = apiKey;
41+
this.headers = configHeaders(new String[0]);
42+
this.jsonHandler = new GsonJsonHandler();
43+
this.httpClient = new HttpClient(this);
44+
}
45+
46+
/**
47+
* Creates a configuration with an API key
48+
*
49+
* @param hostUrl URL of the Meilisearch instance
50+
* @param apiKey API key to pass to the header of requests sent to Meilisearch
51+
* @param clientAgents List of customized agents to be passed to User-Agent header.
52+
*/
53+
public Config(String hostUrl, String apiKey, String[] clientAgents) {
54+
this.hostUrl = hostUrl;
55+
this.apiKey = apiKey;
56+
this.headers = configHeaders(clientAgents);
3557
this.jsonHandler = new GsonJsonHandler();
3658
this.httpClient = new HttpClient(this);
3759
}
@@ -44,4 +66,14 @@ public Config(String hostUrl, String apiKey) {
4466
public String getBearerApiKey() {
4567
return "Bearer " + apiKey;
4668
}
69+
70+
private Map<String, String> configHeaders(String[] clientAgents) {
71+
List<String> list = new ArrayList<String>(Arrays.asList(clientAgents));
72+
list.add(0, Version.getQualifiedVersion());
73+
74+
Map<String, String> data = new HashMap<>();
75+
data.put("User-Agent", String.join(";", list));
76+
77+
return data;
78+
}
4779
}

src/main/java/com/meilisearch/sdk/HttpClient.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
import com.meilisearch.sdk.json.GsonJsonHandler;
1313
import com.meilisearch.sdk.json.JsonHandler;
1414
import java.util.Collections;
15+
import java.util.Map;
1516

1617
/** HTTP client used for API calls to Meilisearch */
1718
public class HttpClient {
1819
private final CustomOkHttpClient client;
1920
private final BasicRequest request;
2021
private final BasicResponse response;
22+
private final Map<String, String> headers;
2123
protected final JsonHandler jsonHandler;
2224

2325
/**
@@ -28,6 +30,7 @@ public class HttpClient {
2830
public HttpClient(Config config) {
2931
this.client = new CustomOkHttpClient(config);
3032
this.jsonHandler = config.jsonHandler;
33+
this.headers = config.headers;
3134
this.request = new BasicRequest(jsonHandler);
3235
this.response = new BasicResponse(jsonHandler);
3336
}
@@ -41,6 +44,7 @@ public HttpClient(Config config) {
4144
public HttpClient(CustomOkHttpClient client, BasicRequest request) {
4245
this.client = client;
4346
this.request = request;
47+
this.headers = Collections.<String, String>emptyMap();
4448
this.jsonHandler = new GsonJsonHandler();
4549
this.response = new BasicResponse(jsonHandler);
4650
}
@@ -67,8 +71,7 @@ <T> T get(String api, Class<T> targetClass, Class<?>... parameters)
6771
*/
6872
<T> T get(String api, String param, Class<T> targetClass, Class<?>... parameters)
6973
throws MeilisearchException {
70-
HttpRequest requestConfig =
71-
request.create(HttpMethod.GET, api + param, Collections.emptyMap(), null);
74+
HttpRequest requestConfig = request.create(HttpMethod.GET, api + param, this.headers, null);
7275
HttpResponse<T> httpRequest = this.client.get(requestConfig);
7376
HttpResponse<T> httpResponse = response.create(httpRequest, targetClass, parameters);
7477

@@ -88,8 +91,7 @@ <T> T get(String api, String param, Class<T> targetClass, Class<?>... parameters
8891
* @throws MeilisearchException if the response is an error
8992
*/
9093
<S, T> T post(String api, S body, Class<T> targetClass) throws MeilisearchException {
91-
HttpRequest requestConfig =
92-
request.create(HttpMethod.POST, api, Collections.emptyMap(), body);
94+
HttpRequest requestConfig = request.create(HttpMethod.POST, api, this.headers, body);
9395
HttpResponse<T> httpRequest = this.client.post(requestConfig);
9496
HttpResponse<T> httpResponse = response.create(httpRequest, targetClass);
9597

@@ -109,8 +111,7 @@ <S, T> T post(String api, S body, Class<T> targetClass) throws MeilisearchExcept
109111
* @throws MeilisearchException if the response is an error
110112
*/
111113
<S, T> T put(String api, S body, Class<T> targetClass) throws MeilisearchException {
112-
HttpRequest requestConfig =
113-
request.create(HttpMethod.PUT, api, Collections.emptyMap(), body);
114+
HttpRequest requestConfig = request.create(HttpMethod.PUT, api, this.headers, body);
114115
HttpResponse<T> httpRequest = this.client.put(requestConfig);
115116
HttpResponse<T> httpResponse = response.create(httpRequest, targetClass);
116117

@@ -130,8 +131,7 @@ <S, T> T put(String api, S body, Class<T> targetClass) throws MeilisearchExcepti
130131
* @throws MeilisearchException if the response is an error
131132
*/
132133
<S, T> T patch(String api, S body, Class<T> targetClass) throws MeilisearchException {
133-
HttpRequest requestConfig =
134-
request.create(HttpMethod.PATCH, api, Collections.emptyMap(), body);
134+
HttpRequest requestConfig = request.create(HttpMethod.PATCH, api, this.headers, body);
135135
HttpResponse<T> httpRequest = this.client.patch(requestConfig);
136136
HttpResponse<T> httpResponse = response.create(httpRequest, targetClass);
137137

@@ -150,8 +150,7 @@ <S, T> T patch(String api, S body, Class<T> targetClass) throws MeilisearchExcep
150150
* @throws MeilisearchException if the response is an error
151151
*/
152152
<T> T delete(String api, Class<T> targetClass) throws MeilisearchException {
153-
HttpRequest requestConfig =
154-
request.create(HttpMethod.DELETE, api, Collections.emptyMap(), null);
153+
HttpRequest requestConfig = request.create(HttpMethod.DELETE, api, this.headers, null);
155154
HttpResponse<T> httpRequest = this.client.delete(requestConfig);
156155
HttpResponse<T> httpResponse = response.create(httpRequest, targetClass);
157156

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.meilisearch.sdk;
2+
3+
public class Version {
4+
static final String VERSION = "0.8.0";
5+
6+
public static String getQualifiedVersion() {
7+
return "Meilisearch Java (v" + VERSION + ")";
8+
}
9+
}

src/main/java/com/meilisearch/sdk/http/CustomOkHttpClient.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,14 @@ private Request buildRequest(HttpRequest request) throws MalformedURLException {
5555
URL url = new URL(this.config.getHostUrl() + request.getPath());
5656
Request.Builder builder = new Request.Builder();
5757
builder.url(url);
58+
5859
if (this.config.getApiKey() != null)
5960
builder.addHeader("Authorization", this.config.getBearerApiKey());
61+
62+
for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
63+
builder.addHeader(entry.getKey(), entry.getValue());
64+
}
65+
6066
switch (request.getMethod()) {
6167
case GET:
6268
builder.get();

src/main/java/com/meilisearch/sdk/http/request/HttpRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
public class HttpRequest {
1111
@Setter private HttpMethod method;
1212
@Setter private String path;
13-
@Setter private Map<String, String> headers;
13+
@Getter private Map<String, String> headers;
1414
private String content;
1515

1616
public HttpRequest() {}

src/test/java/com/meilisearch/sdk/http/CustomOkHttpClientTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.meilisearch.sdk.http;
22

33
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.containsString;
45
import static org.hamcrest.Matchers.equalTo;
56
import static org.hamcrest.Matchers.notNullValue;
67
import static org.mockito.ArgumentMatchers.any;
78
import static org.mockito.Mockito.*;
89

910
import com.meilisearch.sdk.Config;
11+
import com.meilisearch.sdk.Version;
1012
import com.meilisearch.sdk.http.request.HttpMethod;
1113
import com.meilisearch.sdk.http.request.HttpRequest;
1214
import com.meilisearch.sdk.http.response.HttpResponse;
@@ -15,6 +17,8 @@
1517
import java.io.InputStreamReader;
1618
import java.util.ArrayDeque;
1719
import java.util.Collections;
20+
import java.util.HashMap;
21+
import java.util.Map;
1822
import java.util.stream.Collectors;
1923
import okhttp3.*;
2024
import okhttp3.internal.connection.RealCall;
@@ -219,4 +223,33 @@ void deleteWithoutBody() throws Exception {
219223
expectedRequest.url().toString(),
220224
equalTo(this.config.getHostUrl() + request.getPath()));
221225
}
226+
227+
@Test
228+
void requestWithHeaders() throws Exception {
229+
Map<String, String> headers = new HashMap<String, String>();
230+
headers.put("User-Agent", "Meilisearch (v1.2.3)");
231+
232+
HttpRequest request = new HttpRequest(HttpMethod.DELETE, "/test", headers, null);
233+
HttpResponse<Object> response = classToTest.delete(request);
234+
235+
Request expectedRequest = requestQueue.poll();
236+
assertThat(expectedRequest, notNullValue());
237+
assertThat(expectedRequest.headers().toString(), containsString("Meilisearch (v1.2.3)"));
238+
}
239+
240+
@Test
241+
void defaultConfigHasAnalytics() throws Exception {
242+
assertThat(
243+
config.getHeaders().get("User-Agent").toString(),
244+
containsString(Version.getQualifiedVersion()));
245+
}
246+
247+
@Test
248+
void customConfigWithCustomAnalyticsHasBoth() throws Exception {
249+
Config newConfig = new Config("host", "key", new String[] {"MyApp v1.44"});
250+
251+
assertThat(
252+
newConfig.getHeaders().get("User-Agent").toString(),
253+
containsString(Version.getQualifiedVersion() + ";MyApp v1.44"));
254+
}
222255
}

0 commit comments

Comments
 (0)