/// <summary> /// Filters the gifs based on the client rating selection. /// </summary> /// <param name="gifList">A GifList to populate with Gifs</param> /// <param name="giphyObj">The GiphyObj containing Json gifs that were serialized</param> /// <param name="rating">The rating to filter the gifs with</param> /// <returns></returns> private GifList GetGifs(GifList gifList, GiphyObj giphyObj, string rating) { if (giphyObj == null) { return(null); } int size = giphyObj.Data.Count; List <Gif> gifs = new List <Gif>(size); //filters the gifs for (int i = 0; i < giphyObj.Data.Count; i++) { if (rating.Equals("all")) { gifs = AddGif(giphyObj, i, gifs); } else { if (rating.Equals(giphyObj.Data[i].Rating)) { gifs = AddGif(giphyObj, i, gifs); } } } //add the gif list to the GifList object gifList.Gifs = gifs; gifList.Size = gifList.Gifs.Count; return(gifList); }
//Adds a gif to the list of gifs private List <Gif> AddGif(GiphyObj giphyObj, int i, List <Gif> gifs) { Gif myGif = new Gif(); myGif.Url = giphyObj.Data[i].Images.Original.Url; myGif.Username = (giphyObj.Data[i].User != null) ? giphyObj.Data[i].User.Username : null; gifs.Add(myGif); return(gifs); }
public JsonResult GetJsonGifs(string searchArea, string searchParams, string rating) { //Get the IP address and browser user agent for logging. string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; string userAgent = Request.Headers["User-Agent"].ToString(); if (string.IsNullOrEmpty(ipAddress)) { ipAddress = Request.ServerVariables["REMOTE_ADDR"]; } //Get api key and build the GET request header. string apiKey = System.Web.Configuration.WebConfigurationManager.AppSettings["GiphyKey"]; string url = $"http://api.giphy.com{searchArea}?api_key={apiKey}&q={searchParams}"; Debug.WriteLine($"Url is: {url}"); GifList gl = new GifList(); GiphyObj giphyObjs = null; /*Try to deserialize the json data into C# objects*/ try { //Send request to Giphy.com HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url); httpRequest.Method = "GET"; httpRequest.ContentType = "application/json"; //Handle response HttpWebResponse response = (HttpWebResponse)httpRequest.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream()); var result = sr.ReadToEnd(); Debug.WriteLine("Result is: " + result); giphyObjs = JsonConvert.DeserializeObject <GiphyObj>(result, Converter.Settings); } catch (Exception e) { Debug.WriteLine(e.StackTrace); } gl = GetGifs(gl, giphyObjs, rating); LogRequest(ipAddress, userAgent, searchArea, searchParams, rating);//Log user request return(Json(gl, JsonRequestBehavior.AllowGet)); }
public static string ToJson(this GiphyObj self) => JsonConvert.SerializeObject(self, Converter.Settings);