feat: starter kit.
This commit is contained in:
16
lib/data/models/log.dart
Normal file
16
lib/data/models/log.dart
Normal file
@@ -0,0 +1,16 @@
|
||||
/// A data model for holding log entries.
|
||||
class Log {
|
||||
final String date;
|
||||
final int status;
|
||||
final String method;
|
||||
final String path;
|
||||
final String response;
|
||||
|
||||
Log({
|
||||
required this.date,
|
||||
required this.status,
|
||||
required this.method,
|
||||
required this.path,
|
||||
required this.response,
|
||||
});
|
||||
}
|
||||
14
lib/data/models/project_info.dart
Normal file
14
lib/data/models/project_info.dart
Normal file
@@ -0,0 +1,14 @@
|
||||
/// A data model for holding appwrite project information.
|
||||
class ProjectInfo {
|
||||
final String endpoint;
|
||||
final String projectId;
|
||||
final String projectName;
|
||||
final String version;
|
||||
|
||||
ProjectInfo({
|
||||
required this.endpoint,
|
||||
required this.projectId,
|
||||
required this.projectName,
|
||||
required this.version,
|
||||
});
|
||||
}
|
||||
2
lib/data/models/status.dart
Normal file
2
lib/data/models/status.dart
Normal file
@@ -0,0 +1,2 @@
|
||||
/// Enum representing different connection states.
|
||||
enum Status { idle, loading, success, error }
|
||||
73
lib/data/repository/appwrite_repository.dart
Normal file
73
lib/data/repository/appwrite_repository.dart
Normal file
@@ -0,0 +1,73 @@
|
||||
import 'package:appwrite/appwrite.dart';
|
||||
import 'package:appwrite_flutter_starter_kit/data/models/log.dart';
|
||||
import 'package:appwrite_flutter_starter_kit/data/models/project_info.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
/// A repository responsible for handling network interactions with the Appwrite server.
|
||||
///
|
||||
/// It provides a helper method to ping the server.
|
||||
class AppwriteRepository {
|
||||
static const String pingPath = "/ping";
|
||||
static const String appwriteVersion = "1.6.1";
|
||||
static const String appwriteProjectId = "project-id";
|
||||
static const String appwriteProjectName = "My project";
|
||||
static const String appwritePublicEndpoint = "https://cloud.appwrite.io/v1";
|
||||
|
||||
final Client _client = Client()
|
||||
.setProject(appwriteProjectId)
|
||||
.setEndpoint(appwritePublicEndpoint);
|
||||
|
||||
late final Account _account;
|
||||
late final Databases _databases;
|
||||
|
||||
AppwriteRepository._internal() {
|
||||
_account = Account(_client);
|
||||
_databases = Databases(_client);
|
||||
}
|
||||
|
||||
static final AppwriteRepository _instance = AppwriteRepository._internal();
|
||||
|
||||
/// Singleton instance getter
|
||||
factory AppwriteRepository() => _instance;
|
||||
|
||||
ProjectInfo getProjectInfo() {
|
||||
return ProjectInfo(
|
||||
endpoint: appwritePublicEndpoint,
|
||||
projectId: appwriteProjectId,
|
||||
projectName: appwriteProjectName,
|
||||
version: appwriteVersion,
|
||||
);
|
||||
}
|
||||
|
||||
/// Pings the Appwrite server and captures the response.
|
||||
///
|
||||
/// @return [Log] containing request and response details.
|
||||
Future<Log> ping() async {
|
||||
try {
|
||||
final response = await _client.ping();
|
||||
|
||||
return Log(
|
||||
date: _getCurrentDate(),
|
||||
status: 200,
|
||||
method: "GET",
|
||||
path: pingPath,
|
||||
response: response,
|
||||
);
|
||||
} on AppwriteException catch (error) {
|
||||
return Log(
|
||||
date: _getCurrentDate(),
|
||||
status: error.code ?? 500,
|
||||
method: "GET",
|
||||
path: pingPath,
|
||||
response: error.message ?? "Unknown error",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Retrieves the current date in the format "MMM dd, HH:mm".
|
||||
///
|
||||
/// @return [String] A formatted date.
|
||||
String _getCurrentDate() {
|
||||
return DateFormat("MMM dd, HH:mm").format(DateTime.now());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user