public override async Task <string> Authorize(string userName, string password, string path) { if (string.IsNullOrEmpty(userName)) { throw new ArgumentNullException("userName"); } if (string.IsNullOrEmpty(password)) { throw new ArgumentNullException("password"); } var postParams = CreatePostParams(userName, password); var taskComplete = new TaskCompletionSource <string>(); try { var response = await _webClient.DoPostAsync(path, postParams); var stream = await response.Content.ReadAsStreamAsync(); var xmlSerializer = new XmlSerializer(typeof(LitresAuthorizationDto)); var authorizationDto = (LitresAuthorizationDto)xmlSerializer.Deserialize(stream); taskComplete.SetResult(authorizationDto.Token); } catch (InvalidOperationException exp) { if (exp is WebException) { taskComplete.SetException(exp); } else { taskComplete.SetResult(null); } } return(await taskComplete.Task); }
public async Task <string> BuyBook(CatalogBookItemModel book, string authorizationString) { if (string.IsNullOrEmpty(authorizationString)) { throw new CatalogAuthorizationException(CatalogType.Litres, AUTHORIZATION_URL); } try { var art = string.Empty; var uuid = string.Empty; if (book.Id.Contains("|")) { var idParts = book.Id.Split('|'); art = idParts[0]; uuid = idParts[1]; } else { art = book.Id; } var response = await _webClient.DoPostAsync(BUY_BOOK_URL, CreateAcquisitionParams(art, uuid, EncryptService.Decrypt(authorizationString))); if (response.StatusCode == HttpStatusCode.NotFound) { throw new ReadCatalogException("Unable to read catalog"); } var responseStream = await response.Content.ReadAsStreamAsync(); var xDoc = XDocument.Load(responseStream); if (xDoc.Root.Name == "catalit-purchase-ok") { var format = string.Format("sid={0}&art={1}", EncryptService.Decrypt(authorizationString), art); if (!string.IsNullOrEmpty(uuid)) { format = string.Concat(format, string.Format("uuid={0}", uuid)); } return(string.Concat(DOWNLOAD_BOOK_URL, format)); } if (xDoc.Root.Name == "catalit-purchase-failed") { if (xDoc.Root.Attribute("error").Value == "1") { throw new CatalogNotEnoughMoneyException(CatalogType.Litres, string.Concat(NOT_ENOUGH_MONEY_URL, string.Format("?sid={0}", EncryptService.Decrypt(authorizationString)))); } if (xDoc.Root.Attribute("error").Value == "3") { //throw new CatalogBookAlreadyBoughtException(CatalogType.Litres, book.Id); var format = string.Format("sid={0}&art={1}", EncryptService.Decrypt(authorizationString), art); if (!string.IsNullOrEmpty(uuid)) { format = string.Concat(format, string.Format("uuid={0}", uuid)); } return(string.Concat(DOWNLOAD_BOOK_URL, format)); } } if (xDoc.Root.Name == "catalit-authorization-failed") { throw new CatalogAuthorizationException(CatalogType.Litres, AUTHORIZATION_URL); } } catch (WebException) { throw new ReadCatalogException("Unable to read catalog"); } throw new ReadCatalogException("Unable to read catalog"); }