From f837ee0c57247923ce99660690e7a8df32a672a5 Mon Sep 17 00:00:00 2001 From: FireLord Date: Wed, 22 May 2024 18:11:55 +0530 Subject: [PATCH] data: Remove API interface --- lib/data/api/NewsAPIService.dart | 44 ++++++++++++++++++++++++-- lib/data/api/NewsAPIServiceImpl.dart | 46 ---------------------------- lib/injection_container.dart | 3 +- 3 files changed, 42 insertions(+), 51 deletions(-) delete mode 100644 lib/data/api/NewsAPIServiceImpl.dart diff --git a/lib/data/api/NewsAPIService.dart b/lib/data/api/NewsAPIService.dart index 476b470..4938634 100644 --- a/lib/data/api/NewsAPIService.dart +++ b/lib/data/api/NewsAPIService.dart @@ -1,6 +1,44 @@ +import 'dart:convert'; +import 'package:http/http.dart'; import 'package:sample_news/data/model/Article.dart'; -import 'package:sample_news/data/util/Resource.dart'; +import 'package:sample_news/data/util/Constant.dart'; +import '../util/Resource.dart'; -abstract class NewsAPIService { - Future>> getNews(); +class NewsAPIService { + final Client client; + + NewsAPIService(this.client); + + Future>> getNews() async { + try { + Map queryParams = { + 'country': 'in', + 'apiKey': API_KEY, + }; + + var uri = Uri( + scheme: 'https', + host: API_URL, + path: '/v2/top-headlines', + queryParameters: queryParams, + ); + + var response = await client.get(uri); + + if (response.statusCode == 200) { + var data = response.body; + final Map parsed = json.decode(data); + List
articles = (parsed['articles'] as List) + .map((article) => Article.fromJson(article)) + .toList(); + return Success(articles); + } else { + return Failure('Failed to load news: ${response.statusCode}', null); + } + } catch (e) { + return Failure('Error fetching news: $e', null); + } finally { + client.close(); + } + } } \ No newline at end of file diff --git a/lib/data/api/NewsAPIServiceImpl.dart b/lib/data/api/NewsAPIServiceImpl.dart deleted file mode 100644 index 4c4c76d..0000000 --- a/lib/data/api/NewsAPIServiceImpl.dart +++ /dev/null @@ -1,46 +0,0 @@ -import 'dart:convert'; -import 'package:http/http.dart'; -import 'package:sample_news/data/model/Article.dart'; -import 'package:sample_news/data/util/Constant.dart'; -import '../util/Resource.dart'; -import 'NewsAPIService.dart'; - -class NewsAPIServiceImpl implements NewsAPIService{ - final Client client; - - NewsAPIServiceImpl(this.client); - - @override - Future>> getNews() async { - try { - Map queryParams = { - 'country': 'in', - 'apiKey': API_KEY, - }; - - var uri = Uri( - scheme: 'https', - host: API_URL, - path: '/v2/top-headlines', - queryParameters: queryParams, - ); - - var response = await client.get(uri); - - if (response.statusCode == 200) { - var data = response.body; - final Map parsed = json.decode(data); - List
articles = (parsed['articles'] as List) - .map((article) => Article.fromJson(article)) - .toList(); - return Success(articles); - } else { - return Failure('Failed to load news: ${response.statusCode}', null); - } - } catch (e) { - return Failure('Error fetching news: $e', null); - } finally { - client.close(); - } - } -} \ No newline at end of file diff --git a/lib/injection_container.dart b/lib/injection_container.dart index 20330c4..99edf48 100644 --- a/lib/injection_container.dart +++ b/lib/injection_container.dart @@ -1,7 +1,6 @@ import 'package:get_it/get_it.dart'; import 'package:http/http.dart'; import 'package:sample_news/data/api/NewsAPIService.dart'; -import 'package:sample_news/data/api/NewsAPIServiceImpl.dart'; import 'package:sample_news/data/repository/dataSource/NewsRemoteDataSource.dart'; import 'package:sample_news/data/repository/dataSourceImpl/NewsRemoteDataSourceImpl.dart'; import 'package:sample_news/domain/repository/NewsRepository.dart'; @@ -15,7 +14,7 @@ Future initDependency() async { sl.registerSingleton(Client()); // NewsAPIService as Singleton - sl.registerSingleton(NewsAPIServiceImpl(sl())); + sl.registerSingleton(NewsAPIService(sl())); // NewsRemoteDataSourceImpl as Singleton sl.registerSingleton(NewsRemoteDataSourceImpl(sl()));