Skip to content

Commit

Permalink
data: Remove API interface
Browse files Browse the repository at this point in the history
  • Loading branch information
FireLord committed May 22, 2024
1 parent 475749c commit f837ee0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 51 deletions.
44 changes: 41 additions & 3 deletions lib/data/api/NewsAPIService.dart
Original file line number Diff line number Diff line change
@@ -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<Resource<List<Article>>> getNews();
class NewsAPIService {
final Client client;

NewsAPIService(this.client);

Future<Resource<List<Article>>> getNews() async {
try {
Map<String, String> 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<String, dynamic> parsed = json.decode(data);
List<Article> 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();
}
}
}
46 changes: 0 additions & 46 deletions lib/data/api/NewsAPIServiceImpl.dart

This file was deleted.

3 changes: 1 addition & 2 deletions lib/injection_container.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -15,7 +14,7 @@ Future<void> initDependency() async {
sl.registerSingleton<Client>(Client());

// NewsAPIService as Singleton
sl.registerSingleton<NewsAPIService>(NewsAPIServiceImpl(sl()));
sl.registerSingleton(NewsAPIService(sl()));

// NewsRemoteDataSourceImpl as Singleton
sl.registerSingleton<NewsRemoteDataSource>(NewsRemoteDataSourceImpl(sl()));
Expand Down

0 comments on commit f837ee0

Please sign in to comment.