/// <summary> /// Gets and returns the photo for the Person. /// </summary> /// <remarks> /// There is basic caching through a static Dictionary. /// </remarks> /// <param name="rest"> /// The <see cref="RestClient"/> connection. /// </param> /// <returns> /// The Person's profile photo. /// </returns> public Image GetPhoto(RestClient rest) { Image ret = null; lock (sync) { if (fPhotos.ContainsKey(fPersonID)) { ret = fPhotos[fPersonID]; } } if (ret != null) { return ret; } StringBuilder url = new StringBuilder(rest.Host); if (!rest.Host.EndsWith("/")) { url.Append("/"); } url.Append("data/people/"); url.Append(fPersonID); url.Append("/photo"); Uri uri = new Uri(url.ToString(), UriKind.Absolute); HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri); req.Accept = "image/png"; req.Headers["Authorization"] = "Basic " + Convert.ToBase64String( Encoding.UTF8.GetBytes(rest.Username + ":" + rest.Password)); try { HttpWebResponse response = (HttpWebResponse)req.GetResponse(); lock (sync) { fPhotos.Add(fPersonID, Image.FromStream(response.GetResponseStream())); ret = fPhotos[fPersonID]; } return ret; } catch { return null; } }
/// <summary> /// Fetches Signals matching criteria using ReST. /// </summary> /// <param name="rest"> /// A <see cref="RestClient"/> connection used to fetch the Signals. /// </param> /// <param name="criteria"> /// The criteria to be used when fetching Signals. /// </param> /// <returns> /// A <see cref="List<Signal>"/> containing the returned Signals. /// </returns> public static List<Signal> Get(RestClient rest, String criteria) { StringBuilder url = new StringBuilder(rest.Host); if (!rest.Host.EndsWith("/")) { url.Append("/"); } url.Append("data/signals"); if (!String.IsNullOrWhiteSpace(criteria)) { url.Append("?" + criteria); } Uri uri = new Uri(url.ToString(), UriKind.Absolute); HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri); req.Accept = "application/json"; req.Headers["Authorization"] = "Basic " + Convert.ToBase64String( Encoding.UTF8.GetBytes(rest.Username + ":" + rest.Password)); HttpWebResponse response = (HttpWebResponse)req.GetResponse(); DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(Signal[])); Signal[] signals = (Signal[])json.ReadObject(response.GetResponseStream()); foreach (Signal s in signals) { s.Person.GetPhoto(rest); } return new List<Signal>(signals); }
/// <summary> /// Fetches the most recent Signals using ReST. /// </summary> /// <param name="rest"> /// A <see cref="RestClient"/> connection used to fetch the Signals. /// </param> /// <returns> /// A <see cref="List<Signal>"/> containing the most recent Signals. /// </returns> public static List<Signal> Get(RestClient rest) { return Get(rest, String.Empty); }