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 84efdab
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 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) {
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
33 changes: 20 additions & 13 deletions lib/src/fetcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,25 @@ 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) {
static List<LocaleDataItem> _defaultHandleResponse(dynamic responseBody) {
List<LocaleDataItem> results = [];
dynamic data = json.decode(responseBody);
dynamic data = json.decode(responseBody as String);
if (data['user_says'] != null && data['user_says'] is List) {
data['user_says'].forEach((item) {
results.add(LocaleDataItem(
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 = _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);
}
}
6 changes: 5 additions & 1 deletion 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 @@ -51,4 +53,6 @@ typedef LocaleDataFilter = List<MatchedResultData> Function(
List<LocaleDataItem> localeData, String input, String locale);

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

0 comments on commit 84efdab

Please sign in to comment.