public async Task DownloadObjects(string streamId, List <string> objectIds, CbObjectDownloaded onObjectCallback) { if (objectIds.Count == 0) { return; } if (objectIds.Count < BATCH_SIZE_GET_OBJECTS) { await DownloadObjectsImpl(streamId, objectIds, onObjectCallback); return; } List <string> crtRequest = new List <string>(); foreach (string id in objectIds) { if (crtRequest.Count >= BATCH_SIZE_GET_OBJECTS) { await DownloadObjectsImpl(streamId, crtRequest, onObjectCallback); crtRequest = new List <string>(); } crtRequest.Add(id); } await DownloadObjectsImpl(streamId, crtRequest, onObjectCallback); }
private async Task DownloadObjectsImpl(string streamId, List <string> objectIds, CbObjectDownloaded onObjectCallback) { // Stopwatch sw = new Stopwatch(); sw.Start(); if (CancellationToken.IsCancellationRequested) { return; } var childrenHttpMessage = new HttpRequestMessage() { RequestUri = new Uri($"/api/getobjects/{streamId}", UriKind.Relative), Method = HttpMethod.Post, }; Dictionary <string, string> postParameters = new Dictionary <string, string>(); postParameters.Add("objects", JsonConvert.SerializeObject(objectIds)); string serializedPayload = JsonConvert.SerializeObject(postParameters); childrenHttpMessage.Content = new StringContent(serializedPayload, Encoding.UTF8, "application/json"); childrenHttpMessage.Headers.Add("Accept", "text/plain"); HttpResponseMessage childrenHttpResponse = await Client.SendAsync(childrenHttpMessage, HttpCompletionOption.ResponseHeadersRead, CancellationToken); childrenHttpResponse.EnsureSuccessStatusCode(); Stream childrenStream = await childrenHttpResponse.Content.ReadAsStreamAsync(); using (childrenStream) { using (var reader = new StreamReader(childrenStream, Encoding.UTF8)) { while (reader.Peek() > 0) { if (CancellationToken.IsCancellationRequested) { return; } var line = reader.ReadLine(); var pcs = line.Split(new char[] { '\t' }, count: 2); onObjectCallback(pcs[0], pcs[1]); } } } // Console.WriteLine($"ServerApi::DownloadObjects({objectIds.Count}) request in {sw.ElapsedMilliseconds / 1000.0} sec"); }