|
| 1 | +A Dart package that provides a repository for managing news headlines data. |
| 2 | +It abstracts the data source and provides a clean API for |
| 3 | +fetching, creating, updating, deleting, and searching headlines. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Fetch Headlines:** Retrieve a paginated list of news headlines. Supports optional filtering by category, source, and event country. |
| 8 | +- **Get Headline by ID:** Fetch a specific headline by its unique identifier. |
| 9 | +- **Create Headline:** Add a new headline to the data source. |
| 10 | +- **Update Headline:** Modify an existing headline in the data source. |
| 11 | +- **Delete Headline:** Remove a headline from the data source. |
| 12 | +- **Search Headlines:** Search for headlines based on a query string. |
| 13 | +- **Stream Headlines:** Get a stream of headlines that updates periodically. |
| 14 | +- **Error Handling:** Provides custom exceptions for specific error scenarios |
| 15 | + (e.g., `HeadlinesFetchException`, `HeadlineNotFoundException`, `HeadlineCreateException`, `HeadlineUpdateException`, `HeadlineDeleteException`, `HeadlinesSearchException`). |
| 16 | + |
| 17 | +## Getting started |
| 18 | + |
| 19 | +To use this package, add `ht_headlines_repository` as a dependency in your `pubspec.yaml` file. |
| 20 | + |
| 21 | +```yaml |
| 22 | +dependencies: |
| 23 | + ht_headlines_repository: |
| 24 | + git: |
| 25 | + url: https://github.com/Headlines-Toolkit/ht-headlines-client.git |
| 26 | + ref: main |
| 27 | +``` |
| 28 | +
|
| 29 | +Then, import the package in your Dart code: |
| 30 | +
|
| 31 | +```dart |
| 32 | +import 'package:ht_headlines_repository/ht_headlines_repository.dart'; |
| 33 | +``` |
| 34 | + |
| 35 | +## Usage |
| 36 | + |
| 37 | +Create an instance of `HtHeadlinesRepository` by passing a `HtHeadlinesClient` instance. |
| 38 | + |
| 39 | +```dart |
| 40 | +import 'package:ht_headlines_client/ht_headlines_client.dart'; |
| 41 | +import 'package:ht_headlines_repository/ht_headlines_repository.dart'; |
| 42 | +
|
| 43 | +void main() async { |
| 44 | + // Replace with your HtHeadlinesClient implementation |
| 45 | + final headlinesClient = MyHeadlinesClient(); |
| 46 | + final headlinesRepository = |
| 47 | + HtHeadlinesRepository(client: headlinesClient); |
| 48 | +
|
| 49 | + // --- getHeadlines --- |
| 50 | + try { |
| 51 | + // Fetch headlines (paginated) |
| 52 | + final headlinesPage1 = await headlinesRepository.getHeadlines(limit: 10); |
| 53 | + print('Page 1: ${headlinesPage1.items}'); |
| 54 | +
|
| 55 | + // Fetch headlines with filtering |
| 56 | + final filteredHeadlines = await headlinesRepository.getHeadlines( |
| 57 | + limit: 5, |
| 58 | + category: 'technology', |
| 59 | + source: 'TechCrunch', |
| 60 | + ); |
| 61 | + print('Filtered Headlines: ${filteredHeadlines.items}'); |
| 62 | +
|
| 63 | + if (headlinesPage1.hasMore) { |
| 64 | + final headlinesPage2 = await headlinesRepository.getHeadlines( |
| 65 | + limit: 10, |
| 66 | + startAfterId: headlinesPage1.cursor, |
| 67 | + ); |
| 68 | + print('Page 2: ${headlinesPage2.items}'); |
| 69 | + } |
| 70 | + } on HeadlinesFetchException catch (e) { |
| 71 | + print('Error fetching headlines: $e'); |
| 72 | + } |
| 73 | +
|
| 74 | + // --- getHeadline --- |
| 75 | + try { |
| 76 | + final headline = await headlinesRepository.getHeadline(id: 'some_id'); |
| 77 | + if (headline != null) { |
| 78 | + print('Headline: $headline'); |
| 79 | + } else { |
| 80 | + print('Headline not found.'); |
| 81 | + } |
| 82 | + } on HeadlineNotFoundException catch (e) { |
| 83 | + print('Error fetching headline: $e'); |
| 84 | + } on HeadlinesFetchException catch (e) { |
| 85 | + print('Error fetching headline: $e'); |
| 86 | + } |
| 87 | +
|
| 88 | + // --- createHeadline --- |
| 89 | + try { |
| 90 | + final newHeadline = Headline( |
| 91 | + id: 'new_id', |
| 92 | + title: 'New Headline', |
| 93 | + description: 'This is a new headline.', |
| 94 | + url: 'https://example.com/new-headline', |
| 95 | + publishedAt: DateTime.now(), |
| 96 | + ); |
| 97 | + final createdHeadline = |
| 98 | + await headlinesRepository.createHeadline(headline: newHeadline); |
| 99 | + print('Created Headline: $createdHeadline'); |
| 100 | + } on HeadlineCreateException catch (e) { |
| 101 | + print('Error creating headline: $e'); |
| 102 | + } |
| 103 | +
|
| 104 | + // --- updateHeadline --- |
| 105 | + try { |
| 106 | + final updatedHeadline = Headline( |
| 107 | + id: 'existing_id', // Replace with an existing headline ID |
| 108 | + title: 'Updated Headline', |
| 109 | + description: 'This headline has been updated.', |
| 110 | + ); |
| 111 | + final result = |
| 112 | + await headlinesRepository.updateHeadline(headline: updatedHeadline); |
| 113 | + print('Updated Headline: $result'); |
| 114 | + } on HeadlineUpdateException catch (e) { |
| 115 | + print('Error updating headline: $e'); |
| 116 | + } |
| 117 | +
|
| 118 | + // --- deleteHeadline --- |
| 119 | + try { |
| 120 | + await headlinesRepository.deleteHeadline(id: 'existing_id'); // Replace with existing ID |
| 121 | + print('Headline deleted successfully.'); |
| 122 | + } on HeadlineDeleteException catch (e) { |
| 123 | + print('Error deleting headline: $e'); |
| 124 | + } |
| 125 | +
|
| 126 | + // --- searchHeadlines --- |
| 127 | + try { |
| 128 | + final searchedHeadlines = |
| 129 | + await headlinesRepository.searchHeadlines(query: 'example'); |
| 130 | + print('Searched Headlines: $searchedHeadlines.items}'); |
| 131 | + } on HeadlinesSearchException catch (e) { |
| 132 | + print('Error searching headlines: $e'); |
| 133 | + } |
| 134 | +
|
| 135 | + // --- getHeadlinesStream --- |
| 136 | + try { |
| 137 | + headlinesRepository |
| 138 | + .getHeadlinesStream(limit: 5, interval: const Duration(seconds: 30)) |
| 139 | + .listen((headlines) { |
| 140 | + print('Streamed Headlines: ${headlines.items}'); |
| 141 | + }); |
| 142 | + } on HeadlinesFetchException catch (e) { |
| 143 | + print('Error fetching headlines stream: $e'); |
| 144 | + } |
| 145 | +} |
| 146 | +``` |
| 147 | + |
| 148 | + |
| 149 | +## Additional information |
| 150 | + |
| 151 | +This package is designed to be data source agnostic, allowing you to easily switch |
| 152 | +between different backend implementations by providing a different `HtHeadlinesClient`. |
| 153 | + |
| 154 | +For issues and contributions, please refer to the [GitHub repository](https://github.com/Headlines-Toolkit/ht-headlines-client). |
0 commit comments