Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HttpClientResponseを扱えるメソッドが定義できるように改修 #1

Closed
wants to merge 1 commit into from
Closed
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
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);