Skip to content

Commit 14608ee

Browse files
committed
Expose headers method & Add qualifiedVersion to headers
1 parent d187845 commit 14608ee

File tree

6 files changed

+97
-0
lines changed

6 files changed

+97
-0
lines changed

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) 'X-Meili-API-Key': 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, {

pubspec.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ dependencies:
1111

1212
dev_dependencies:
1313
test: ^1.0.0
14+
15+
assets:
16+
- pubspec.yaml

test/analytics_test.dart

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
group('Version', () {
10+
test('extracts current version from yaml', () {
11+
final path = '${Directory.current.path}/test/fixtures/fake_spec.yaml';
12+
13+
final version = Version.current(path);
14+
15+
expect(version, equals('0.9.1'));
16+
});
17+
18+
test("responds with invalid when can't read file", () {
19+
final path = '${Directory.current.path}/test/fixtures/invalid_spec.yaml';
20+
21+
final version = Version.current(path);
22+
23+
expect(version, equals('invalid'));
24+
});
25+
26+
test('gets current package version', () {
27+
final path = '${Directory.current.path}/pubspec.yaml';
28+
String pubspec = new File(path).readAsStringSync();
29+
30+
final version = Version.current(path);
31+
32+
expect(pubspec, contains(version));
33+
});
34+
});
35+
36+
group('Analytics', () {
37+
setUpClient();
38+
39+
test('sends the User-Agent header in every call', () {
40+
final headers = client.http.headers();
41+
42+
expect(headers.keys, contains('User-Agent'));
43+
expect(headers['User-Agent'], isNotNull);
44+
});
45+
46+
test('has current version from pubspec.yaml', () {
47+
final headers = client.http.headers();
48+
49+
expect(headers['User-Agent'], equals(Version.qualifiedVersion));
50+
});
51+
});
52+
}

test/fixtures/fake_spec.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: meilisearch
2+
description: MeiliSearch Dart is the MeiliSearch API client for Dart and Flutter developers.
3+
version: 0.9.1
4+
homepage: https://github.com/meilisearch/meilisearch-dart
5+
6+
environment:
7+
sdk: ">=2.12.0 <3.0.0"
8+
9+
dependencies:
10+
dio: ^4.0.0
11+
12+
dev_dependencies:
13+
test: ^1.0.0
14+
15+
assets:
16+
- pubspec.yaml

test/fixtures/invalid_spec.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: meilisearch
2+
description: MeiliSearch Dart is the MeiliSearch API client for Dart and Flutter developers.
3+
version: invalid-0.version.1
4+
homepage: https://github.com/meilisearch/meilisearch-dart
5+
6+
environment:
7+
sdk: ">=2.12.0 <3.0.0"
8+
9+
dependencies:
10+
dio: ^4.0.0
11+
12+
dev_dependencies:
13+
test: ^1.0.0
14+
15+
assets:
16+
- pubspec.yaml

0 commit comments

Comments
 (0)