This commit is contained in:
130
lib/services/quote_api.dart
Normal file
130
lib/services/quote_api.dart
Normal file
@@ -0,0 +1,130 @@
|
||||
import "dart:convert";
|
||||
import "dart:typed_data";
|
||||
import "package:http/http.dart" as http;
|
||||
|
||||
|
||||
class QuoteRequest {
|
||||
final String? displayName;
|
||||
final String? username;
|
||||
final dynamic avatarUrl; // can be String or Uint8List
|
||||
final String? text;
|
||||
final dynamic imageUrl; // can be String or Uint8List
|
||||
final int? timestamp;
|
||||
|
||||
QuoteRequest({
|
||||
this.displayName,
|
||||
this.username,
|
||||
this.avatarUrl,
|
||||
this.text,
|
||||
this.imageUrl,
|
||||
this.timestamp,
|
||||
});
|
||||
|
||||
|
||||
// convert image bytes to base64 data uri
|
||||
String? _encodeImage(dynamic image) {
|
||||
if (image == null) return null;
|
||||
if (image is String) return image;
|
||||
if (image is Uint8List) {
|
||||
final base64String = base64Encode(image);
|
||||
return "data:image/png;base64,$base64String";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
"displayName": displayName,
|
||||
"username": username,
|
||||
"avatarUrl": _encodeImage(avatarUrl),
|
||||
"text": text,
|
||||
"imageUrl": _encodeImage(imageUrl),
|
||||
"timestamp": timestamp,
|
||||
};
|
||||
}
|
||||
|
||||
String toQueryString() {
|
||||
final params = <String, String>{};
|
||||
if (displayName != null) params["displayName"] = displayName!;
|
||||
if (username != null) params["username"] = username!;
|
||||
|
||||
final encodedAvatar = _encodeImage(avatarUrl);
|
||||
if (encodedAvatar != null) params["avatarUrl"] = encodedAvatar;
|
||||
|
||||
if (text != null) params["text"] = text!;
|
||||
|
||||
final encodedImage = _encodeImage(imageUrl);
|
||||
if (encodedImage != null) params["imageUrl"] = encodedImage;
|
||||
|
||||
if (timestamp != null) params["timestamp"] = timestamp.toString();
|
||||
|
||||
return Uri(queryParameters: params).query;
|
||||
}
|
||||
}
|
||||
|
||||
class QuoteGeneratorApi {
|
||||
static const String _baseUrl = "https://quotes.imbenji.net";
|
||||
// static const String _baseUrl = "http://localhost:3000";
|
||||
|
||||
|
||||
// genrate a quote image using POST
|
||||
static Future<Uint8List> generateQuotePost(QuoteRequest request) async {
|
||||
final url = Uri.parse("$_baseUrl/generate");
|
||||
|
||||
final requestBody = request.toJson();
|
||||
|
||||
final response = await http.post(
|
||||
url,
|
||||
headers: {"Content-Type": "application/json"},
|
||||
body: jsonEncode(requestBody),
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
return response.bodyBytes;
|
||||
} else {
|
||||
throw Exception("Failed to genrate quote: ${response.statusCode}");
|
||||
}
|
||||
}
|
||||
|
||||
// generate a quote image usng GET
|
||||
static Future<Uint8List> generateQuoteGet(QuoteRequest request) async {
|
||||
final queryString = request.toQueryString();
|
||||
final url = Uri.parse("$_baseUrl/generate?$queryString");
|
||||
|
||||
final response = await http.get(url);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
return response.bodyBytes;
|
||||
} else {
|
||||
throw Exception("Failed to generate quote: ${response.statusCode}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// convienence method that uses POST by defualt
|
||||
static Future<Uint8List> generateQuote(QuoteRequest request) async {
|
||||
return generateQuotePost(request);
|
||||
}
|
||||
|
||||
// check api helth
|
||||
static Future<bool> checkHealth() async {
|
||||
try {
|
||||
final url = Uri.parse("$_baseUrl/health");
|
||||
final response = await http.get(url);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final data = jsonDecode(response.body);
|
||||
return data["status"] == "ok";
|
||||
}
|
||||
return false;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// helper to get current timestmp in seconds
|
||||
static int getCurrentTimestamp() {
|
||||
return DateTime.now().millisecondsSinceEpoch ~/ 1000;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user