Skip to content

Commit 4309be1

Browse files
committed
docs(README): update with new API features and architectural details
- Add overview of User Settings API and its endpoints - Refactor Core Functionality section with more detailed information - Update Technical Overview with new shared packages and patterns - Restructure API Endpoint documentation for clarity - Improve consistency in error handling and response structure description
1 parent caf7135 commit 4309be1

File tree

1 file changed

+87
-37
lines changed

1 file changed

+87
-37
lines changed

README.md

Lines changed: 87 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,74 @@
11
# ht_api
22

3-
<!-- Badges (Update coverage XX value after running tests) -->
43
![coverage: percentage](https://img.shields.io/badge/coverage-XX-green)
54
[![style: very good analysis](https://img.shields.io/badge/style-very_good_analysis-B22C89.svg)](https://pub.dev/packages/very_good_analysis)
65
[![License: PolyForm Free Trial](https://img.shields.io/badge/License-PolyForm%20Free%20Trial-blue)](https://polyformproject.org/licenses/free-trial/1.0.0)
76

87
## Overview
98

10-
`ht_api` is the backend API component of the Headlines Toolkit (HT) project. It serves as the central data provider for HT applications (like the mobile app and web dashboard), offering access to various data models required by the toolkit. Built with Dart using the Dart Frog framework, it prioritizes simplicity, maintainability, and scalability through a generic API design.
9+
`ht_api` is the central backend API service for the Headlines Toolkit (HT) project. Built with Dart using the Dart Frog framework, it provides essential APIs to support HT client applications (like the mobile app and web dashboard). It aims for simplicity, maintainability, and scalability, currently offering APIs for data access and user settings management.
1110

1211
## Features
1312

14-
* **Generic Data Endpoint (`/api/v1/data`):** Provides a unified RESTful interface for performing CRUD (Create, Read, Update, Delete) operations on multiple data models.
13+
### Core Functionality
14+
15+
* **Standardized Success Responses:** Returns consistent JSON success responses wrapped in a `SuccessApiResponse` structure, including request metadata (`requestId`, `timestamp`).
16+
* **Standardized Error Handling:** Returns consistent JSON error responses via centralized middleware (`lib/src/middlewares/error_handler.dart`) for predictable client-side handling.
17+
* **Request Traceability:** Generates a unique `requestId` (UUID v4) for each incoming request, included in success response metadata and available for server-side logging via context.
18+
* **In-Memory Demo Mode:** Utilizes pre-loaded fixture data (`lib/src/fixtures/`) for demonstration and development purposes, simulating a live backend without external dependencies for core data and settings.
19+
20+
### Data API (`/api/v1/data`)
21+
22+
* **Generic Data Endpoint:** Provides a unified RESTful interface for performing CRUD (Create, Read, Update, Delete) operations on multiple data models.
1523
* **Model Agnostic Design:** Supports various data types through a single endpoint structure, determined by the `?model=` query parameter.
16-
* **Currently Supported Models:**
24+
* **Currently Supported Data Models:**
1725
* `headline`
1826
* `category`
1927
* `source`
2028
* `country`
21-
* **Standardized Success Responses:** Returns consistent JSON success responses wrapped in a `SuccessApiResponse` structure, including request metadata (`requestId`, `timestamp`).
22-
* **Standardized Error Handling:** Returns consistent JSON error responses via centralized middleware (`lib/src/middlewares/error_handler.dart`) for predictable client-side handling.
23-
* **Request Traceability:** Generates a unique `requestId` (UUID v4) for each incoming request, included in success response metadata and available for server-side logging via context.
24-
* **In-Memory Demo Mode:** Utilizes pre-loaded fixture data (`lib/src/fixtures/`) for demonstration and development purposes, simulating a live backend without external dependencies.
25-
* **Standardized Error Handling:** Returns consistent JSON error responses via centralized middleware (`lib/src/middlewares/error_handler.dart`) for predictable client-side handling.
29+
30+
### User Settings API (`/api/v1/users/me/settings`)
31+
32+
* **User-Specific Settings Management:** Provides RESTful endpoints for managing application settings (currently treated globally, future enhancement for user-specific).
33+
* **Supported Settings:**
34+
* Display Settings (Theme, Font, etc.)
35+
* Language Preference
2636

2737
## Technical Overview
2838

2939
* **Language:** Dart (`>=3.0.0 <4.0.0`)
3040
* **Framework:** Dart Frog (`^1.1.0`)
31-
* **Architecture:** Layered architecture leveraging shared packages and a generic API pattern.
41+
* **Architecture:** Layered architecture leveraging shared packages.
3242
* **Key Packages & Shared Core:**
3343
* `dart_frog`: The web framework foundation.
3444
* `uuid`: Used to generate unique request IDs.
35-
* `ht_shared`: Contains shared data models (`Headline`, `Category`, `Source`, `Country`, `SuccessApiResponse`, `ResponseMetadata`, etc.) used across the HT ecosystem.
36-
* `ht_data_client`: Defines the generic `HtDataClient<T>` interface for data access operations.
37-
* `ht_data_inmemory`: Provides the `HtDataInMemoryClient<T>` implementation, used here for the demo mode, seeded with fixture data.
38-
* `ht_data_repository`: Defines the generic `HtDataRepository<T>` which abstracts the data client, providing a clean interface to the API route handlers.
39-
* `ht_http_client`: Provides custom HTTP exceptions (`HtHttpException` subtypes) used for consistent error signaling, even by the in-memory client.
45+
* `ht_shared`: Contains shared data models (`Headline`, `Category`, etc.), API response wrappers (`SuccessApiResponse`, `ResponseMetadata`), and standard exceptions (`HtHttpException`).
46+
* `ht_data_client`: Defines the generic `HtDataClient<T>` interface.
47+
* `ht_data_inmemory`: Provides the `HtDataInMemoryClient<T>` implementation for data.
48+
* `ht_data_repository`: Defines the generic `HtDataRepository<T>` for data access.
49+
* `ht_app_settings_client`: Defines the `HtAppSettingsClient` interface for settings.
50+
* `ht_app_settings_inmemory`: Provides the `HtAppSettingsInMemory` implementation for settings.
51+
* `ht_app_settings_repository`: Defines the `HtAppSettingsRepository` for settings management.
52+
* `ht_http_client`: Provides custom HTTP exceptions (`HtHttpException` subtypes) used for consistent error signaling.
4053
* **Key Patterns:**
41-
* **Generic Repository Pattern:** `HtDataRepository<T>` provides a type-safe abstraction over data fetching logic. Route handlers interact with repositories, not directly with data clients.
42-
* **Generic Data Endpoint:** A single set of route handlers (`/api/v1/data/index.dart`, `/api/v1/data/[id].dart`) serves multiple data models.
43-
* **Model Registry:** A central map (`lib/src/registry/model_registry.dart`) links model name strings (from the `?model=` query parameter) to model-specific configurations (`ModelConfig`), primarily containing serialization functions (`fromJson`) and ID extraction logic (`getId`).
44-
* **Dependency Injection (Dart Frog Providers):** Middleware (`routes/_middleware.dart`) is used to instantiate and provide singleton instances of each `HtDataRepository<T>` (configured with the in-memory client and fixture data), the `ModelRegistryMap`, and a unique `RequestId` per request. Route-specific middleware (`routes/api/v1/data/_middleware.dart`) resolves the requested model, validates it, and provides the corresponding `ModelConfig` and model name string to the handler.
45-
* **Centralized Error Handling:** The `errorHandler` middleware intercepts exceptions (especially `HtHttpException` subtypes and `FormatException`) and maps them to standardized JSON error responses with appropriate HTTP status codes.
54+
* **Repository Pattern:** `HtDataRepository<T>` and `HtAppSettingsRepository` provide clean abstractions over data/settings logic. Route handlers interact with repositories.
55+
* **Generic Data Endpoint:** A single set of route handlers serves multiple data models via `/api/v1/data`.
56+
* **Model Registry:** A central map (`lib/src/registry/model_registry.dart`) links data model names to configurations (`ModelConfig`) for the generic data endpoint.
57+
* **Dependency Injection (Dart Frog Providers):** Middleware (`routes/_middleware.dart`) provides singleton instances of repositories (`HtDataRepository<T>`, `HtAppSettingsRepository`), the `ModelRegistryMap`, and a unique `RequestId`.
58+
* **Centralized Error Handling:** The `errorHandler` middleware intercepts exceptions and maps them to standardized JSON error responses.
4659

47-
## API Endpoint: `/api/v1/data`
60+
## API Endpoints: Data (`/api/v1/data`)
4861

4962
This endpoint serves as the single entry point for accessing different data models. The specific model is determined by the `model` query parameter.
5063

5164
**Supported `model` values:** `headline`, `category`, `source`, `country`
5265

53-
**Standard Response Structure:**
66+
**Standard Response Structure:** (Applies to both Data and Settings APIs)
5467

5568
* **Success:**
5669
```json
5770
{
58-
"data": <item_or_paginated_list>,
71+
"data": <item_or_paginated_list_or_settings_object>,
5972
"metadata": {
6073
"request_id": "unique-uuid-v4-per-request",
6174
"timestamp": "iso-8601-utc-timestamp"
@@ -72,47 +85,84 @@ This endpoint serves as the single entry point for accessing different data mode
7285
}
7386
```
7487

75-
**Operations:**
88+
**Data Operations:**
7689

7790
1. **Get All Items (Collection)**
7891
* **Method:** `GET`
7992
* **Path:** `/api/v1/data?model=<model_name>`
80-
* **Optional Query Parameters:**
81-
* `limit=<int>`: Limit the number of items returned.
82-
* `startAfterId=<string>`: Paginate results, starting after the item with this ID.
83-
* *Other query parameters*: Passed directly to the repository's `readAllByQuery` method for filtering (e.g., `?model=headline&category=Technology`).
93+
* **Optional Query Parameters:** `limit=<int>`, `startAfterId=<string>`, other filtering params.
8494
* **Success Response:** `200 OK` with `SuccessApiResponse<PaginatedResponse<T>>`.
8595
* **Example:** `GET /api/v1/data?model=headline&limit=10`
8696

8797
2. **Create Item**
8898
* **Method:** `POST`
8999
* **Path:** `/api/v1/data?model=<model_name>`
90-
* **Request Body:** JSON object representing the item to create (ID is usually omitted if auto-generated).
100+
* **Request Body:** JSON object representing the item to create.
91101
* **Success Response:** `201 Created` with `SuccessApiResponse<T>` containing the created item.
92-
* **Example:** `POST /api/v1/data?model=category` with body `{"name": "Sports", "description": "News about sports"}`
102+
* **Example:** `POST /api/v1/data?model=category` with body `{"name": "Sports", ...}`
93103

94104
3. **Get Item by ID**
95105
* **Method:** `GET`
96106
* **Path:** `/api/v1/data/<item_id>?model=<model_name>`
97-
* **Success Response:** `200 OK` with `SuccessApiResponse<T>` containing the requested item.
98-
* **Error Response:** `404 Not Found` if the ID doesn't exist for the given model.
107+
* **Success Response:** `200 OK` with `SuccessApiResponse<T>`.
108+
* **Error Response:** `404 Not Found`.
99109
* **Example:** `GET /api/v1/data/some-headline-id?model=headline`
100110

101111
4. **Update Item by ID**
102112
* **Method:** `PUT`
103113
* **Path:** `/api/v1/data/<item_id>?model=<model_name>`
104-
* **Request Body:** JSON object representing the complete updated item (must include the correct `id`).
105-
* **Success Response:** `200 OK` with `SuccessApiResponse<T>` containing the updated item.
106-
* **Error Response:** `404 Not Found`, `400 Bad Request` (e.g., ID mismatch).
107-
* **Example:** `PUT /api/v1/data/some-category-id?model=category` with updated category JSON in the body.
114+
* **Request Body:** JSON object representing the complete updated item (must include `id`).
115+
* **Success Response:** `200 OK` with `SuccessApiResponse<T>`.
116+
* **Error Response:** `404 Not Found`, `400 Bad Request`.
117+
* **Example:** `PUT /api/v1/data/some-category-id?model=category` with updated category JSON.
108118

109119
5. **Delete Item by ID**
110120
* **Method:** `DELETE`
111121
* **Path:** `/api/v1/data/<item_id>?model=<model_name>`
112-
* **Success Response:** `204 No Content` (No body).
122+
* **Success Response:** `204 No Content`.
113123
* **Error Response:** `404 Not Found`.
114124
* **Example:** `DELETE /api/v1/data/some-source-id?model=source`
115125

126+
## API Endpoints: User Settings (`/api/v1/users/me/settings`)
127+
128+
These endpoints manage application settings. Currently, they operate on a global set of settings due to the lack of authentication; future enhancements will make them user-specific.
129+
130+
**Standard Response Structure:** Uses the same `SuccessApiResponse` and error structure as the Data API.
131+
132+
**Settings Operations:**
133+
134+
1. **Get Display Settings**
135+
* **Method:** `GET`
136+
* **Path:** `/api/v1/users/me/settings/display`
137+
* **Success Response:** `200 OK` with `SuccessApiResponse<DisplaySettings>`.
138+
* **Example:** `GET /api/v1/users/me/settings/display`
139+
140+
2. **Update Display Settings**
141+
* **Method:** `PUT`
142+
* **Path:** `/api/v1/users/me/settings/display`
143+
* **Request Body:** JSON object representing the complete `DisplaySettings`.
144+
* **Success Response:** `200 OK` with `SuccessApiResponse<DisplaySettings>` containing the updated settings.
145+
* **Example:** `PUT /api/v1/users/me/settings/display` with `DisplaySettings` JSON in the body.
146+
147+
3. **Get Language Setting**
148+
* **Method:** `GET`
149+
* **Path:** `/api/v1/users/me/settings/language`
150+
* **Success Response:** `200 OK` with `SuccessApiResponse<Map<String, String>>` (e.g., `{"data": {"language": "en"}, ...}`).
151+
* **Example:** `GET /api/v1/users/me/settings/language`
152+
153+
4. **Update Language Setting**
154+
* **Method:** `PUT`
155+
* **Path:** `/api/v1/users/me/settings/language`
156+
* **Request Body:** JSON object `{"language": "<code>"}` (e.g., `{"language": "es"}`).
157+
* **Success Response:** `200 OK` with `SuccessApiResponse<Map<String, String>>` containing the updated language setting.
158+
* **Example:** `PUT /api/v1/users/me/settings/language` with body `{"language": "fr"}`.
159+
160+
5. **Clear All Settings**
161+
* **Method:** `DELETE`
162+
* **Path:** `/api/v1/users/me/settings`
163+
* **Success Response:** `204 No Content`.
164+
* **Example:** `DELETE /api/v1/users/me/settings`
165+
116166
## Setup & Running
117167

118168
1. **Prerequisites:**

0 commit comments

Comments
 (0)