Skip to content

Commit a52eabf

Browse files
Merge #129
129: Feature/Analytics r=brunoocasali a=brunoocasali - Expose headers method - Create a `Version` class to easily access package version data. - Add `Version.qualifiedVersion` to headers After the implementation the MeiliSearch server is outputting the expected 💯 `[2022-01-19T18:33:47Z INFO actix_web::middleware::logger] 172.17.0.1:57704 "GET /keys HTTP/1.1" 200 14 "-" "MeiliSearch Dart (v0.5.0)" 0.000118` _DISCLAIMER:_ I've tried other ways to collect these data eg. - Reading from pubspec.yaml: - This is not supported by dart native and flutter in the same way. - Generating the version class dynamically: - Due to the time constraint I was not able to do this, actually, I don’t know for sure if this is going to work exactly the same way as it is now. Add dart support as requested here meilisearch/integration-guides#150 Co-authored-by: Bruno Casali <[email protected]>
2 parents 35586f5 + 65a882e commit a52eabf

File tree

6 files changed

+68
-2
lines changed

6 files changed

+68
-2
lines changed

.github/scripts/check-release.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,22 @@ file1='pubspec.yaml'
77
file2='example/pubspec.yaml'
88
file3='example/pubspec.lock'
99
file4='README.md'
10+
file5='lib/src/version.dart'
1011
changelog_file='CHANGELOG.md'
1112
ret=0
1213

1314
file_tag1=$(grep '^version: ' $file1 | cut -d ':' -f 2 | tr -d ' ')
1415
file_tag2=$(grep 'meilisearch: "' $file2 | cut -d ':' -f 2 | tr -d '"' | tr -d ' ')
1516
file_tag3=$(grep 'meilisearch' -A 6 $file3 | grep 'version: ' | cut -d ':' -f2 | tr -d '"' | tr -d ' ')
1617
file_tag4=$(grep 'meilisearch: ' $file4 | cut -d '^' -f2)
17-
if [ "$current_tag" != "$file_tag1" ] || [ "$current_tag" != "$file_tag2" ] || [ "$current_tag" != "$file_tag3" ] || [ "$current_tag" != "$file_tag4" ]; then
18+
file_tag5=$(grep -o "[0-9\.]" $file5 | tr -d '[:space:]')
19+
if [ "$current_tag" != "$file_tag1" ] || [ "$current_tag" != "$file_tag2" ] || [ "$current_tag" != "$file_tag3" ] || [ "$current_tag" != "$file_tag4" ] || [ "$current_tag" != "$file_tag5" ]; then
1820
echo "Error: the current tag does not match the version in package file(s)."
1921
echo "$file1: found $file_tag1 - expected $current_tag"
2022
echo "$file2: found $file_tag2 - expected $current_tag"
2123
echo "$file3: found $file_tag3 - expected $current_tag"
22-
echo "$file4: found $file_tag4 - expected $round_current_tag"
24+
echo "$file4: found $file_tag4 - expected $current_tag"
25+
echo "$file5: found $file_tag5 - expected $current_tag"
2326
ret=1
2427
fi
2528

CONTRIBUTING.md

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

106106
Make a PR modifying the version in:
107107

108+
- the file [`lib/src/version.dart:2`](./lib/src/version.dart).
109+
110+
```dart
111+
static const String current = 'X.X.X';
112+
```
113+
108114
- the file [`pubspec.yaml`](./pubspec.yaml).
109115

110116
```yaml

lib/src/http_request.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ abstract class HttpRequest {
1414
/// Timeout in milliseconds for opening a url.
1515
int? get connectTimeout;
1616

17+
/// Retrieve all headers used when Http calls are made.
18+
Map<String, dynamic> headers();
19+
1720
/// GET method
1821
Future<Response<T>> getMethod<T>(
1922
String path, {

lib/src/http_request_impl.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:dio/dio.dart';
2+
import 'package:meilisearch/src/version.dart';
23
import 'http_request.dart';
34
import 'exception.dart';
45

@@ -9,6 +10,7 @@ class HttpRequestImpl implements HttpRequest {
910
headers: <String, dynamic>{
1011
if (apiKey != null) 'Authorization': 'Bearer ${apiKey}',
1112
'Content-Type': 'application/json',
13+
'User-Agent': Version.qualifiedVersion,
1214
},
1315
responseType: ResponseType.json,
1416
connectTimeout: connectTimeout ?? 0,
@@ -25,6 +27,11 @@ class HttpRequestImpl implements HttpRequest {
2527

2628
final Dio dio;
2729

30+
@override
31+
Map<String, dynamic> headers() {
32+
return this.dio.options.headers;
33+
}
34+
2835
@override
2936
Future<Response<T>> getMethod<T>(
3037
String path, {

lib/src/version.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Version {
2+
static const String current = '0.5.0';
3+
4+
static String get qualifiedVersion {
5+
return "MeiliSearch Dart (v${current})";
6+
}
7+
}

test/analytics_test.dart

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import 'dart:io';
2+
3+
import 'package:meilisearch/src/version.dart';
4+
import 'package:test/test.dart';
5+
6+
import 'utils/client.dart';
7+
8+
void main() {
9+
final RegExp semVer = new RegExp(
10+
r"version\:.(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?");
11+
12+
group('Version', () {
13+
test('matches with the current package version in pubspec.yaml', () {
14+
final path = '${Directory.current.path}/pubspec.yaml';
15+
String data = new File(path).readAsStringSync();
16+
String? version = semVer.stringMatch(data)?.replaceFirst('version: ', '');
17+
18+
expect(version, isNotNull);
19+
expect(Version.current, isNotNull);
20+
expect(Version.current, equals(version));
21+
});
22+
});
23+
24+
group('Analytics', () {
25+
setUpClient();
26+
27+
test('sends the User-Agent header in every call', () {
28+
final headers = client.http.headers();
29+
30+
expect(headers.keys, contains('User-Agent'));
31+
expect(headers['User-Agent'], isNotNull);
32+
});
33+
34+
test('has current version data from Version class', () {
35+
final headers = client.http.headers();
36+
37+
expect(headers['User-Agent'], equals(Version.qualifiedVersion));
38+
});
39+
});
40+
}

0 commit comments

Comments
 (0)