/// <summary> /// Get embeds of all documents. Max pagesize is 100. If there are more elements,additional request is needed. /// Requests are allowed at rate 1 request/second for free account /// <see cref="http://developers.issuu.com/api/limits.html"/>. /// This method takes first embed of all documents,because of dataConfigId for dropdown. /// </summary> /// <param name="docDict"> /// Dictionary with common-documents. /// </param> /// <returns> /// The /// <see> /// <cref>Dictionary</cref> /// </see> /// . /// </returns> public Dictionary<string, DocumentEmbed> GetEmbedsOfDocuments(Dictionary<string, DocumentCommon> docDict) { if (docDict == null) { throw new ArgumentException(TextResources.IssuDocument_Core_Exception_MissingArgument_DocumentCommon_Message, "docDict"); } Dictionary<string, DocumentEmbed> resultDict = new Dictionary<string, DocumentEmbed>(); bool more; int counter = 0; int startIndex = 0; do { // prepare parameters Dictionary<RequestParameters.RequestParamters, string> paramDict = new Dictionary<RequestParameters.RequestParamters, string> { { RequestParameters.RequestParamters.action, IssuuListEmbedsActionParameterValue }, { RequestParameters.RequestParamters.apiKey, _issuuConfig.APIKey }, { RequestParameters.RequestParamters.pageSize, PageSize.ToString(CultureInfo.InvariantCulture) }, { RequestParameters.RequestParamters.startIndex, startIndex.ToString(CultureInfo.InvariantCulture) } }; // build request string response = DoHttpRequest(paramDict); // parse xml to get data XDocument parsedDoc = XDocument.Parse(response); IEnumerable<XElement> result = parsedDoc.Descendants("rsp").Descendants("result"); // is the pagesize big enough or there are more elements more = bool.Parse(result.First().Attribute("more").Value); IEnumerable<XElement> docsElements = parsedDoc.Descendants("rsp").Descendants("result").Descendants("documentEmbed"); // try to get embed of document foreach (XElement docsElement in docsElements) { // deleted embeds don't have documentId -> find first one with id string docId = docsElement.Attribute("documentId") != null ? docsElement.Attribute("documentId").Value : null; if (docId == null) { continue; } if (docDict.ContainsKey(docId)) { DocumentEmbed docEmbed = new DocumentEmbed(docDict[docId]); docDict.Remove(docId); docEmbed.EmbedId = docsElement.Attribute("id").Value; docEmbed.Height = docsElement.Attribute("height").Value; docEmbed.Width = docsElement.Attribute("width").Value; docEmbed.Created = docsElement.Attribute("created").Value; docEmbed.DataConfigId = docsElement.Attribute("dataConfigId").Value; resultDict.Add(docEmbed.DataConfigId, docEmbed); } } // if there's no embed found,check if there are more embedds and get them if (more) { // prepare parameters for next iterations // with premium licences,limits are 2 requests/sec or 3 requests/sec-> wait time needs to be adjusted (http://developers.issuu.com/api/limits.html) int requestsProSecond = int.Parse(IssuuDocumentConfigurationFacade.Configuration.RequestsProSecond, CultureInfo.InvariantCulture); int sleepTime = (int)Math.Floor((decimal)(1000.0 / requestsProSecond)); Thread.Sleep(sleepTime + 1); counter++; startIndex = counter * PageSize; } } while (more); // if there are still unprocessed documents (without embedds),create embed for them if (docDict.Any()) { foreach (KeyValuePair<string, DocumentCommon> documentCommonKeyValue in docDict) { DocumentEmbed newDocEmbed = CreateEmbedForDocument(documentCommonKeyValue.Value); resultDict.Add(newDocEmbed.DataConfigId, newDocEmbed); int requestsProSecond = int.Parse(IssuuDocumentConfigurationFacade.Configuration.RequestsProSecond, CultureInfo.InvariantCulture); int sleepTime = (int)Math.Floor((decimal)(1000.0 / requestsProSecond)); Thread.Sleep(sleepTime + 1); } } return resultDict; }
/// <summary> /// Create embed for documents without embeds to have an dataConfigId (embed). /// </summary> /// <param name="document"> /// Common documents for embed. /// </param> /// <returns> /// The <see cref="DocumentEmbed"/>. /// </returns> private DocumentEmbed CreateEmbedForDocument(DocumentCommon document) { // prepare parameters Dictionary<RequestParameters.RequestParamters, string> paramDict = new Dictionary<RequestParameters.RequestParamters, string> { { RequestParameters.RequestParamters.action, IssuuAddActionParameterValue }, { RequestParameters.RequestParamters.apiKey, _issuuConfig.APIKey }, { RequestParameters.RequestParamters.documentId, document.DocumentId }, { RequestParameters.RequestParamters.readerStartPage, "1" }, { RequestParameters.RequestParamters.width, Width.ToString(CultureInfo.InvariantCulture) }, { RequestParameters.RequestParamters.height, Height.ToString(CultureInfo.InvariantCulture) }, }; // build request string response = DoHttpRequest(paramDict); // parse xml to get data XDocument parsedDoc = XDocument.Parse(response); IEnumerable<XElement> docsElements = parsedDoc.Descendants("rsp").Descendants("documentEmbed"); // dictionary with embedId as key,and documents as value,in this case,there should be only one documentEmbed foreach (XElement docsElement in docsElements) { // existing documents string docId = docsElement.Attribute("documentId") != null ? docsElement.Attribute("documentId").Value : null; if (docId == null) { continue; } DocumentEmbed docEmbed = new DocumentEmbed(document); docEmbed.EmbedId = docsElement.Attribute("id").Value; docEmbed.Height = docsElement.Attribute("height").Value; docEmbed.Width = docsElement.Attribute("width").Value; docEmbed.Created = docsElement.Attribute("created").Value; docEmbed.DataConfigId = docsElement.Attribute("dataConfigId").Value; return docEmbed; } return null; }