public Task <IssuuResultSet <T> > GetDataAsync <T>(Action <IssuuRequestOptions> configure = null, CancellationToken cancellationToken = default) where T : IIssuuData { var options = new IssuuRequestOptions(); configure?.Invoke(options); return(GetDataAsync <T>(options, cancellationToken)); }
private Task <IssuuResultSet <IssuuDocument> > SearchAsync(string query, Action <IssuuRequestOptions> configure = null, CancellationToken cancellationToken = default) { var options = new IssuuRequestOptions(); configure?.Invoke(options); return(SearchAsync(query, options, cancellationToken)); }
public Task <IssuuResultSet <IssuuDocument> > GetDocumentsAsync(IssuuRequestOptions options, CancellationToken cancellationToken = default) { return(GetDataAsync <IssuuDocument>(options, cancellationToken)); }
public async Task <IssuuResultSet <T> > GetDataAsync <T>(IssuuRequestOptions options = null, CancellationToken cancellationToken = default) where T : IIssuuData { if (options == null) { options = new IssuuRequestOptions(); } if (typeof(T) == typeof(IssuuDocument) && !string.IsNullOrEmpty(options.SearchQuery)) { return(await SearchAsync(options.SearchQuery, options, cancellationToken) as IssuuResultSet <T>); } var urlParams = new Dictionary <string, string>(); var properties = typeof(T).GetProperties() .Select(p => new { Property = p, Attribute = p.GetCustomAttribute <JsonPropertyAttribute>() }) .Where(p => p.Attribute != null) .ToList(); urlParams["action"] = "issuu.documents.list"; urlParams["apiKey"] = Options.Credentials.ApiKey; urlParams["access"] = "public"; urlParams["responseParams"] = string.Join(",", properties.Select(p => p.Attribute.PropertyName)); urlParams["format"] = "json"; urlParams["documentSortBy"] = options.SortBy; urlParams["resultOrder"] = options.SortOrder.ToString().ToLower(); urlParams["startIndex"] = options.StartIndex.ToString(); urlParams["pageSize"] = options.PageSize.ToString(); urlParams["title"] = "sommer"; var urlParamsSorted = urlParams.OrderBy(p => p.Key).ToList(); var signature = IssuuHasher.CalculateSignature(urlParamsSorted, Options.Credentials.ApiSecret); urlParamsSorted.Add(new KeyValuePair <string, string>("signature", signature)); var url = $@"{Options.ApiUrl}?{string.Join("&", urlParamsSorted.Select(p => p.Key + "=" + p.Value))}"; var cacheKey = $"IssuuResultSet_{url.GetHashCode()}"; var cache = ServiceProvider.GetService <IMemoryCache>(); if (options.Cache && cache != null) { var cachedResultSet = cache.Get(cacheKey) as IssuuResultSet <T>; if (cachedResultSet != null) { return(cachedResultSet); } } using (var client = new HttpClient()) using (var response = await client.GetAsync(url, cancellationToken)) { var resultString = await response.Content.ReadAsStringAsync(); JObject resultJson = JsonConvert.DeserializeObject(resultString) as JObject; var resultContent = resultJson["rsp"]["_content"]; if (resultContent["error"] != null) { var issuuError = resultContent["error"].ToObject <IssuuExceptionDetails>(); throw new IssuuException(issuuError); } var result = resultJson["rsp"]["_content"]["result"].ToObject <IssuuResultSet <T> >(); result.Client = this; result.Url = url; if (options.Cache && cache != null) { cache.Set(cacheKey, result, DateTime.Now.AddMilliseconds(options.CacheTime)); } return(result); } }
private async Task <IssuuResultSet <IssuuDocument> > SearchAsync(string query, IssuuRequestOptions options = null, CancellationToken cancellationToken = default) { if (options == null) { options = new IssuuRequestOptions(); } var urlParams = new Dictionary <string, string>(); urlParams[options.SearchField] = query; if (!string.IsNullOrEmpty(Options.Credentials.ApiUsername)) { urlParams["username"] = Options.Credentials.ApiUsername; } urlParams["startIndex"] = options.StartIndex.ToString(); urlParams["pageSize"] = options.PageSize.ToString(); urlParams["responseParams"] = "*"; var url = $@"{Options.SearchApiUrl}?{string.Join("&", urlParams.Select(p => p.Key + "=" + p.Value))}"; var cacheKey = $"IssuuResultSet_{url.GetHashCode()}"; var cache = ServiceProvider.GetService <IMemoryCache>(); if (options.Cache && cache != null) { var cachedResultSet = cache.Get(cacheKey) as IssuuResultSet <IssuuDocument>; if (cachedResultSet != null) { return(cachedResultSet); } } using (var client = new HttpClient()) using (var response = await client.GetAsync(url, cancellationToken)) { var resultString = await response.Content.ReadAsStringAsync(); JObject resultJson = JsonConvert.DeserializeObject(resultString) as JObject; var resultContent = resultJson["response"]; var searchResultSet = resultContent.ToObject <IssuuSearchResultSet>(); var result = new IssuuResultSet <IssuuDocument>(); result.Client = this; result.Url = url; result.Results = searchResultSet.Docs .Select(d => new IssuuResult <IssuuDocument>(new IssuuDocument(d))) .ToArray(); result.PageSize = options.PageSize; result.TotalCount = searchResultSet.NumFound; result.StartIndex = searchResultSet.Start; if (options.Cache && cache != null) { cache.Set(cacheKey, result, DateTime.Now.AddMilliseconds(options.CacheTime)); } return(result); } }