public UscfMember LookupById(LookupByIdRequest lookupByIdRequest) { var lookupResult = MobileApi.LookupById(lookupByIdRequest); var mobileApiResult = new List <MobileApiResult>(new[] { lookupResult }); var searchResult = ProcessSearchResults(mobileApiResult).FirstOrDefault(); return(searchResult == null ? null : new UscfMember { FirstName = searchResult.FirstName, LastName = searchResult.LastName, Suffix = searchResult.Suffix, StateOrCountry = searchResult.StateOrCountry, UscfId = lookupResult.UscfId, MembershipExpirationDate = lookupResult.MembershipExpirationDate, MembershipStatus = lookupResult.MembershipStatus, Comments = lookupResult.Comments, BlitzRating = lookupResult.BlitzRating, RegularRating = lookupResult.RegularRating, QuickRating = lookupResult.QuickRating }); }
public void Should_LookupById_Success() { var lookupByIdRequest = new LookupByIdRequest() { UscfId = 15007412 }; // var resultApi = UsChessApi.MobileApi.SearchByName(searchByNameRequest); var result = new Api().LookupById(lookupByIdRequest); var ss = result; }
/// <summary> /// /// </summary> /// <param name="lookupByIdRequest"></param> /// <returns></returns> public static MobileApiResult LookupById(LookupByIdRequest lookupByIdRequest) { if (!lookupByIdRequest.IsValid) { throw lookupByIdRequest.Exception; } var client = new RestClient(BaseUrl); var request = new RestRequest(LookupByIdUrl, Method.GET) .AddParameter("id", lookupByIdRequest.UscfId, ParameterType.UrlSegment) .AddParameter("date", lookupByIdRequest.AsOfDate.HasValue ? lookupByIdRequest.AsOfDate.Value.ToString("yyyy-MM-dd") : "", ParameterType.UrlSegment); var response = client.Execute(request); if (response.ResponseStatus == ResponseStatus.Completed) { var htmlContent = response.Content; var htmlDocument = new HtmlAgilityPack.HtmlDocument(); htmlDocument.LoadHtml(htmlContent); var rows = htmlDocument.DocumentNode.SelectNodes("//input"); // get as much as we can from the input elements. All of them are required except for a comment. // If any of them are missing just return null var fullNameNode = rows.FirstOrDefault(row => row.Attributes["name"].Value == "memname"); if (fullNameNode == null) { return(null); } var fullNameString = fullNameNode.Attributes["value"].Value; if (string.IsNullOrWhiteSpace(fullNameString)) { return(null); } var stateNode = rows.FirstOrDefault(row => row.Attributes["name"].Value == "state_country"); if (stateNode == null) { return(null); } // state is optional. var state = stateNode.Attributes["value"].Value; var expDateNode = rows.FirstOrDefault(row => row.Attributes["name"].Value == "memexpdt"); if (expDateNode == null) { return(null); } DateTime expirationDate; if (!DateTime.TryParse(expDateNode.Attributes["value"].Value, out expirationDate)) { return(null); } var ratingNode = rows.FirstOrDefault(row => row.Attributes["name"].Value == "rating1"); if (ratingNode == null) { return(null); } var ratingString = ratingNode.Attributes["value"].Value; if (string.IsNullOrWhiteSpace(ratingString)) { return(null); } var rating = ParseRating(ratingString); var quickRatingNode = rows.FirstOrDefault(row => row.Attributes["name"].Value == "rating2"); if (quickRatingNode == null) { return(null); } var quickRatingString = quickRatingNode.Attributes["value"].Value; if (string.IsNullOrWhiteSpace(quickRatingString)) { return(null); } var quickRating = ParseRating(quickRatingString); var blitzRatingNode = rows.FirstOrDefault(row => row.Attributes["name"].Value == "rating3"); if (blitzRatingNode == null) { return(null); } var blitzRatingString = blitzRatingNode.Attributes["value"].Value; if (string.IsNullOrWhiteSpace(blitzRatingString)) { return(null); } var blitzRating = ParseRating(blitzRatingString); // get the comments var commentNode = htmlDocument.DocumentNode .SelectSingleNode("//form") .SelectNodes("//tr")[1] .SelectSingleNode("//font[@color='red']"); var comment = commentNode != null?commentNode.InnerText.Trim() : string.Empty; return(new MobileApiResult { FullName = fullNameString, UscfId = lookupByIdRequest.UscfId, StateOrCountry = state, MembershipExpirationDate = expirationDate, RegularRating = rating, QuickRating = quickRating, BlitzRating = blitzRating, Comments = comment, MembershipStatus = ParseMembershipStatus(expirationDate, comment) }); } return(null); }