Future searchPhotos()

in experimental/desktop_photo_search/lib/src/unsplash/unsplash.dart [31:78]


  Future<SearchPhotosResponse?> searchPhotos({
    required String query,
    num page = 1,
    num perPage = 10,
    List<num> collections = const [],
    SearchPhotosOrientation? orientation,
  }) async {
    final searchPhotosUrl = _unsplashBaseUrl
        .replace(path: '/search/photos', queryParameters: <String, String>{
      'query': query,
      if (page != 1) 'page': '$page',
      if (perPage != 10) 'per_page': '$perPage',
      if (collections.isNotEmpty) 'collections': collections.join(','),
      if (orientation == SearchPhotosOrientation.landscape)
        'orientation': 'landscape',
      if (orientation == SearchPhotosOrientation.portrait)
        'orientation': 'portrait',
      if (orientation == SearchPhotosOrientation.squarish)
        'orientation': 'squarish',
    });
    _log.info('GET $searchPhotosUrl');

    final response = await _client.get(
      searchPhotosUrl,
      headers: {
        'Accept-Version': 'v1',
        'Authorization': 'Client-ID $_accessKey',
      },
    );

    dynamic body;
    try {
      body = json.fuse(utf8).decode(response.bodyBytes);
    } catch (e) {
      throw UnsplashException('Invalid JSON received');
    }

    if (body is Map &&
        body['errors'] is List &&
        body['errors'].isNotEmpty as bool) {
      final apiError = ApiError.fromJson(response.body)!;
      throw UnsplashException(apiError.errors!.join(', '));
    }

    return SearchPhotosResponse.fromJson(
      response.body,
    );
  }