search method

Future<SuccessResponseMultiModel> search(
  1. String query, {
  2. int limit = 25,
  3. int offset = 0,
  4. Rating? rating,
  5. GiphyLanguage language = GiphyLanguage.en,
  6. Bundle? bundle,
  7. bool sticker = false,
})

Search Gif or Stickers GIPHY Search gives you instant access to our library of millions of GIFs and Stickers by entering a word or phrase. With our unparalleled search algorithm, users can easily express themselves and animate their conversations.

Implementation

Future<SuccessResponseMultiModel> search(
  String query, {
  int limit = 25,
  int offset = 0,
  Rating? rating,
  GiphyLanguage language = GiphyLanguage.en,
  Bundle? bundle,
  bool sticker = false,
}) async {
  // Verify if rating is not "y"
  if (rating == Rating.y) {
    throw Exception('Rating "y" is not supported for search endpoint');
  }

  // Verify if limit is an Int32 value
  if (!limit.isInt32 && limit < 1) {
    throw Exception('Limit must be an Int32 value and greater than 0');
  }

  // Verify if offset is an Int32 value
  if (!offset.isInt32 && offset < 0) {
    throw Exception(
      'Offset must be an Int32 value and greater than or equal to 0',
    );
  }

  // Verify if query is not empty and the maximum length is 50 characters
  if (query.isEmpty || query.length > 50) {
    throw Exception(
      'Query must not be empty and have a maximum length of 50 characters',
    );
  }

  const baseUrl = GiphyAPIPath.baseUrl;

  final pathSegments = [
    GiphyAPIPath.version1,
    sticker ? GiphyAPIPath.stickers : GiphyAPIPath.gif,
    GiphyAPIPath.search,
  ];

  Map<String, dynamic> queryParameters = {
    'api_key': apiKey,
    'q': query,
    'limit': limit.toString(),
    'offset': offset.toString(),
    'lang': language.apiValue,
  };

  if (rating != null) {
    queryParameters['rating'] = rating.apiValue;
  }

  if (randomID != null) {
    queryParameters['random_id'] = randomID;
  }

  if (bundle != null) {
    queryParameters['bundle'] = bundle.apiValue;
  }

  // Make the request

  final responseMap = await GiphyApiManager.get(
    baseUrl,
    pathSegments,
    queryParameters: queryParameters,
    debugMode: debugMode,
  );

  // Parse the response

  return SuccessResponseMultiModel.fromJson(responseMap);
}