Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/2/protocols/http/constructor/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ Use this constructor to create a new instance of the `Http` protocol with specif
## Arguments

```dart
HttpProtocol(Uri);
HttpProtocol(Uri, {bool acceptBadCertificate = false});
```

<br/>

| Argument | Type | Description |
| --------- | ----------------- | ---------------------------- |
| `uri` | <pre>Uri</pre> | URI pointing to a Kuzzle server. See more: [https://api.dart.dev/stable/2.10.5/dart-core/Uri-class.html](https://api.dart.dev/stable/2.10.5/dart-core/Uri-class.html) |
| `acceptBadCertificate` | <pre>bool(false)</pre> | Accept or not bad certificate when using https


## Return
Expand Down
17 changes: 13 additions & 4 deletions lib/src/protocols/http.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
import 'dart:convert';
import 'dart:io';

import 'package:http/io_client.dart';
import 'package:kuzzle/src/kuzzle/request.dart';
import 'package:kuzzle/src/kuzzle/response.dart';
import 'package:kuzzle/src/protocols/abstract.dart';
import 'package:http/http.dart' as http;

class HttpProtocol extends KuzzleProtocol {
HttpProtocol(Uri uri) : super(uri);
HttpProtocol(Uri uri, {bool acceptBadCertificate = false}) : super(uri) {
_client = HttpClient()
..badCertificateCallback =
((cert, host, port) => acceptBadCertificate);
_ioClient = IOClient(_client);
}

HttpClient _client;
IOClient _ioClient;

@override
Future<void> connect() async {
await super.connect();

final res = await http.get('${uri.toString()}/_query');
final res = await _ioClient.get('${uri.toString()}/_query');
if (res.statusCode == 401 || res.statusCode == 403) {
return Future.error('You must have permission on the _query route.');
}
Expand All @@ -32,7 +41,7 @@ class HttpProtocol extends KuzzleProtocol {
headers['x-kuzzle-volatile'] = jsonEncode(request.volatile);
}

final res = await http.post(
final res = await _ioClient.post(
'${uri.toString()}/_query',
headers: headers,
body: jsonEncode(request),
Expand Down