From 84efdab7df205b6ee2562e286cdab9282277e1a7 Mon Sep 17 00:00:00 2001 From: LIN YE Date: Wed, 24 Apr 2024 09:49:40 +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 | 33 ++++++++++++++++++++------------- lib/src/types.dart | 6 +++++- 3 files changed, 40 insertions(+), 17 deletions(-) diff --git a/example/example.dart b/example/example.dart index 953abcb..c2d58ad 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) { + 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..7ec9215 100644 --- a/lib/src/fetcher.dart +++ b/lib/src/fetcher.dart @@ -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 _defaultHandleResponse(String responseBody) { + static List _defaultHandleResponse(dynamic responseBody) { List 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( @@ -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 = _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..0f6bb84 100644 --- a/lib/src/types.dart +++ b/lib/src/types.dart @@ -1,3 +1,5 @@ +import 'dart:io'; + class LocaleDataItem { String text; String keywords; @@ -51,4 +53,6 @@ typedef LocaleDataFilter = List Function( List localeData, String input, String locale); typedef GetEndpoint = String Function(String locale); -typedef HandleResponse = List Function(String responseBody); +typedef HandleResponse = List Function(dynamic responseBody); +typedef HandleHttpResponse = List? Function( + HttpClientResponse response);