From 428e8a42579b5e4e30f57422b05c0411f2237419 Mon Sep 17 00:00:00 2001 From: LIN YE Date: Wed, 24 Apr 2024 10:24:57 +0900 Subject: [PATCH] =?UTF-8?q?HttpClientResponse=E3=82=92=E6=89=B1=E3=81=88?= =?UTF-8?q?=E3=82=8B=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89=E3=81=8C=E5=AE=9A?= =?UTF-8?q?=E7=BE=A9=E3=81=A7=E3=81=8D=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= =?UTF-8?q?=E6=94=B9=E4=BF=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/example.dart | 18 +++++++++++++++--- lib/src/fetcher.dart | 29 ++++++++++++++++++----------- lib/src/types.dart | 4 ++++ 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/example/example.dart b/example/example.dart index 953abcb..f1ebf98 100644 --- a/example/example.dart +++ b/example/example.dart @@ -26,13 +26,25 @@ void main(List 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 jaData = await fetcher.fetch(locale); - print("Fetched ${jaData.length} items: $jaData"); + List 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:"); diff --git a/lib/src/fetcher.dart b/lib/src/fetcher.dart index ae74699..2d0974c 100644 --- a/lib/src/fetcher.dart +++ b/lib/src/fetcher.dart @@ -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 _defaultHandleResponse(String responseBody) { List results = []; @@ -39,14 +42,6 @@ class Fetcher { String endpoint = _getEndpoint!(locale); Map headers = {_apiKeyHeaderName: _apiKey}; - dynamic response = await _fetch(endpoint, headers); - return _handleResponse!(response); - } - - Future _fetch(String endpoint, Map headers) async { - // HTTPRequestでデータを取得する - String? responseBody; - HttpClient client = HttpClient(); HttpClientRequest request = await client.openUrl(_httpMethod, Uri.parse(endpoint)); @@ -55,11 +50,23 @@ class Fetcher { }); HttpClientResponse response = await request.close(); + if (_handleHttpResponse != null) { + // HttpClientResponseを任意に扱うメソッドの定義があればそれを使う + + List? 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); } } diff --git a/lib/src/types.dart b/lib/src/types.dart index 6635371..c48cd53 100644 --- a/lib/src/types.dart +++ b/lib/src/types.dart @@ -1,3 +1,5 @@ +import 'dart:io'; + class LocaleDataItem { String text; String keywords; @@ -52,3 +54,5 @@ typedef LocaleDataFilter = List Function( typedef GetEndpoint = String Function(String locale); typedef HandleResponse = List Function(String responseBody); +typedef HandleHttpResponse = Future?> Function( + HttpClientResponse response);