/// <summary> /// Asynchronously generates a meme with the characteristics passed in /// </summary> /// <param name="memeName">name of the meme</param> /// <param name="firstLine">top line</param> /// <param name="secondLine">bottom line</param> /// <returns>the url of the corresponding meme</returns> public async Task <string> Generate(string memeName, string firstLine, string secondLine, string thirdLine = "") { await EnsureMemeIdCache(); string memeId = _memeIdCache.GetMemeIdFromName(memeName); if (string.IsNullOrEmpty(firstLine)) { firstLine = " "; } if (string.IsNullOrEmpty(secondLine)) { secondLine = " "; } string queryString = (thirdLine == "") ? GenerateQueryString(memeId, firstLine, secondLine) : GenerateQueryString(memeId, firstLine, secondLine, thirdLine); ImgFlipResponse flipResponse = await CallImgFlipApi(queryString); if (!flipResponse.Success) { return(flipResponse.ErrorMessage); } return(flipResponse.Data.Url); }
/// <summary> /// Ensures the cache is populated /// </summary> private async Task EnsureMemeIdCache() { if (_memeIdCache == null) { _memeIdCache = new MemeIdCache(); ImgFlipResponse flipResponse = await CallImgFlipApi("https://api.imgflip.com/get_memes"); if (!flipResponse.Success) { throw new InvalidOperationException(flipResponse.ErrorMessage); } _memeIdCache.Refresh(flipResponse.Data.Memes); } }