Skip to content

Commit

Permalink
HttpClientResponseを扱えるメソッドが定義できるように改修
Browse files Browse the repository at this point in the history
  • Loading branch information
LINYE-MARIANA committed Apr 24, 2024
1 parent 2d21faf commit 428e8a4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
18 changes: 15 additions & 3 deletions example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,25 @@ void main(List<String> args) async {
apiKey: apiKey,
getEndpoint: (String locale) {
return "$host/input_completion/$locale/";
},
handleHttpResponse: (HttpClientResponse response) async {
if (response.statusCode != 200) {
print("Failed to fetch data, ${response.statusCode}");
return [];
}
return null;
});

List<LocaleDataItem> jaData = await fetcher.fetch(locale);
print("Fetched ${jaData.length} items: $jaData");
List<LocaleDataItem> localeData = await fetcher.fetch(locale);
if (localeData.isEmpty) {
print("No data fetched. Exiting.");
return;
}

print("Fetched ${localeData.length} items: $localeData");

Generator generator = Generator();
generator.loadData(locale, jaData);
generator.loadData(locale, localeData);

while (true) {
print("Enter a keyword to get completions:");
Expand Down
29 changes: 18 additions & 11 deletions lib/src/fetcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@ class Fetcher {
final String _httpMethod;
final GetEndpoint? _getEndpoint;
final HandleResponse? _handleResponse;
final HandleHttpResponse? _handleHttpResponse;

Fetcher(
{required String apiKey,
String apiKeyHeaderName = "X-Secret-Key",
String httpMethod = "GET",
GetEndpoint? getEndpoint,
HandleResponse? handleResponse = _defaultHandleResponse})
HandleResponse? handleResponse = _defaultHandleResponse,
HandleHttpResponse? handleHttpResponse})
: _apiKey = apiKey,
_apiKeyHeaderName = apiKeyHeaderName,
_httpMethod = httpMethod,
_getEndpoint = getEndpoint,
_handleResponse = handleResponse;
_handleResponse = handleResponse,
_handleHttpResponse = handleHttpResponse;

static List<LocaleDataItem> _defaultHandleResponse(String responseBody) {
List<LocaleDataItem> results = [];
Expand All @@ -39,14 +42,6 @@ class Fetcher {
String endpoint = _getEndpoint!(locale);
Map<String, String> headers = {_apiKeyHeaderName: _apiKey};

dynamic response = await _fetch(endpoint, headers);
return _handleResponse!(response);
}

Future<dynamic> _fetch(String endpoint, Map<String, String> headers) async {
// HTTPRequestでデータを取得する
String? responseBody;

HttpClient client = HttpClient();
HttpClientRequest request =
await client.openUrl(_httpMethod, Uri.parse(endpoint));
Expand All @@ -55,11 +50,23 @@ class Fetcher {
});
HttpClientResponse response = await request.close();

if (_handleHttpResponse != null) {
// HttpClientResponseを任意に扱うメソッドの定義があればそれを使う

List<LocaleDataItem>? handled = await _handleHttpResponse(response);
if (handled != null) {
// レスポンスを処理した結果があればそれを返す、なければデフォルトの処理を行う
client.close(force: true);
return handled;
}
}

String responseBody;
try {
responseBody = await response.transform(utf8.decoder).join();
} finally {
client.close();
}
return responseBody;
return _handleResponse!(responseBody);
}
}
4 changes: 4 additions & 0 deletions lib/src/types.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:io';

class LocaleDataItem {
String text;
String keywords;
Expand Down Expand Up @@ -52,3 +54,5 @@ typedef LocaleDataFilter = List<MatchedResultData> Function(

typedef GetEndpoint = String Function(String locale);
typedef HandleResponse = List<LocaleDataItem> Function(String responseBody);
typedef HandleHttpResponse = Future<List<LocaleDataItem>?> Function(
HttpClientResponse response);

0 comments on commit 428e8a4

Please sign in to comment.