translate method

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

Translate a word into a Gif or Sticker GIPHY Translate converts words and phrases to the perfect GIF or Sticker using GIPHY's special sauce algorithm. This feature is best exhibited in GIPHY's Slack integration.

Implementation

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

  // 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.translate,
  ];

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

  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);
}