The PortalService interface defines the contract for implementing portal-specific operations including settings management, authorization, URL parsing, and content retrieval.
Interface definition
abstract interface class PortalService<T extends PortalSettings> {
T settingsFromJson(Map<String, dynamic>? json);
List<PortalSettingItem> buildSettingsSchema(T settings);
bool isAuthorized(T settings);
String getIdFromUrl(Uri url);
void Function(T updatedSettings)? onSettingsChanged;
Future<Book> getBookFromId(String id, {required T settings});
Future<List<Chapter>> getTextFromId(String id, {required T settings});
}
Methods
settingsFromJson
T settingsFromJson(Map<String, dynamic>? json)
Deserializes portal settings from a JSON map.
The JSON map containing settings data, may be null for default settings.
Returns: An instance of the portal’s settings type T.
buildSettingsSchema
List<PortalSettingItem> buildSettingsSchema(T settings)
Builds the settings schema UI definition for the portal.
The current settings instance.
Returns: A list of PortalSettingItem objects defining the settings UI.
isAuthorized
bool isAuthorized(T settings)
Checks if the portal is authorized with the given settings.
The settings to check for authorization.
Returns: true if authorized, false otherwise.
getIdFromUrl
String getIdFromUrl(Uri url)
Extracts the book or content ID from a portal URL.
Returns: The extracted content ID as a string.
getBookFromId
Future<Book> getBookFromId(String id, {required T settings})
Retrieves book metadata from the portal.
The unique identifier of the book.
The portal settings to use for the request.
Returns: A Future that resolves to a Book object with metadata.
getTextFromId
Future<List<Chapter>> getTextFromId(String id, {required T settings})
Retrieves all chapters for a book from the portal.
The unique identifier of the book.
The portal settings to use for the request.
Returns: A Future that resolves to a list of Chapter objects.
Properties
onSettingsChanged
void Function(T updatedSettings)?
Optional callback invoked when settings are updated. Can be set to receive notifications of settings changes.
Usage example
class MyPortalService implements PortalService<MyPortalSettings> {
@override
MyPortalSettings settingsFromJson(Map<String, dynamic>? json) {
return MyPortalSettings.fromJson(json ?? {});
}
@override
List<PortalSettingItem> buildSettingsSchema(MyPortalSettings settings) {
return [
PortalSettingItem.text(
key: 'apiKey',
label: 'API Key',
value: settings.apiKey,
),
];
}
@override
bool isAuthorized(MyPortalSettings settings) {
return settings.apiKey?.isNotEmpty ?? false;
}
@override
String getIdFromUrl(Uri url) {
return url.pathSegments.last;
}
@override
Future<Book> getBookFromId(String id, {required MyPortalSettings settings}) async {
// Fetch and return book metadata
final response = await http.get(Uri.parse('https://api.example.com/books/$id'));
return Book.fromJson(jsonDecode(response.body));
}
@override
Future<List<Chapter>> getTextFromId(String id, {required MyPortalSettings settings}) async {
// Fetch and return chapters
final response = await http.get(Uri.parse('https://api.example.com/books/$id/chapters'));
return (jsonDecode(response.body) as List)
.map((json) => Chapter.fromJson(json))
.toList();
}
}
Type parameters
The settings type for this portal service, must extend PortalSettings.