mirror of
https://github.com/Myzel394/quid_faciam_hodie.git
synced 2025-06-19 15:45:26 +02:00
20 lines
628 B
Dart
20 lines
628 B
Dart
import 'dart:convert';
|
|
import 'dart:math';
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
class PhotoManager {
|
|
static const MAX_PHOTOS_PER_PAGE = 80;
|
|
|
|
// Searches for photos based on `query` and returns a random one.
|
|
static Future<String> getRandomPhoto(final String query) async {
|
|
final url =
|
|
'https://api.pexels.com/v1/search?query=$query&per_page=$MAX_PHOTOS_PER_PAGE&orientation=portait';
|
|
final response = await http.get(Uri.parse(url));
|
|
final data = jsonDecode(response.body);
|
|
final photoIndex = Random().nextInt(data['per_page']);
|
|
|
|
return data['photos'][photoIndex]['src']['portrait'];
|
|
}
|
|
}
|