public List<User> GetAllUsers(int page) { string szReq = Resources.USERS_ALL + "?page=" + page.ToString(); string data = Method.Get(szReq, this.Session); List<User> users = new List<User>(); XmlDocument xdoc = new XmlDocument(); xdoc.LoadXml(data); foreach (XmlNode node in xdoc.SelectNodes("/response/response")) { User u = new User(); u.JobTitle = node.SelectSingleNode("job-title").InnerText; u.FullName = node.SelectSingleNode("full-name").InnerText; u.MugshotUrl = new Uri(node.SelectSingleNode("mugshot-url").InnerText); u.Name = node.SelectSingleNode("name").InnerText; u.Url = new Uri(node.SelectSingleNode("url").InnerText); u.WebUrl = new Uri(node.SelectSingleNode("web-url").InnerText); u.Id = int.Parse(node.SelectSingleNode("id").InnerText); u.Extention = null; users.Add(u); } return users; }
private MessageObjects GetMessageObjects(string data) { XmlDocument xdoc = new XmlDocument(); xdoc.LoadXml(data); MessageObjects objects = new MessageObjects(); List<Message> messages = new List<Message>(); foreach (XmlNode mnode in xdoc.SelectNodes("/response/messages/message")) { Message message = new Message(); message.WebUrl = new Uri(mnode.SelectSingleNode("web-url").InnerText); message.ClientType = mnode.SelectSingleNode("client-type").InnerText; MessageBody mb = new MessageBody(); mb.Parsed = mnode.SelectSingleNode("body/parsed").InnerText; mb.Plain = mnode.SelectSingleNode("body/plain").InnerText; List<Uri> urls = new List<Uri>(); foreach (XmlNode unode in mnode.SelectNodes("urls/url")) { Uri u = new Uri(unode.InnerText); urls.Add(u); } mb.Urls = urls; message.Body = mb; message.SenderType = (SenderType)Enum.Parse(Type.GetType("Yammer.SenderType"), mnode.SelectSingleNode("sender-type").InnerText.ToUpper()); if (mnode.SelectSingleNode("client-url").InnerText != string.Empty) { message.ClientUrl = new Uri(mnode.SelectSingleNode("client-url").InnerText); } message.Id = int.Parse(mnode.SelectSingleNode("id").InnerText); message.IsSystemMessage = (mnode.SelectSingleNode("system-message").InnerText == "true"); message.Url = new Uri(mnode.SelectSingleNode("url").InnerText); message.ThreadId = int.Parse(mnode.SelectSingleNode("thread-id").InnerText); if (mnode.SelectSingleNode("replied-to-id").InnerText != string.Empty) { message.RepliedToId = int.Parse(mnode.SelectSingleNode("replied-to-id").InnerText); } message.SenderId = int.Parse(mnode.SelectSingleNode("sender-id").InnerText); message.MessageType = (MessageType)Enum.Parse(Type.GetType("Yammer.MessageType"), mnode.SelectSingleNode("message-type").InnerText.ToUpper()); message.CreatedAt = DateTime.Parse(mnode.SelectSingleNode("created-at").InnerText); messages.Add(message); } List<Reference> references = new List<Reference>(); foreach (XmlNode rnode in xdoc.SelectNodes("/response/references/reference")) { Reference reference = new Reference(); reference.Id = int.Parse(rnode.SelectSingleNode("id").InnerText); reference.ObjectType = (ObjectType)Enum.Parse(Type.GetType("Yammer.ObjectType"), rnode.SelectSingleNode("type").InnerText.ToUpper()); switch (reference.ObjectType) { case ObjectType.MESSAGE: Message msg = new Message(); msg.Id = reference.Id; msg.WebUrl = new Uri(rnode.SelectSingleNode("web-url").InnerText); msg.SenderType = (SenderType)Enum.Parse(Type.GetType("Yammer.SenderType"), rnode.SelectSingleNode("sender-type").InnerText.ToUpper()); msg.Url = new Uri(rnode.SelectSingleNode("url").InnerText); msg.ThreadId = int.Parse(rnode.SelectSingleNode("thread-id").InnerText); if (rnode.SelectSingleNode("replied-to-id").InnerText != string.Empty) { msg.RepliedToId = int.Parse(rnode.SelectSingleNode("replied-to-id").InnerText); } msg.SenderId = int.Parse(rnode.SelectSingleNode("sender-id").InnerText); msg.CreatedAt = DateTime.Parse(rnode.SelectSingleNode("created-at").InnerText); msg.Body = new MessageBody { Plain = rnode.SelectSingleNode("body/plain").InnerText }; reference.Object = msg; break; case ObjectType.USER: User usr = new User(); usr.Id = reference.Id; usr.FullName = rnode.SelectSingleNode("full-name").InnerText; usr.JobTitle = rnode.SelectSingleNode("job-title").InnerText; usr.MugshotUrl = new Uri(rnode.SelectSingleNode("mugshot-url").InnerText); usr.Name = rnode.SelectSingleNode("name").InnerText; usr.Url = new Uri(rnode.SelectSingleNode("url").InnerText); reference.Object = usr; break; case ObjectType.TAG: Tag tag = new Tag(); tag.Id = reference.Id; tag.WebUrl = new Uri(rnode.SelectSingleNode("web-url").InnerText); tag.Name = rnode.SelectSingleNode("name").InnerText; tag.Url = new Uri(rnode.SelectSingleNode("url").InnerText); reference.Object = tag; break; case ObjectType.THREAD: Thread thread = new Thread(); thread.Id = reference.Id; thread.Url = new Uri(rnode.SelectSingleNode("url").InnerText); thread.WebUrl = new Uri(rnode.SelectSingleNode("web-url").InnerText); Stats stats = new Stats(); stats.Updates = int.Parse(rnode.SelectSingleNode("stats/updates").InnerText); if (rnode.SelectSingleNode("stats/latest-reply-at").InnerText != string.Empty) { stats.LatestReplyAt = DateTime.Parse(rnode.SelectSingleNode("stats/latest-reply-at").InnerText); } thread.Stats = stats; reference.Object = thread; break; default: // NOP break; } references.Add(reference); } objects.Messages = messages; objects.References = references; return objects; }
private User GetCurrentUser(OAuthKey auth, WebProxy proxy) { #region OAuth OAuthBase oAuth = new OAuthBase(); string nonce = oAuth.GenerateNonce(); string timeStamp = oAuth.GenerateTimeStamp(); string nurl, nrp; string query = Resources.USERS_CURRENT + "?"; Uri uri = new Uri(query); string sig = oAuth.GenerateSignature( uri, auth.ConsumerKey, auth.ConsumerSecret, auth.TokenKey, auth.TokenSecret, "GET", timeStamp, nonce, OAuthBase.SignatureTypes.PLAINTEXT, out nurl, out nrp); // sig = HttpUtility.UrlEncode(sig); StringBuilder sb = new StringBuilder(uri.ToString()); sb.AppendFormat("oauth_consumer_key={0}&", auth.ConsumerKey); sb.AppendFormat("oauth_token={0}&", auth.TokenKey); // sb.AppendFormat("oauth_signature_method={0}&", "HMAC-SHA1"); sb.AppendFormat("oauth_signature_method={0}&", "PLAINTEXT"); sb.AppendFormat("oauth_timestamp={0}&", timeStamp); sb.AppendFormat("oauth_nonce={0}&", nonce); sb.AppendFormat("oauth_version={0}&", "1.0"); sb.AppendFormat("oauth_signature={0}", sig); query = sb.ToString(); #endregion HttpWebRequest req = (HttpWebRequest)WebRequest.Create(query); req.Method = "GET"; req.PreAuthenticate = true; req.Accept = "text/xml, application/xml"; req.Proxy = proxy; WebResponse res = req.GetResponse(); StreamReader reader = new StreamReader(res.GetResponseStream(), Encoding.UTF8); string data = reader.ReadToEnd(); reader.Close(); res.Close(); #region XmlDocument User u = new User(); XmlDocument xdoc = new XmlDocument(); xdoc.LoadXml(data); u.JobTitle = xdoc.SelectSingleNode("/response/job-title").InnerText; u.FullName = xdoc.SelectSingleNode("/response/full-name").InnerText; u.MugshotUrl = new Uri(xdoc.SelectSingleNode("/response/mugshot-url").InnerText); u.Name = xdoc.SelectSingleNode("/response/name").InnerText; u.Url = new Uri(xdoc.SelectSingleNode("/response/url").InnerText); u.WebUrl = new Uri(xdoc.SelectSingleNode("/response/web-url").InnerText); u.Id = int.Parse(xdoc.SelectSingleNode("/response/id").InnerText); #region extend UserExtention ux = new UserExtention(); ux.NetworkName = xdoc.SelectSingleNode("/response/network-name").InnerText; ux.NetworkId = int.Parse(xdoc.SelectSingleNode("/response/network-id").InnerText); ux.BirthDate = xdoc.SelectSingleNode("/response/birth-date").InnerText; // ux.HireDate = DateTime.ParseExact(xdoc.SelectSingleNode("/response/hire-date").InnerText, "yyyy-MM-dd", null); Status s = new Status(); s.Followers = int.Parse(xdoc.SelectSingleNode("/response/stats/followers").InnerText); s.Following = int.Parse(xdoc.SelectSingleNode("/response/stats/following").InnerText); s.Updates = int.Parse(xdoc.SelectSingleNode("/response/stats/updates").InnerText); ux.Status = s; Location l = new Location(); //l.Id = int.Parse(xdoc.SelectSingleNode("/response/location/id").InnerText); //l.UserId = int.Parse(xdoc.SelectSingleNode("/response/location/user-id").InnerText); //l.Name = xdoc.SelectSingleNode("/response/location/name").InnerText; //l.Value = xdoc.SelectSingleNode("/response/location/value").InnerText; //l.CreatedAt = DateTime.ParseExact( // xdoc.SelectSingleNode("/response/location/created-at").InnerText, // "yyyy-MM-ddTHH:mm:ssZ", null); //l.UpdatedAt = DateTime.ParseExact( // xdoc.SelectSingleNode("/response/location/updated-at").InnerText, // "yyyy-MM-ddTHH:mm:ssZ", null); ux.Location = l; Contact c = new Contact(); List<EmailAddress> emails = new List<EmailAddress>(); foreach (XmlNode aNode in xdoc.SelectNodes("/response/contact/email-address")) { EmailAddress email = new EmailAddress(); email.Type = aNode.SelectSingleNode("type").InnerText; email.Address = aNode.SelectSingleNode("address").InnerText; emails.Add(email); } c.EmailAddresses = emails; List<PhoneNumber> phones = new List<PhoneNumber>(); foreach (XmlNode pNode in xdoc.SelectNodes("/response/contact/phone-number")) { PhoneNumber phone = new PhoneNumber(); phone.Type = pNode.SelectSingleNode("type").InnerText; phone.Number = pNode.SelectSingleNode("number").InnerText; phones.Add(phone); } c.PhoneNumbers = phones; Im im = new Im(); im.UserName = xdoc.SelectSingleNode("/response/contact/im/username").InnerText; im.Provider = xdoc.SelectSingleNode("/response/contact/im/provider").InnerText; c.Im = im; ux.Contact = c; u.Extention = ux; #endregion #endregion #region case LINQ //var xml = XElement.Parse(Encoding.UTF8.GetString(data)); //var result = from response in xml.Root.Elements() // select new User // { // Id = int.Parse(response.Element("id").Value), // NetworkName = response.Element("network-name").Value, // Name = response.Element("name").Value, // FullName = response.Element("full-name").Value, // JobTitle = response.Element("job-title").Value, // NetworkId = int.Parse(response.Element("network-id").Value), // BirthDate = response.Element("birth-date").Value, // Url = new Uri(response.Element("url").Value), // MugshotUrl = new Uri(response.Element("mugshot-url").Value), // HireDate = DateTime.ParseExact(response.Element("hire-date").Value, "yyyy-MM-dd", null), // WebUrl = new Uri(response.Element("web-url").Value), // }; #endregion return u; }
private static ObjectType ConvertReferenceType(object reference, out User user, out Tag tag, out Thread thread) { user = null; tag = null; thread = null; user = reference as User; ObjectType type = ObjectType.NONE; bool converted = false; if (user != null) { converted = true; type = ObjectType.USER; } if (!converted) { thread = reference as Thread; if (thread != null) { converted = true; type = ObjectType.THREAD; } if (!converted) { tag = reference as Tag; if (tag != null) { converted = true; type = ObjectType.TAG; } } } return type; }
public User GetUserById(string id) { int me = this.Session.CurrentUser.Id; string szReq = Resources.USERS_TARGET + me.ToString() + ".xml"; string data = Method.Get(szReq, this.Session); User u = new User(); XmlDocument xdoc = new XmlDocument(); xdoc.LoadXml(data); return u; }
public User GetUserByName(string name) { User u = new User(); return u; }