public void Merge(UponorResponseContainer container) { foreach ((int @object, UponorProperties property, object value) in container.GetValues()) { AddResponse(@object, property, value); } }
public async Task <UponorResponseContainer> ReadValues(IEnumerable <int> objects, IEnumerable <UponorProperties> properties, CancellationToken token = default) { ICollection <UponorProperties> propsList = properties as ICollection <UponorProperties> ?? properties.ToList(); List <List <int> > batches = new List <List <int> >(); List <int> current = new List <int>(); batches.Add(current); int objectsPerRequest = (int)Math.Max(1, Math.Round(PropertiesPerRequest * 1f / propsList.Count)); foreach (int o in objects) { current.Add(o); if (current.Count >= objectsPerRequest) { batches.Add(current = new List <int>()); } } UponorResponseContainer responseContainer = new UponorResponseContainer(); List <Task <HttpResponseMessage> > tasks = new List <Task <HttpResponseMessage> >(); if (batches.All(x => !x.Any())) { // Early exit, if no requests are to be made return(responseContainer); } foreach (List <int> batch in batches) { UponorRequest request = new UponorRequest(); request.Id = Interlocked.Increment(ref _nextRequestInteger); foreach (int objectId in batch) { UponorObject obj = new UponorObject(); obj.Id = objectId.ToString(); foreach (UponorProperties property in propsList) { obj.Properties.Add((int)property, UponorValueContainer.EmptyValueContainer); } request.Params.Objects.Add(obj); } string json = JsonConvert.SerializeObject(request); HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, "api"); requestMessage.Content = new StringContent(json); Task <HttpResponseMessage> requestTask = SendRequest(requestMessage, token); tasks.Add(requestTask); } // Await each response foreach (Task <HttpResponseMessage> task in tasks) { HttpResponseMessage res = await task; string resJson = res.Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult(); // Hack json to handle empty properties resJson = resJson.Replace(":}", ":\"\"}"); UponorResponse <UponorParams> resp = JsonConvert.DeserializeObject <UponorResponse <UponorParams> >(resJson); foreach (UponorObject o in resp.Result.Objects) { foreach (KeyValuePair <int, UponorValueContainer> prop in o.Properties) { responseContainer.AddResponse(Convert.ToInt32(o.Id), (UponorProperties)prop.Key, prop.Value.Value); } } } return(responseContainer); }