random method

Future<SuccessResponseSingleModel> random(
  1. String tag, {
  2. Rating? rating,
  3. bool sticker = false,
})

Get a random Gif or Sticker GIPHY Random lets you add some weirdness to the conversation by returning a single random GIF or Sticker related to the word or phrase entered. If no tag is specified, the GIF or Sticker returned is completely random.

Implementation

Future<SuccessResponseSingleModel> random(
  String tag, {
  Rating? rating,
  bool sticker = false,
}) async {
  // Verify if rating is not "y"
  if (rating == Rating.y) {
    throw Exception('Rating "y" is not supported for random endpoint');
  }

  // Verify if query is not empty and the maximum length is 50 characters
  if (tag.isEmpty || tag.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.translate,
  ];

  Map<String, dynamic> queryParameters = {
    'api_key': apiKey,
    'tag': tag,
  };

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

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

  // Make the request

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

  // Parse the response

  return SuccessResponseSingleModel.fromJson(responseMap);
}