public async Task <IEnumerable <TModel> > Query <TModel>(IQueryContext query) { query = query ?? new QueryContext <TModel>(); if (query.PageLimit != null && query.PageLimit <= 0) { return(Enumerable.Empty <TModel>()); } if (query.PageSize != null && query.PageSize <= 0) { return(Enumerable.Empty <TModel>()); } var include = ModelRegistry.GetInclude <TModel>(); var request = HttpRequestBuilder.QueryResources(query, include); var response = await HttpClient.SendAsync(request).ConfigureAwait(false); HttpResponseListener.QueryResources(response.StatusCode, query); if (response.StatusCode == HttpStatusCode.NotFound) { return(Enumerable.Empty <TModel>()); } response.CheckStatusCode(); var root = await response.GetContentModel <ResourceRootCollection>(JsonSettings); var createData = Task.WhenAll(root.Data.Select(x => Task.Run(() => { var dataModel = CreateResourceModel <TModel>(x); Cache.Update(x.Id, dataModel); return(dataModel); }))); var createIncludes = root.Included != null ? Task.WhenAll(root.Included.Select(x => Task.Run(() => { var includedModel = CreateResourceModel(x); Cache.Update(x.Id, includedModel); }))) : Task.CompletedTask; await Task.WhenAll(createIncludes, createData); return(createData.Result); }
public async Task <TModel> Get <TModel>(Guid id) { ThrowIfDisposed(); var model = Cache.Retrieve <TModel>(id); if (model != null) { return(model); } var resourceType = ModelRegistry.GetResourceType <TModel>(); var include = ModelRegistry.GetInclude <TModel>(); var request = HttpRequestBuilder.GetResource(id, resourceType, include); var response = await HttpClient.SendAsync(request).ConfigureAwait(false); HttpResponseListener.GetResource(response.StatusCode, id, resourceType); if (response.StatusCode == HttpStatusCode.NotFound) { return(default(TModel)); // null } response.CheckStatusCode(); var root = await response.GetContentModel <ResourceRootSingle>(JsonSettings); model = CreateResourceModel <TModel>(root.Data); Cache.Update(id, model); if (root.Included != null) { await Task.WhenAll(root.Included.Select(x => Task.Run(() => { var includedModel = CreateResourceModel(x); Cache.Update(x.Id, includedModel); }))); } return(model); }