public HttpResponseMessage Get(string file1, string file2) { var fileA = new StreamContent(new FileStream(Path.Combine(_root, file1), FileMode.Open)); fileA.Headers.ContentType = new MediaTypeHeaderValue("text/html"); var fileB = new StreamContent(new FileStream(Path.Combine(_root, file2), FileMode.Open)); fileA.Headers.ContentType = new MediaTypeHeaderValue("text/html"); var result = new HttpResponseMessage(HttpStatusCode.OK); var content = new MultipartContent {fileA, fileB}; result.Content = content; return result; }
public Task<HttpResponseMessage> PostNotification (BlackberryNotification notification) { var c = new MultipartContent ("related", Configuration.Boundary); c.Headers.Remove("Content-Type"); c.Headers.TryAddWithoutValidation("Content-Type", "multipart/related; boundary=" + Configuration.Boundary + "; type=application/xml"); var xml = notification.ToPapXml (); c.Add (new StringContent (xml, Encoding.UTF8, "application/xml")); var bc = new ByteArrayContent(notification.Content.Content); bc.Headers.Add("Content-Type", notification.Content.ContentType); foreach (var header in notification.Content.Headers) bc.Headers.Add(header.Key, header.Value); c.Add(bc); return PostAsync (Configuration.SendUrl, c); }
private static async Task RunTest(string baseAddress) { HttpClient client = new HttpClient(); HttpRequestMessage batchRequest = new HttpRequestMessage( HttpMethod.Post, baseAddress + "/api/batch" ); MultipartContent batchContent = new MultipartContent("batch"); batchRequest.Content = batchContent; CreateBatchedRequest(baseAddress, batchContent); using (var stdout = Console.OpenStandardOutput()) { Console.WriteLine("<<< REQUEST >>>"); Console.WriteLine(); Console.WriteLine(batchRequest); Console.WriteLine(); await batchContent.CopyToAsync(stdout); Console.WriteLine(); var batchResponse = await client.SendAsync(batchRequest); Console.WriteLine("<<< RESPONSE >>>"); Console.WriteLine(); Console.WriteLine(batchResponse); Console.WriteLine(); await batchResponse.Content.CopyToAsync(stdout); Console.WriteLine(); Console.WriteLine(); } }
public async Task<Result> PostData(Uri uri, MultipartContent header, StringContent content) { var httpClient = new HttpClient(); try { if (!string.IsNullOrEmpty(AuthenticationToken)) { httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AuthenticationToken); } HttpResponseMessage response; if (header == null) { if(content == null) content = new StringContent(string.Empty); response = await httpClient.PostAsync(uri, content); } else { response = await httpClient.PostAsync(uri, header); } var responseContent = await response.Content.ReadAsStringAsync(); return new Result(response.IsSuccessStatusCode, responseContent); } catch (Exception ex) { throw new WebException("Kinder Chat API Error: Service error", ex); } }
public void ReadFromStreamAsyncShouldReturnUploadImageDataTest() { ImageMultipartMediaFormatter target = new ImageMultipartMediaFormatter(); Type type = typeof(UploadImageData); Stream readStream = null; MultipartContent content = new MultipartContent("form-data"); byte[] content_bytes=new byte[] { 10, 11, 12 }; StreamContent content_part = new StreamContent(new MemoryStream(content_bytes)); content_part.Headers.Add("Content-Disposition", @"form-data; name=fieldName; filename=image.jpg"); content_part.Headers.Add("Content-Type", "image/jpeg"); content.Add(content_part); IFormatterLogger formatterLogger = null; var actual = target.ReadFromStreamAsync(type, readStream, content, formatterLogger); var actualResult = actual.Result; Assert.IsInstanceOfType(actualResult,typeof(UploadImageData)); Assert.AreEqual(3, (actualResult as UploadImageData).ImageBuffer.Length); for (int ind = 0; ind < 3; ind++) { Assert.AreEqual(content_bytes[ind], (actualResult as UploadImageData).ImageBuffer[ind]); } Assert.AreEqual("image.jpg", (actualResult as UploadImageData).FileName); Assert.AreEqual("image/jpeg", (actualResult as UploadImageData).ImageType); }
public async Task ExecuteAsync(string accessToken, string ebookId, int partCount, Func<Stream, Task> streamHandler) { var message = new HttpRequestMessage(HttpMethod.Post, new Uri("http://localhost:20394/api/batch")); var content = new MultipartContent("mixed"); message.Content = content; for (int i = 0; i < partCount; ++i) { content.Add(new HttpMessageContent( new HttpRequestMessage(HttpMethod.Get, new Uri(string.Format("http://localhost:20394/api/ebooks/ebook/{0}/part/{1}", ebookId, i))))); } using (var client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); var response = await client.SendAsync(message); var streamProvider = await response.Content.ReadAsMultipartAsync(); foreach (var partialContent in streamProvider.Contents) { var part = await partialContent.ReadAsHttpResponseMessageAsync(); var partStream = await part.Content.ReadAsStreamAsync(); await streamHandler(partStream); } } }
static void Main(string[] args) { var client = new HttpClient(); var batchRequest = new HttpRequestMessage( HttpMethod.Post, "http://fsatnav:8080/api/batch" ); var batchContent = new MultipartContent("mixed"); batchRequest.Content = batchContent; batchContent.Add( new HttpMessageContent( new HttpRequestMessage( HttpMethod.Get, "http://localhost:8080/api/products" ) ) ); batchContent.Add( new HttpMessageContent( new HttpRequestMessage( HttpMethod.Put, "http://localhost:8080/api/products" ) { Content = new StringContent("{\"Name\":\"Product X\",\"StockQuantity\":300}", Encoding.UTF8, "application/json") } ) ); using (Stream stdout = Console.OpenStandardOutput()) { Console.WriteLine("<<< REQUEST >>>"); Console.WriteLine(); Console.WriteLine(batchRequest); Console.WriteLine(); batchContent.CopyToAsync(stdout).Wait(); Console.WriteLine(); var batchResponse = client.SendAsync(batchRequest).Result; Console.WriteLine("<<< RESPONSE >>>"); Console.WriteLine(); Console.WriteLine(batchResponse); Console.WriteLine(); batchResponse.Content.CopyToAsync(stdout).Wait(); Console.WriteLine(); Console.WriteLine(); } Console.ReadLine(); }
public void Add() { var m = new MultipartContent ("a", "b"); var other = new MultipartContent ("2", "44"); other.Headers.Expires = new DateTimeOffset (2020, 11, 30, 19, 55, 22, TimeSpan.Zero); m.Add (other); Assert.AreEqual ("multipart/a", m.Headers.ContentType.MediaType, "#1"); Assert.AreEqual (114, m.Headers.ContentLength, "#2"); Assert.AreEqual ("--b\r\nContent-Type: multipart/2; boundary=\"44\"\r\nExpires: Mon, 30 Nov 2020 19:55:22 GMT\r\n\r\n--44\r\n\r\n--44--\r\n\r\n--b--\r\n", m.ReadAsStringAsync ().Result, "#3"); Assert.AreEqual (other, m.First (), "#4"); }
static void Main(string[] args) { var client = new HttpClient(); var batchRequest = new HttpRequestMessage( HttpMethod.Post, "http://localhost:52857/api/$batch" ); var batchContent = new MultipartContent("mixed"); batchRequest.Content = batchContent; batchContent.Add( new HttpMessageContent( new HttpRequestMessage( HttpMethod.Get, "http://localhost:52857/api/TestClassOne" ) ) ); batchContent.Add( new HttpMessageContent( new HttpRequestMessage( HttpMethod.Get, "http://localhost:52857/api/TestClassTwo" ) ) ); using (Stream stdout = Console.OpenStandardOutput()) { Console.WriteLine("<<< REQUEST >>>"); Console.WriteLine(); Console.WriteLine(batchRequest); Console.WriteLine(); batchContent.CopyToAsync(stdout).Wait(); Console.WriteLine(); var batchResponse = client.SendAsync(batchRequest).Result; Console.WriteLine("<<< RESPONSE >>>"); Console.WriteLine(); Console.WriteLine(batchResponse); Console.WriteLine(); batchResponse.Content.CopyToAsync(stdout).Wait(); Console.WriteLine(); Console.WriteLine(); } Console.ReadLine(); }
public void BeginBatch() { _batchId = Guid.NewGuid().ToString(); _changesetId = Guid.NewGuid().ToString(); this.Request = CreateRequest(CreateRequestUrl(FluentCommand.BatchLiteral)); this.Request.Method = RestVerbs.POST; var batchContent = new MultipartContent("mixed", "batch_" + _batchId); this.Request.Content = batchContent; this.Request.ContentType = "application/http"; var changesetContent = new MultipartContent("mixed", "changeset_" + _changesetId); batchContent.Add(changesetContent); _content = changesetContent; }
public void Add_2() { var m = new MultipartContent ("a", "X"); var other = new MultipartContent ("2", "2a"); m.Add (other); var other2 = new MultipartContent ("3", "3a"); other2.Headers.Add ("9", "9n"); m.Add (other2); Assert.AreEqual ("multipart/a", m.Headers.ContentType.MediaType, "#1"); Assert.AreEqual (148, m.Headers.ContentLength, "#2"); Assert.AreEqual ("--X\r\nContent-Type: multipart/2; boundary=\"2a\"\r\n\r\n--2a\r\n\r\n--2a--\r\n\r\n--X\r\nContent-Type: multipart/3; boundary=\"3a\"\r\n9: 9n\r\n\r\n--3a\r\n\r\n--3a--\r\n\r\n--X--\r\n", m.ReadAsStringAsync ().Result, "#3"); Assert.AreEqual (other, m.First (), "#4"); }
public void Ctor () { using (var m = new MultipartContent ("a", "b")) { m.Headers.Add ("extra", "value"); Assert.AreEqual ("multipart/a", m.Headers.ContentType.MediaType, "#1"); Assert.AreEqual (14, m.Headers.ContentLength, "#2"); Assert.AreEqual ("--b\r\n\r\n--b--\r\n", m.ReadAsStringAsync ().Result, "#3"); } using (var m = new MultipartContent ()) { Assert.AreEqual ("multipart/mixed", m.Headers.ContentType.MediaType, "#11"); Assert.AreEqual (84, m.Headers.ContentLength, "#12"); } using (var m = new MultipartContent ("ggg")) { Assert.AreEqual ("multipart/ggg", m.Headers.ContentType.MediaType, "#21"); Assert.AreEqual (84, m.Headers.ContentLength, "#22"); } }
public void PostConvertFile_ConvertToKml_ShouldReturnByteArray() { var multipartContent = new MultipartContent(); var streamContent = new StreamContent(new MemoryStream(new byte[1] { 1 })); streamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") { Name = "\"files\"", FileName = "\"SomeFile.twl\"" }; streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/kml"); multipartContent.Add(streamContent); _controller.Request = new HttpRequestMessage(); _controller.Request.Content = multipartContent; _gpsBabelGateway.ConvertFileFromat(Arg.Any<byte[]>(), "naviguide", "kml").Returns(Task.FromResult(new byte[2] { 1, 1 })); var response = _controller.PostConvertFile("kml").Result as OkNegotiatedContentResult<byte[]>; Assert.AreEqual(2, response.Content.Length); }
static void Main() { var address = "http://localhost:9000/"; using (WebApp.Start<Startup>(address)) { var client = new HttpClient(); var batchContent = new MultipartContent("mixed") { new HttpMessageContent(new HttpRequestMessage(HttpMethod.Post, address + "/api/items") { Content = new ObjectContent(typeof (Item), new Item {Country = "Switzerland", Id = 1, Name = "Filip"}, new JsonMediaTypeFormatter()) }), new HttpMessageContent(new HttpRequestMessage(HttpMethod.Post, address + "/api/items") { Content = new ObjectContent(typeof (Item), new Item {Country = "Canada", Id = 2, Name = "Felix"}, new JsonMediaTypeFormatter()) }), new HttpMessageContent(new HttpRequestMessage(HttpMethod.Get, address + "/api/items")) }; var batchRequest = new HttpRequestMessage(HttpMethod.Post, address + "/api/batch") { Content = batchContent }; var batchResponse = client.SendAsync(batchRequest).Result; var streamProvider = batchResponse.Content.ReadAsMultipartAsync().Result; foreach (var content in streamProvider.Contents) { var response = content.ReadAsHttpResponseMessageAsync().Result; Print(response); } Console.ReadLine(); } }
public HttpContent Build(string boundary) { if (boundary == null) { throw new ArgumentNullException("boundary"); } if (string.IsNullOrWhiteSpace(boundary)) { throw new ArgumentException("The provided boundary value is invalid", "boundary"); } MultipartContent content = new MultipartContent("mixed", boundary); foreach (var request in Requests) { HttpMessageContent messageContent = new HttpMessageContent(request); messageContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/http"); messageContent.Headers.Add("Content-Transfer-Encoding", "binary"); content.Add(messageContent); } return content; }
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { if (request.Content == null || !request.Content.IsMimeMultipartContent("batch")) { return request.CreateErrorResponse(HttpStatusCode.UnsupportedMediaType, "The mediatype is expected to be \"multipart/batch\" for a batching request"); } MultipartContent outerContent = new MultipartContent("batch"); HttpResponseMessage outerResponse = request.CreateResponse(); outerResponse.Content = outerContent; MultipartMemoryStreamProvider multipart = await request.Content.ReadAsMultipartAsync(); /// Load requests from multipart content. Submit each of them /// to the in memory batch server sequentially. They are then /// processed asynchronously. /// The individual response are gathered and added to the /// multipart content for return response. foreach (var content in multipart.Contents) { HttpResponseMessage innerResponse = null; try { var innerRequest = await content.ReadAsHttpRequestMessageAsync(); innerResponse = await _entry.SendAsync(innerRequest, cancellationToken); } catch (Exception) { innerResponse = new HttpResponseMessage(HttpStatusCode.BadRequest); } outerContent.Add(new HttpMessageContent(innerResponse)); } return outerResponse; }
private HttpRequestMessage SetUpRequest(params string[] parts) { var content = new MultipartContent("mixed", "xyz-boundary"); foreach (var part in parts) { var nestedContent = new StringContent(part); nestedContent.Headers.ContentType = new MediaTypeHeaderValue("application/http"); nestedContent.Headers.Add("Content-Transfer-Encoding", "binary"); content.Add(nestedContent); } var request = new HttpRequestMessage(HttpMethod.Post, "http://example.com/api/odata/$batch") { Content = content }; request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml+atom")); return request; }
public virtual Task<HttpResponseMessage> CreateResponseMessageAsync(IList<HttpResponseMessage> responses, HttpRequestMessage request, CancellationToken cancellationToken) { if (responses == null) { throw Error.ArgumentNull("responses"); } if (request == null) { throw Error.ArgumentNull("request"); } MultipartContent batchContent = new MultipartContent(MultiPartContentSubtype); foreach (HttpResponseMessage batchResponse in responses) { batchContent.Add(new HttpMessageContent(batchResponse)); } HttpResponseMessage response = request.CreateResponse(); response.Content = batchContent; return Task.FromResult(response); }
public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext) { var boundry = Guid.NewGuid().ToString(); var multipartFormDataContent = new MultipartContent("mixed", boundry); //content. .StatusCode = (int)System.Net.HttpStatusCode.OK; var contentType = new MediaTypeHeaderValue("multipart/mixed"); // String.Format("multipart/mixed; boundary={0}", boundry)); var boundaryParameter = new NameValueHeaderValue("boundary", boundry); contentType.Parameters.Add(boundaryParameter); contentType.Parameters.Add(new System.Net.Http.Headers.NameValueHeaderValue("revision", "0.1")); content.Headers.ContentType = contentType; //content.Headers.Add("Accept", "multipart/mixed"); //content.Headers.Add("MIME-Version", "1.0"); var entities = (IEnumerable<object>)value; foreach (var entity in entities) { var entityContent = new ObjectContent<object>(entity, new JsonMediaTypeFormatter()); entityContent.Headers.LastModified = DateTime.UtcNow; // Add("Last-Modified", entity.LastModified.ToUniversalTime().ToString("R")); entityContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-" + entity.GetType().Name); multipartFormDataContent.Add(entityContent); } return multipartFormDataContent.CopyToAsync(writeStream); }
public async Task TestFileUpload() { var http = new HttpClient(); byte[] data = File.ReadAllBytes(HttpContext.Current.Server.MapPath("~/images/sailbig.jpg")); var fileContent = new ByteArrayContent(data); fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "sailbig.jpg", Name="file", }; var mp = new MultipartContent(); mp.Add(fileContent); var response = await http.PostAsync(BaseUrl + "samples/PostFileValues", mp); Assert.IsTrue(response.IsSuccessStatusCode); var result = await response.Content.ReadAsStringAsync(); Console.WriteLine(result); }
public async Task<Result> PostData(Uri uri, MultipartContent header, StringContent content, UserAccountEntity userAccountEntity) { var httpClient = new HttpClient(); try { var authenticationManager = new AuthenticationManager(); if (userAccountEntity.GetAccessToken().Equals("refresh")) { await authenticationManager.RefreshAccessToken(userAccountEntity); } var user = userAccountEntity.GetUserEntity(); if (user != null) { var language = userAccountEntity.GetUserEntity().Language; httpClient.DefaultRequestHeaders.Add("Accept-Language", language); } httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", userAccountEntity.GetAccessToken()); HttpResponseMessage response; if (header == null) { response = await httpClient.PostAsync(uri, content); } else { response = await httpClient.PostAsync(uri, header); } var responseContent = await response.Content.ReadAsStringAsync(); return new Result(true, responseContent); } catch { // TODO: Add detail error result to json object. return new Result(false, string.Empty); } }
public void Dispose_Empty_Sucess() { var content = new MultipartContent(); content.Dispose(); }
public async Task ReadAsStreamAsync_CustomEncodingSelector_CustomEncodingIsUsed(bool async) { var mc = new MultipartContent("subtype", "fooBoundary"); var stringContent = new StringContent("bar1"); stringContent.Headers.Add("latin1", "\U0001F600"); mc.Add(stringContent); var byteArrayContent = new ByteArrayContent("bar2" u8.ToArray()); byteArrayContent.Headers.Add("utf8", "\U0001F600"); mc.Add(byteArrayContent); byteArrayContent = new ByteArrayContent("bar3" u8.ToArray()); byteArrayContent.Headers.Add("ascii", "\U0001F600"); mc.Add(byteArrayContent); byteArrayContent = new ByteArrayContent("bar4" u8.ToArray()); byteArrayContent.Headers.Add("default", "\U0001F600"); mc.Add(byteArrayContent); mc.HeaderEncodingSelector = (name, _) => name switch { "latin1" => Encoding.Latin1, "utf8" => Encoding.UTF8, "ascii" => Encoding.ASCII, _ => null }; var ms = new MemoryStream(); if (async) { await(await mc.ReadAsStreamAsync()).CopyToAsync(ms); } else { mc.ReadAsStream().CopyTo(ms); } byte[] expected = Concat( "--fooBoundary\r\n" u8.ToArray(), "Content-Type: text/plain; charset=utf-8\r\n" u8.ToArray(), "latin1: " u8.ToArray(), Encoding.Latin1.GetBytes("\U0001F600"), "\r\n\r\n" u8.ToArray(), "bar1" u8.ToArray(), "\r\n--fooBoundary\r\n" u8.ToArray(), "utf8: " u8.ToArray(), "\U0001F600" u8.ToArray(), "\r\n\r\n" u8.ToArray(), "bar2" u8.ToArray(), "\r\n--fooBoundary\r\n" u8.ToArray(), "ascii: " u8.ToArray(), Encoding.ASCII.GetBytes("\U0001F600"), "\r\n\r\n" u8.ToArray(), "bar3" u8.ToArray(), "\r\n--fooBoundary\r\n" u8.ToArray(), "default: " u8.ToArray(), Encoding.Latin1.GetBytes("\U0001F600"), "\r\n\r\n" u8.ToArray(), "bar4" u8.ToArray(), "\r\n--fooBoundary--\r\n" u8.ToArray()); Assert.Equal(expected, ms.ToArray());
protected async Task <string> CreateFolder(string szFolderName) { HttpResponseMessage response = null; string szResult = string.Empty; using (HttpClient httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + AuthState.AccessToken); // Create the metadata. Name is most important, but we can also specify mimeType for CSV to import into GoogleDocs Dictionary <string, string> dictMeta = new Dictionary <string, string>() { { "name", szFolderName }, { "mimeType", "application/vnd.google-apps.folder" } }; // Create the form. The form itself needs the authtoken header using (MultipartContent form = new MultipartContent("related")) { // Next add the metadata - it is in Json format using (StringContent metadata = new StringContent(JsonConvert.SerializeObject(dictMeta))) { metadata.Headers.ContentType = new MediaTypeHeaderValue("application/json"); form.Add(metadata); try { response = await httpClient.PostAsync(new Uri(szURLUploadEndpoint), form).ConfigureAwait(false); szResult = response.Content.ReadAsStringAsync().Result; response.EnsureSuccessStatusCode(); if (!String.IsNullOrEmpty(szResult)) { GoogleDriveFileMetadata gfm = JsonConvert.DeserializeObject <GoogleDriveFileMetadata>(szResult); if (gfm != null) { return(gfm.id); } } } catch (System.Net.Http.HttpRequestException ex) { if (response == null) { throw new MyFlightbookException("Unknown error in GoogleDrive.CreateFolder", ex); } else { throw new MyFlightbookException(response.ReasonPhrase + " " + szResult); } } finally { if (response != null) { response.Dispose(); } } } } return(string.Empty); } }
public static async Task <HttpResponseMessage> SecuredMultiPartPostAsync(this HttpClient httpClient, string ApiPath, MultipartContent content) { ApiPath = $" {ApiPath}".Replace(" /", "").Trim(); return(await httpClient.PostAsync(ApiPath, content)); }
public async System.Threading.Tasks.Task TestFileListing() { string initialName = randomNewUserName("First InitialName", 6); string changedName = randomNewUserName("First ChangedName", 6); string service = "Application"; // Login as default user var loginUser = randomNewUserName("NewLoginUser", 6); var strId = await LoginAndRegisterAsNewUser(loginUser); User user = await GetCurrentUser(); Account currentAccount = await GetAccountForCurrentUser(); // C - Create var request = new HttpRequestMessage(HttpMethod.Post, "/api/" + service + "/Waiver"); Application viewmodel_application = SecurityHelper.CreateNewApplication(currentAccount); var jsonString = JsonConvert.SerializeObject(viewmodel_application); request.Content = new StringContent(jsonString, Encoding.UTF8, "application/json"); var response = await _client.SendAsync(request); jsonString = await response.Content.ReadAsStringAsync(); response.EnsureSuccessStatusCode(); // parse as JSON. Application responseViewModel = JsonConvert.DeserializeObject <Application>(jsonString); //Assert.Equal("Applying Person", responseViewModel.applyingPerson); Assert.Equal("Testing", responseViewModel.mainbusinessfocus); Assert.Equal("Automated Testing", responseViewModel.manufacturingprocessdescription); Guid id = new Guid(responseViewModel.id); // Attach a file string testData = "This is just a test."; byte[] bytes = Encoding.ASCII.GetBytes(testData); string documentType = "Test Document Type"; // Create random filename var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; var stringChars = new char[9]; var random = new Random(); for (int i = 0; i < stringChars.Length; i++) { stringChars[i] = chars[random.Next(chars.Length)]; } var randomString = new String(stringChars); string filename = randomString + ".txt"; MultipartFormDataContent multiPartContent = new MultipartFormDataContent("----TestBoundary"); var fileContent = new MultipartContent { new ByteArrayContent(bytes) }; fileContent.Headers.ContentType = new MediaTypeHeaderValue("text/plain"); fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data"); fileContent.Headers.ContentDisposition.Name = "File"; fileContent.Headers.ContentDisposition.FileName = filename; multiPartContent.Add(fileContent); multiPartContent.Add(new StringContent(documentType), "documentType"); // form input string accountId = user.accountid; // create a new request object for the upload, as we will be using multipart form submission. var requestMessage = new HttpRequestMessage(HttpMethod.Post, $"/api/file/{ id }/attachments/incident"); requestMessage.Content = multiPartContent; var uploadResponse = await _client.SendAsync(requestMessage); uploadResponse.EnsureSuccessStatusCode(); // Cleanup request = new HttpRequestMessage(HttpMethod.Post, "/api/" + service + "/" + id + "/delete"); response = await _client.SendAsync(request); response.EnsureSuccessStatusCode(); // should get a 404 if we try a get now. request = new HttpRequestMessage(HttpMethod.Get, "/api/" + service + "/" + id); response = await _client.SendAsync(request); Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); await LogoutAndCleanupTestUser(strId); }
public HttpRequestMessage PrepareBatchRequest(RequestOptions options) { if (!options.Url.StartsWith("http")) { options.Url = DataUtil.GetUrl(options.BaseUrl, options.Url); } HttpRequestMessage httpRequestMessage = new HttpRequestMessage() { RequestUri = new Uri(options.Url), Method = options.RequestMethod }; MultipartContent multipartContent1 = new MultipartContent("mixed", options.ContentType); CRUDModel <object> batchRecords = options.Data as CRUDModel <object>; int num1 = 0; JsonSerializerSettings settings = new JsonSerializerSettings(); settings.Converters.Add((JsonConverter) new StringEnumConverter()); settings.NullValueHandling = NullValueHandling.Ignore; CRUDModel <object> crudModel1 = batchRecords; int?count; int num2; if (crudModel1 == null) { num2 = 0; } else { count = crudModel1.Added?.Count; int num3 = 0; num2 = count.GetValueOrDefault() > num3 & count.HasValue ? 1 : 0; } if (num2 != 0) { foreach (object obj in batchRecords.Added) { MultipartContent multipartContent2 = new MultipartContent("mixed", options.CSet); HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Post, options.BaseUrl); httpRequest.Content = (HttpContent) new StringContent(JsonConvert.SerializeObject(obj, Formatting.None, settings), Encoding.UTF8, "application/json"); httpRequest.Headers.Add("Accept", options.Accept); httpRequest.Headers.Add("Content-Id", num1.ToString()); ++num1; HttpMessageContent httpMessageContent = new HttpMessageContent(httpRequest); httpMessageContent.Headers.Remove("Content-Type"); httpMessageContent.Headers.Add("Content-Type", "application/http"); httpMessageContent.Headers.Add("Content-Transfer-Encoding", "binary"); multipartContent2.Add((HttpContent)httpMessageContent); multipartContent1.Add((HttpContent)multipartContent2); } } CRUDModel <object> crudModel2 = batchRecords; int num4; if (crudModel2 == null) { num4 = 0; } else { count = crudModel2.Changed?.Count; int num3 = 0; num4 = count.GetValueOrDefault() > num3 & count.HasValue ? 1 : 0; } int i; if (num4 != 0) { for (i = 0; i < batchRecords.Changed.Count; i++) { MultipartContent multipartContent2 = new MultipartContent("mixed", options.CSet); object val = DataUtil.GetVal((IEnumerable)batchRecords.Changed, i, options.keyField); string odataUrlKey = DataUtil.GetODataUrlKey((object)null, options.keyField, val); HttpRequestMessage httpRequest = new HttpRequestMessage(options.UpdateType, options.BaseUrl + odataUrlKey); List <object> objectList = options.Original is IEnumerable original?original.Cast <object>().ToList <object>().Where <object>((Func <object, bool>)(e => DataUtil.GetVal((IEnumerable)batchRecords.Changed, i, options.keyField)?.ToString() == e.GetType().GetProperty(options.keyField).GetValue(e)?.ToString())).ToList <object>() : (List <object>)null; httpRequest.Content = (HttpContent) new StringContent(JsonConvert.SerializeObject(DataUtil.CompareAndRemove(batchRecords.Changed[i], objectList?[0], options.keyField), Formatting.None, settings), Encoding.UTF8, "application/json"); httpRequest.Headers.Add("Accept", options.Accept); httpRequest.Headers.Add("Content-Id", num1.ToString()); ++num1; HttpMessageContent httpMessageContent = new HttpMessageContent(httpRequest); httpMessageContent.Headers.Remove("Content-Type"); httpMessageContent.Headers.Add("Content-Type", "application/http"); httpMessageContent.Headers.Add("Content-Transfer-Encoding", "binary"); multipartContent2.Add((HttpContent)httpMessageContent); multipartContent1.Add((HttpContent)multipartContent2); } } CRUDModel <object> crudModel3 = batchRecords; int num5; if (crudModel3 == null) { num5 = 0; } else { count = crudModel3.Deleted?.Count; int num3 = 0; num5 = count.GetValueOrDefault() > num3 & count.HasValue ? 1 : 0; } if (num5 != 0) { foreach (object rowData in batchRecords.Deleted) { MultipartContent multipartContent2 = new MultipartContent("mixed", options.CSet); string odataUrlKey = DataUtil.GetODataUrlKey(rowData, options.keyField); HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Delete, options.BaseUrl + odataUrlKey); httpRequest.Content = (HttpContent) new StringContent(JsonConvert.SerializeObject(rowData, Formatting.None, settings), Encoding.UTF8, "application/json"); httpRequest.Headers.Add("Accept", "application/json;odata=light;q=1,application/json;odata=verbose;q=0.5"); httpRequest.Headers.Add("Content-Id", num1.ToString()); ++num1; HttpMessageContent httpMessageContent = new HttpMessageContent(httpRequest); httpMessageContent.Headers.Remove("Content-Type"); httpMessageContent.Headers.Add("Content-Type", "application/http"); httpMessageContent.Headers.Add("Content-Transfer-Encoding", "binary"); multipartContent2.Add((HttpContent)httpMessageContent); multipartContent1.Add((HttpContent)multipartContent2); } } httpRequestMessage.Content = (HttpContent)multipartContent1; return(httpRequestMessage); }
public async Task <HttpResponseMessage> SendBatchRequest() { JObject record = new JObject(); string accessToken = await GetAccessToken(); var appSettings = ConfigurationManager.AppSettings; string apiUrl = appSettings["apiUrl"]; HttpClient client = new HttpClient(); //Init Batch string batchName = $"batch_{Guid.NewGuid()}"; MultipartContent batchContent = new MultipartContent("mixed", batchName); string changesetName = $"changeset_{Guid.NewGuid()}"; MultipartContent changesetContent = new MultipartContent("mixed", changesetName); //Create first request - Create new Contact record.Add("firstname", "Jane"); record.Add("lastname", "Doe"); HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, apiUrl + "contacts"); HttpMessageContent messageContent = new HttpMessageContent(requestMessage); messageContent.Headers.Remove("Content-Type"); messageContent.Headers.Add("Content-Type", "application/http"); messageContent.Headers.Add("Content-Transfer-Encoding", "binary"); StringContent stringContent = new StringContent(record.ToString()); stringContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json;type=entry"); requestMessage.Content = stringContent; messageContent.Headers.Add("Content-ID", "1"); changesetContent.Add(messageContent); //Create second request - Create new Contact record = new JObject(); record.Add("firstname", "John"); record.Add("lastname", "Doe"); requestMessage = new HttpRequestMessage(HttpMethod.Post, apiUrl + "contacts"); messageContent = new HttpMessageContent(requestMessage); messageContent.Headers.Remove("Content-Type"); messageContent.Headers.Add("Content-Type", "application/http"); messageContent.Headers.Add("Content-Transfer-Encoding", "binary"); stringContent = new StringContent(record.ToString()); stringContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json;type=entry"); requestMessage.Content = stringContent; messageContent.Headers.Add("Content-ID", "2"); changesetContent.Add(messageContent); batchContent.Add(changesetContent); //Create third request - Retrieve contacts requestMessage = new HttpRequestMessage(HttpMethod.Get, apiUrl + "contacts?$select=firstname, lastname&$filter=firstname eq 'Jane' or firstname eq 'John'"); messageContent = new HttpMessageContent(requestMessage); messageContent.Headers.Remove("Content-Type"); messageContent.Headers.Add("Content-Type", "application/http"); messageContent.Headers.Add("Content-Transfer-Encoding", "binary"); requestMessage.Headers.Add("Accept", "application/json"); batchContent.Add(messageContent); //Create batch request HttpRequestMessage batchRequest = new HttpRequestMessage(HttpMethod.Post, apiUrl + "$batch"); batchRequest.Content = batchContent; batchRequest.Headers.Add("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\""); batchRequest.Headers.Add("OData-MaxVersion", "4.0"); batchRequest.Headers.Add("OData-Version", "4.0"); batchRequest.Headers.Add("Accept", "application/json"); batchRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); //Execute Batch request HttpResponseMessage response = await client.SendAsync(batchRequest); MultipartMemoryStreamProvider body = await response.Content.ReadAsMultipartAsync(); //Output result Console.WriteLine($"Batch Request Result:\n********************************************************\n {await response.Content.ReadAsStringAsync()}"); return(response); }
public void ReadAsAsync_WhenContentIsMultipartContentAndFormatterCanReadFromTheContent() { MultipartContent mimeContent = new MultipartContent(); mimeContent.Add(new StringContent("multipartContent")); _formatterMock.Setup(f => f.CanWriteType(It.IsAny<Type>())).Returns(true); _formatterMock.Setup(f => f.CanReadType(It.IsAny<Type>())).Returns(true); _formatterMock.Setup(f => f.ReadFromStreamAsync(It.IsAny<Type>(), It.IsAny<Stream>(), It.IsAny<HttpContent>(), It.IsAny<IFormatterLogger>())) .Returns<Type, Stream, HttpContent, IFormatterLogger>((type, stream, content, logger) => { MultipartMemoryStreamProvider provider = content.ReadAsMultipartAsync().Result; Assert.Equal(1, provider.Contents.Count); return TaskHelpers.FromResult<object>(provider.Contents[0].ReadAsStringAsync().Result); }); MediaTypeFormatter formatter = _formatterMock.Object; formatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("multipart/mixed")); Assert.Equal("multipartContent", mimeContent.ReadAsAsync<string>(new[] { formatter }).Result); }
internal void SendAsyncMultipartRequest(HttpMethod method, string relativePath, MultipartContent multiPartEntity, RemoteRequestCompletionBlock completionHandler) { Uri url = null; try { url = _baseUrl.Append(relativePath); } catch (UriFormatException) { Log.To.Sync.E(Tag, "Invalid path received for request: {0}, throwing...", new SecureLogString(relativePath, LogMessageSensitivity.PotentiallyInsecure)); throw new ArgumentException("Invalid path", "relativePath"); } var message = new HttpRequestMessage(method, url); message.Content = multiPartEntity; message.Headers.Add("Accept", "*/*"); AddRequestHeaders(message); var client = default(CouchbaseLiteHttpClient); if (!_client.AcquireFor(TimeSpan.FromSeconds(1), out client)) { Log.To.Sync.I(Tag, "Client is disposed, aborting request to {0}", new SecureLogString(relativePath, LogMessageSensitivity.PotentiallyInsecure)); return; } var _lastError = default(Exception); client.Authenticator = Authenticator; var t = client.SendAsync(message, _cancellationTokenSource.Token).ContinueWith(response => { multiPartEntity.Dispose(); if (response.Status != TaskStatus.RanToCompletion) { _lastError = response.Exception; Log.To.Sync.W(Tag, "SendAsyncRequest did not run to completion, returning null..."); return(Task.FromResult((Stream)null)); } if ((int)response.Result.StatusCode > 300) { _lastError = new HttpResponseException(response.Result.StatusCode); Log.To.Sync.W(Tag, "Server returned HTTP Error, returning null..."); return(Task.FromResult((Stream)null)); } return(response.Result.Content.ReadAsStreamAsync()); }, _cancellationTokenSource.Token).ContinueWith(response => { try { var hasEmptyResult = response.Result == null || response.Result.Result == null || response.Result.Result.Length == 0; if (response.Status != TaskStatus.RanToCompletion) { Log.To.Sync.W(Tag, "SendAsyncRequest phase two did not run to completion, continuing..."); } else if (hasEmptyResult) { Log.To.Sync.W(Tag, "Server returned an empty response, continuing..."); } if (completionHandler != null) { object fullBody = null; if (!hasEmptyResult) { var mapper = Manager.GetObjectMapper(); fullBody = mapper.ReadValue <Object>(response.Result.Result); } completionHandler(fullBody, response.Exception ?? _lastError); } } finally { Task dummy; _requests.TryRemove(message, out dummy); } }, _cancellationTokenSource.Token); _requests.TryAdd(message, t); }
/// <summary> /// create multiple records at once using a batch /// </summary> /// <param name="entityCollection"></param> /// <param name="datalist"></param> /// <returns></returns> public async Task <CRMBatchResult> Create(string entityCollection, object[] datalist) { await CheckAuthToken(); #if WINDOWS_APP throw new NotImplementedException(); #elif NETCOREAPP1_0 throw new NotImplementedException(); #elif NETSTANDARD1_4 throw new NotImplementedException(); #else var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _AccessToken); httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0"); httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0"); var batchid = "batch_" + Guid.NewGuid().ToString(); MultipartContent batchContent = new MultipartContent("mixed", batchid); var changesetID = "changeset_" + Guid.NewGuid().ToString(); MultipartContent changeSetContent = new MultipartContent("mixed", changesetID); int contentID = 1; foreach (var data in datalist) { HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, _apiUrl + entityCollection); req.Content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json"); HttpMessageContent content = new HttpMessageContent(req); content.Headers.Remove("Content-Type"); content.Headers.TryAddWithoutValidation("Content-Type", "application/http"); content.Headers.TryAddWithoutValidation("Content-Transfer-Encoding", "binary"); content.Headers.TryAddWithoutValidation("Content-ID", contentID.ToString()); contentID++; changeSetContent.Add(content); } batchContent.Add(changeSetContent); HttpRequestMessage batchRequest = new HttpRequestMessage(HttpMethod.Post, _apiUrl + "$batch"); batchRequest.Content = batchContent; var batchstring = await batchRequest.Content.ReadAsStringAsync(); var response = await httpClient.SendAsync(batchRequest); var responseString = response.Content.ReadAsStringAsync(); MultipartMemoryStreamProvider batchStream = await response.Content.ReadAsMultipartAsync();; var changesetStream = batchStream.Contents.FirstOrDefault(); StreamContent changesetFixedContent = FixupChangeStreamDueToBug(changesetStream); var changesetFixedStream = await changesetFixedContent.ReadAsMultipartAsync(); CRMBatchResult finalResult = new CRMBatchResult(); finalResult.ResultItems = new List <CRMBatchResultItem>(); foreach (var responseContent in changesetFixedStream.Contents) { var fixedREsponseContent = FixupToAddCorrectHttpContentType(responseContent); var individualResponseString = await fixedREsponseContent.ReadAsStringAsync(); var indivdualResponse = await fixedREsponseContent.ReadAsHttpResponseMessageAsync(); var idString = indivdualResponse.Headers.GetValues("OData-EntityId").FirstOrDefault(); idString = idString.Replace(_apiUrl + entityCollection, "").Replace("(", "").Replace(")", ""); CRMBatchResultItem resultItem = new CRMBatchResultItem(); resultItem.EntityID = Guid.Parse(idString); finalResult.ResultItems.Add(resultItem); } return(finalResult); #endif }
private void AddByteArrayContent(MultipartContent multipartContent) { multipartContent.Add(new ByteArrayContent(ByteData)); }
public async Task MultipartContent_TryComputeLength_ReturnsSameLengthAsCopyToAsync(MultipartContent multipartContent) { Assert.True(multipartContent.TryComputeLength(out long length)); var copyToStream = new MemoryStream(); multipartContent.CopyTo(copyToStream, context: null, cancellationToken: default); Assert.Equal(length, copyToStream.Length); var copyToAsyncStream = new MemoryStream(); await multipartContent.CopyToAsync(copyToAsyncStream, context : null, cancellationToken : default); Assert.Equal(length, copyToAsyncStream.Length); Assert.Equal(copyToStream.ToArray(), copyToAsyncStream.ToArray()); }
private bool UploadMultipartRevision(RevisionInternal revision) { MultipartContent multiPart = null; var revProps = revision.GetProperties(); var attachments = revProps.Get("_attachments").AsDictionary <string, object>(); foreach (var attachmentKey in attachments.Keys) { var attachment = attachments.Get(attachmentKey).AsDictionary <string, object>(); if (attachment.ContainsKey("follows")) { if (multiPart == null) { multiPart = new MultipartContent("related"); try { var json = Manager.GetObjectMapper().WriteValueAsString(revProps); var utf8charset = Encoding.UTF8; //multiPart.Add(new StringContent(json, utf8charset, "application/json"), "param1"); var jsonContent = new StringContent(json, utf8charset, "application/json"); //jsonContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); multiPart.Add(jsonContent); } catch (IOException e) { throw new ArgumentException("Not able to serialize revision properties into a multipart request content.", e); } } var blobStore = LocalDatabase.Attachments; var base64Digest = (string)attachment.Get("digest"); var blobKey = new BlobKey(base64Digest); var inputStream = blobStore.BlobStreamForKey(blobKey); if (inputStream == null) { Log.W(TAG, "Unable to find blob file for blobKey: " + blobKey + " - Skipping upload of multipart revision."); multiPart = null; } else { string contentType = null; if (attachment.ContainsKey("content_type")) { contentType = (string)attachment.Get("content_type"); } else { if (attachment.ContainsKey("content-type")) { var message = string.Format("Found attachment that uses content-type" + " field name instead of content_type (see couchbase-lite-android" + " issue #80): " + attachment); Log.W(TAG, message); } } var content = new StreamContent(inputStream); content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = attachmentKey }; content.Headers.ContentType = new MediaTypeHeaderValue(contentType ?? "application/octet-stream"); multiPart.Add(content); } } } if (multiPart == null) { return(false); } var path = string.Format("/{0}?new_edits=false", revision.GetDocId()); // TODO: need to throttle these requests Log.D(TAG, "Uploading multipart request. Revision: " + revision); SendAsyncMultipartRequest(HttpMethod.Put, path, multiPart, (result, e) => { if (e != null) { var httpError = e as HttpResponseException; if (httpError != null) { if (httpError.StatusCode == System.Net.HttpStatusCode.UnsupportedMediaType) { _dontSendMultipart = true; UploadJsonRevision(revision); } } else { Log.E(TAG, "Exception uploading multipart request", e); LastError = e; RevisionFailed(); } } else { Log.D(TAG, "Uploaded multipart request. Result: " + result); SafeIncrementCompletedChangesCount(); RemovePending(revision); } }); return(true); }
/// <summary> /// Adds the specified OnenotePage to the collection via POST. /// </summary> /// <param name="onenotePage">The OnenotePage to add in stream form.</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/> for the request.</param> /// <returns>The created OnenotePage.</returns> public System.Threading.Tasks.Task <OnenotePage> AddAsync(MultipartContent onenotePage, CancellationToken cancellationToken) { //this.ContentType = contentType; this.Method = "POST"; return(this.SendMultiPartAsync <OnenotePage>(onenotePage, cancellationToken)); }
public static IEnumerable <object[]> PostAsync_Cancel_CancellationTokenPassedToContent_MemberData() { // Note: For HTTP2, the actual token will be a linked token and will not be an exact match for the original token. // Verify that it behaves as expected by cancelling it and validating that cancellation propagates. // StreamContent { CancellationTokenSource tokenSource = new CancellationTokenSource(); var actualToken = new StrongBox <CancellationToken>(); bool called = false; var content = new StreamContent(new DelegateStream( canReadFunc: () => true, readAsyncFunc: async(buffer, offset, count, cancellationToken) => { int result = 1; if (called) { result = 0; Assert.False(cancellationToken.IsCancellationRequested); tokenSource.Cancel(); // Wait for cancellation to occur. It should be very quickly after it's been requested. var tcs = new TaskCompletionSource <bool>(); using (cancellationToken.Register(() => tcs.SetResult(true))) { await tcs.Task; } } called = true; return(result); } )); yield return(new object[] { content, tokenSource }); } // MultipartContent { CancellationTokenSource tokenSource = new CancellationTokenSource(); var actualToken = new StrongBox <CancellationToken>(); bool called = false; var content = new MultipartContent(); content.Add(new StreamContent(new DelegateStream( canReadFunc: () => true, canSeekFunc: () => true, lengthFunc: () => 1, positionGetFunc: () => 0, positionSetFunc: _ => {}, readAsyncFunc: async(buffer, offset, count, cancellationToken) => { int result = 1; if (called) { result = 0; Assert.False(cancellationToken.IsCancellationRequested); tokenSource.Cancel(); // Wait for cancellation to occur. It should be very quickly after it's been requested. var tcs = new TaskCompletionSource <bool>(); using (cancellationToken.Register(() => tcs.SetResult(true))) { await tcs.Task; } } called = true; return(result); } ))); yield return(new object[] { content, tokenSource }); } // MultipartFormDataContent { CancellationTokenSource tokenSource = new CancellationTokenSource(); var actualToken = new StrongBox <CancellationToken>(); bool called = false; var content = new MultipartFormDataContent(); content.Add(new StreamContent(new DelegateStream( canReadFunc: () => true, canSeekFunc: () => true, lengthFunc: () => 1, positionGetFunc: () => 0, positionSetFunc: _ => {}, readAsyncFunc: async(buffer, offset, count, cancellationToken) => { int result = 1; if (called) { result = 0; Assert.False(cancellationToken.IsCancellationRequested); tokenSource.Cancel(); // Wait for cancellation to occur. It should be very quickly after it's been requested. var tcs = new TaskCompletionSource <bool>(); using (cancellationToken.Register(() => tcs.SetResult(true))) { await tcs.Task; } } called = true; return(result); } ))); yield return(new object[] { content, tokenSource }); } }
public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string accessToken = await GetAccessToken(); string apiUrl = Environment.GetEnvironmentVariable("apiUrl"); string filename = req.Query["filename"]; int i = 0; int totalRecords = 0; JObject dailyReportObject = new JObject(); List <JObject> dailyReportBatch = new List <JObject>(); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); filename = filename ?? data?.filename; if (filename == null) { filename = DateTime.Now.ToString("MM-dd-yyyy"); } HttpClient client = new HttpClient(); try { // get file date DateTime filedate = DateTime.ParseExact(req.Query["filename"], "MM-dd-yyyy", System.Globalization.CultureInfo.InvariantCulture); filename = filedate.ToString("MM-dd-yyyy") + ".csv"; // get daily report from John Hopkins CSSE repository HttpResponseMessage response = await client.GetAsync("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/" + filename); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); responseBody = responseBody.Replace("Country/Region", "Country_Region").Replace("Province/State", "Province_State").Replace("Last Update", "Last_Update"); JObject countryRegionObject = new JObject(); JObject provinceStateObject = new JObject(); using (var csv = new CsvReader(new StringReader(responseBody), System.Globalization.CultureInfo.InvariantCulture)) { List <dailyReport> list = new List <dailyReport>(); foreach (dailyReport record in csv.GetRecords <dailyReport>()) { list.Add(record); } list.Sort(); IEnumerable <dailyReport> l = list; var grouped = l.GroupBy(x => new { x.Country_Region, x.Province_State }).Select(g => new { Name = g.Key, Deaths = g.Sum(x => (int)x.Deaths), Confirmed = g.Sum(x => (int)x.Confirmed), Recovered = g.Sum(x => (int)x.Recovered) });; totalRecords = grouped.Count(); foreach (var rec in grouped) { // if country / region does not exist then create it countryRegionObject = await countryRegionCreateIfNotExist(rec.Name.Country_Region, client, accessToken); if (rec.Name.Province_State != "") { // if province / state does not exist then create it provinceStateObject = await provinceStateCreateIfNotExist(countryRegionObject, rec.Name.Province_State, client, accessToken); } dailyReportObject = new JObject(); string name = countryRegionObject["pag_name"].ToString(); if (rec.Name.Province_State != "") { name = name + ", " + provinceStateObject["pag_name"].ToString(); } dailyReportObject.Add("pag_name", name); Console.WriteLine(i.ToString() + " " + dailyReportObject["pag_name"]); dailyReportObject.Add("*****@*****.**", "/pag_countryregions(" + countryRegionObject["pag_countryregionid"] + ")"); if (rec.Name.Province_State != "") { dailyReportObject.Add("*****@*****.**", "/pag_provincestates(" + provinceStateObject["pag_provincestateid"] + ")"); } dailyReportObject.Add("pag_deaths", rec.Deaths); dailyReportObject.Add("pag_confirmed", rec.Confirmed); dailyReportObject.Add("pag_recovered", rec.Recovered); dailyReportObject.Add("pag_filedate", filedate); dailyReportBatch.Add(dailyReportObject); } //Init Batch string batchName = $"batch_{Guid.NewGuid()}"; MultipartContent batchContent = new MultipartContent("mixed", batchName); string changesetName = $"changeset_{Guid.NewGuid()}"; MultipartContent changesetContent = new MultipartContent("mixed", changesetName); for (int j = 0; j < dailyReportBatch.Count; j++) { HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, apiUrl + "pag_dailyreports"); requestMessage.Version = new Version(1, 1); HttpMessageContent messageContent = new HttpMessageContent(requestMessage); messageContent.Headers.Remove("Content-Type"); messageContent.Headers.Add("Content-Type", "application/http"); messageContent.Headers.Add("Content-Transfer-Encoding", "binary"); StringContent stringContent = new StringContent(dailyReportBatch[j].ToString()); stringContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json;type=entry"); requestMessage.Content = stringContent; messageContent.Headers.Add("Content-ID", j.ToString()); changesetContent.Add(messageContent); } batchContent.Add(changesetContent); HttpRequestMessage batchRequest = new HttpRequestMessage(HttpMethod.Post, apiUrl + "$batch"); batchRequest.Version = new Version(1, 1); batchRequest.Content = batchContent; batchRequest.Headers.Add("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\""); batchRequest.Headers.Add("OData-MaxVersion", "4.0"); batchRequest.Headers.Add("OData-Version", "4.0"); batchRequest.Headers.Add("Accept", "application/json"); batchRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); //Execute Batch request HttpResponseMessage batchResponse = await client.SendAsync(batchRequest); //clear list of records dailyReportBatch.Clear(); } } catch (CsvHelper.CsvHelperException ex) { Console.WriteLine(ex.Data["CsvHelper"]); } catch (HttpRequestException e) { Console.WriteLine("\nException Caught!"); Console.WriteLine("Message :{0} ", e.Message); } string responseMessage = $"Function executed successfully. Filename: {filename} processed. {totalRecords} records written"; return(new OkObjectResult(responseMessage)); }
/// <summary> /// Put's a file as a stream using the REST API documented at https://developers.google.com/drive/v3/web/manage-uploads#multipart /// </summary> /// <param name="szFileName">The file name to use</param> /// <param name="ms">The stream of the data</param> /// <param name="szMimeType">The mime type for the data</param> /// <returns>True for success</returns> /// <exception cref="MyFlightbookException"></exception> /// <exception cref="System.Net.Http.HttpRequestException"></exception> public async Task <IReadOnlyDictionary <string, string> > PutFile(Stream ms, string szFileName, string szMimeType) { if (!CheckAccessToken()) { throw new MyFlightbookException("Google drive: access token missing or expired"); } ms.Seek(0, SeekOrigin.Begin); // write out the whole stream. UploadAsync appears to pick up from the current location, which is the end-of-file after writing to a ZIP. string szResult = string.Empty; HttpResponseMessage response = null; using (HttpClient httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + AuthState.AccessToken); string idExisting = null; if (String.IsNullOrEmpty(RootFolderID)) { RootFolderID = await IDForFolder(RootPath); if (String.IsNullOrEmpty(RootFolderID)) { RootFolderID = await CreateFolder(Branding.CurrentBrand.AppName); } } // update the existing file if it is present. // if (!String.IsNullOrEmpty(RootFolderID)) // idExisting = await IDForFile(szFileName, RootFolderID); // Create the metadata. Name is most important, but we can also specify mimeType for CSV to import into GoogleDocs Dictionary <string, object> dictMeta = new Dictionary <string, object>() { { "name", szFileName } }; if (szMimeType.CompareCurrentCultureIgnoreCase("text/csv") == 0) { dictMeta["mimeType"] = "application/vnd.google-apps.spreadsheet"; // get it to show up in google drive sheets. } if (!String.IsNullOrEmpty(idExisting)) { dictMeta["id"] = idExisting; } if (!String.IsNullOrEmpty(RootFolderID)) { dictMeta["parents"] = new List <string>() { RootFolderID } } ; // Create the form. The form itself needs the authtoken header using (MultipartContent form = new MultipartContent("related")) { // Next add the metadata - it is in Json format string szJSonMeta = JsonConvert.SerializeObject(dictMeta); StringContent metadata = new StringContent(szJSonMeta, System.Text.Encoding.UTF8); metadata.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json") { CharSet = "UTF-8" }; form.Add(metadata); // Finally, add the body, with its appropriate mime type. StreamContent body = new StreamContent(ms); body.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(szMimeType); form.Add(body); try { response = (String.IsNullOrEmpty(idExisting)) ? await httpClient.PostAsync(szURLUploadEndpoint, form) : await httpClient.PatchAsync(new Uri(String.Format(szURLUpdateEndpointTemplate, idExisting)), form); szResult = response.Content.ReadAsStringAsync().Result; response.EnsureSuccessStatusCode(); return((String.IsNullOrEmpty(szResult)) ? null : JsonConvert.DeserializeObject <Dictionary <string, string> >(szResult)); } catch (System.Net.Http.HttpRequestException ex) { if (response == null) { throw new MyFlightbookException("Unknown error in GoogleDrive.PutFile", ex); } else { throw new MyFlightbookException(response.ReasonPhrase + " " + (String.IsNullOrEmpty(szResult) ? "" : szResult)); } } } } } }
public async System.Threading.Tasks.Task TestUploadFile() { // Create application string initialName = randomNewUserName("Application Initial Name ", 6); string changedName = randomNewUserName("Application Changed Name ", 6); string service = "Application"; // login as default and get account for current user string loginUser = randomNewUserName("TestAppUser_", 6); var strId = await LoginAndRegisterAsNewUser(loginUser); User user = await GetCurrentUser(); Account currentAccount = await GetAccountForCurrentUser(); var request = new HttpRequestMessage(HttpMethod.Post, "/api/" + service + "/Waiver"); Application viewmodel_application = new Application() { applicant = currentAccount, //account mainbusinessfocus = "Testing", manufacturingprocessdescription = "Automated Testing" }; var jsonString = JsonConvert.SerializeObject(viewmodel_application); request.Content = new StringContent(jsonString, Encoding.UTF8, "application/json"); var response = await _client.SendAsync(request); response.EnsureSuccessStatusCode(); jsonString = await response.Content.ReadAsStringAsync(); Application responseViewModel = JsonConvert.DeserializeObject <Application>(jsonString); Assert.Equal("Testing", responseViewModel.mainbusinessfocus); Assert.Equal("Automated Testing", responseViewModel.manufacturingprocessdescription); Guid id = new Guid(responseViewModel.id); request = new HttpRequestMessage(HttpMethod.Get, "/api/" + service + "/" + id); response = await _client.SendAsync(request); response.EnsureSuccessStatusCode(); jsonString = await response.Content.ReadAsStringAsync(); responseViewModel = JsonConvert.DeserializeObject <Application>(jsonString); Assert.Equal("Testing", responseViewModel.mainbusinessfocus); Assert.Equal("Automated Testing", responseViewModel.manufacturingprocessdescription); Assert.True(responseViewModel.applicant != null); Assert.Equal(currentAccount.id, responseViewModel.applicant.id); // Test upload, get, delete attachment string documentType = "Licence Application Main"; using (var formData = new MultipartFormDataContent()) { /* * // Upload * var fileContent = new ByteArrayContent(new byte[100]); * fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") * { * Name = "file", * FileName = "test.pdf" * }; * formData.Add(fileContent); * formData.Add(new StringContent(documentType, Encoding.UTF8, "application/json"), "documentType"); * response = _client.PostAsync($"/api/file/{id}/attachments/incident", formData).Result; * Assert.Equal(HttpStatusCode.OK, response.StatusCode); * */ // Attach a file string testData = "This is just a test."; byte[] bytes = Encoding.ASCII.GetBytes(testData); documentType = "Test Document Type"; // Create random filename var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; var stringChars = new char[9]; var random = new Random(); for (int i = 0; i < stringChars.Length; i++) { stringChars[i] = chars[random.Next(chars.Length)]; } var randomString = new String(stringChars); string filename = randomString + ".txt"; MultipartFormDataContent multiPartContent = new MultipartFormDataContent("----TestBoundary"); var fileContent = new MultipartContent { new ByteArrayContent(bytes) }; fileContent.Headers.ContentType = new MediaTypeHeaderValue("text/plain"); fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data"); fileContent.Headers.ContentDisposition.Name = "File"; fileContent.Headers.ContentDisposition.FileName = filename; multiPartContent.Add(fileContent); multiPartContent.Add(new StringContent(documentType), "documentType"); // form input string accountId = user.accountid; // create a new request object for the upload, as we will be using multipart form submission. var requestMessage = new HttpRequestMessage(HttpMethod.Post, $"/api/file/{ id }/attachments/incident"); requestMessage.Content = multiPartContent; var uploadResponse = await _client.SendAsync(requestMessage); uploadResponse.EnsureSuccessStatusCode(); // Get request = new HttpRequestMessage(HttpMethod.Get, $"/api/file/{id}/attachments/incident/{documentType}"); response = await _client.SendAsync(request); response.EnsureSuccessStatusCode(); jsonString = await response.Content.ReadAsStringAsync(); var files = JsonConvert.DeserializeObject <List <FileSystemItem> >(jsonString); files.ForEach(async file => { // Delete request = new HttpRequestMessage(HttpMethod.Delete, $"/api/file/{id}/attachments/incident?serverRelativeUrl={Uri.EscapeDataString(file.serverrelativeurl)}&documentType={documentType}"); response = await _client.SendAsync(request); response.EnsureSuccessStatusCode(); }); request = new HttpRequestMessage(HttpMethod.Get, $"/api/file/{id}/attachments/incident/{documentType}"); response = await _client.SendAsync(request); response.EnsureSuccessStatusCode(); jsonString = await response.Content.ReadAsStringAsync(); files = JsonConvert.DeserializeObject <List <FileSystemItem> >(jsonString); Assert.Empty(files); } await LogoutAndCleanupTestUser(strId); }
/// <summary> /// Adds the specified OnenotePage to the collection via POST. /// </summary> /// <param name="onenotePage">The OnenotePage to add in stream form.</param> /// <returns>The created OnenotePage.</returns> public System.Threading.Tasks.Task <OnenotePage> AddAsync(MultipartContent onenotePage) { return(this.AddAsync(onenotePage, CancellationToken.None)); }
public async Task putHL7(RoutedItem routedItem, int taskID, LifeImageCloudConnection connection, IHttpManager httpManager) { var Connection = connection; var httpClient = _liteHttpClient.GetClient(connection); var taskInfo = $"task: {taskID} connection: {Connection.name}"; MultipartContent content = null; StreamContent streamContent = null; HttpResponseMessage response = null; try { if (!File.Exists(routedItem.sourceFileName)) { routedItem.Error = "File Not Found"; _routedItemManager.Init(routedItem); _routedItemManager.Dequeue(Connection, Connection.toCloud, nameof(Connection.toCloud), error: true); return; } var stopWatch = new Stopwatch(); stopWatch.Start(); //set theConnection.URL http://localhost:8080/universal-inbox/api/agent/v1/hl7-upload //string putHL7URL = Connection.URL + "/api/agent/v1/hl7-upload?connectionName=" + routedItem.fromConnection; string putHL7URL = Connection.URL + CloudAgentConstants.GetPutHl7Url(routedItem.fromConnection); _logger.Log(LogLevel.Debug, $"{taskInfo} putHL7URL: {putHL7URL}"); //generate guid for boundary...boundaries cannot be accidentally found in the content var boundary = Guid.NewGuid(); _logger.Log(LogLevel.Debug, $"{taskInfo} boundary: {boundary}"); // create the content content = new MultipartContent("related", boundary.ToString()); //add the sharing headers List <string> shareHeader = new List <string>(); if (Connection.shareDestinations != null) { foreach (var connectionSet in routedItem.toConnections.FindAll(e => e.connectionName.Equals(Connection.name))) { if (connectionSet.shareDestinations != null) { foreach (var shareDestination in connectionSet.shareDestinations) { shareHeader.Add(shareDestination.boxUuid); } } } } content.Headers.Add("X-Li-Destination", shareHeader); // //var fileSize = routedItem.stream.Length; var fileSize = new FileInfo(routedItem.sourceFileName).Length; //var streamContent = new StreamContent(routedItem.stream); streamContent = new StreamContent(File.OpenRead(routedItem.sourceFileName)); streamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { // FileName = filename FileName = routedItem.sourceFileName }; streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); //streamContent.Headers.Add("Content-Transfer-Encoding", "gzip"); content.Add(streamContent); // issue the POST Task <HttpResponseMessage> task; var cookies = _liteHttpClient.GetCookies(putHL7URL); _logger.LogCookies(cookies, taskInfo); if (routedItem.Compress == true) { task = httpClient.PostAsync(putHL7URL, new CompressedContent(content, "gzip"), _taskManager.cts.Token); } else { task = httpClient.PostAsync(putHL7URL, content, _taskManager.cts.Token); } response = await task; // output the result _logger.LogHttpResponseAndHeaders(response, taskInfo); if (response.StatusCode == HttpStatusCode.Unauthorized) { httpManager.loginNeeded = true; } _logger.Log(LogLevel.Debug, $"{taskInfo} response.Content.ReadAsStringAsync(): {await response.Content.ReadAsStringAsync()}"); // convert from stream to JSON //var serializer = new DataContractJsonSerializer(typeof(LoginJSON)); //var loginJSON = serializer.ReadObject(await response.Content.ReadAsStreamAsync()) as LoginJSON; stopWatch.Stop(); _logger.Log(LogLevel.Information, $"{taskInfo} elapsed: {stopWatch.Elapsed} size: {fileSize} rate: {(float)fileSize / stopWatch.Elapsed.TotalMilliseconds * 1000 / 1000000} MB/s"); //dequeue the work, we're done! if (streamContent != null) { streamContent.Dispose(); } if (response != null) { response.Dispose(); } if (content != null) { content.Dispose(); } _routedItemManager.Init(routedItem); _routedItemManager.Dequeue(Connection, Connection.toCloud, nameof(Connection.toCloud)); } catch (TaskCanceledException) { _logger.Log(LogLevel.Information, $"{taskInfo} Task was canceled."); } catch (HttpRequestException e) { _logger.Log(LogLevel.Warning, $"{taskInfo} Exception: {e.Message} {e.StackTrace}"); if (e.InnerException != null) { _logger.Log(LogLevel.Warning, $"{taskInfo} Inner Exception: {e.InnerException}"); } _liteHttpClient.DumpHttpClientDetails(); } catch (Exception e) { _logger.LogFullException(e, taskInfo); _liteHttpClient.DumpHttpClientDetails(); } finally { try { _taskManager.Stop($"{Connection.name}.putHL7"); if (streamContent != null) { streamContent.Dispose(); } if (response != null) { response.Dispose(); } if (content != null) { content.Dispose(); } } catch (Exception e) { _logger.LogFullException(e, taskInfo); } } }
/// <summary> /// Prepare an archived response based on a folder or fileStore in the response. /// </summary> /// <param name="targetStream"> /// The target Stream. /// </param> /// <param name="resourceResponse"> /// The resource response. /// </param> /// <param name="requestDataModelVersion"> /// The request data model version. /// </param> /// <param name="partition"> /// The database partition (schema) where the requested resource is stored. /// </param> /// <param name="routeSegments"> /// The route segments. /// </param> private void PrepareArchivedResponse( Stream targetStream, List <Thing> resourceResponse, Version requestDataModelVersion, string partition, dynamic routeSegments) { string folderPath = this.FileArchiveService.CreateFileStructure(resourceResponse, partition, routeSegments); try { var content = new MultipartContent("mixed", BoundaryString); using (var stream = new MemoryStream()) { this.CreateFilteredResponseStream(resourceResponse, stream, requestDataModelVersion); // rewind stream prior to reading stream.Position = 0; // write out the json content to the first multipart content entry var jsonContent = new StringContent(new StreamReader(stream).ReadToEnd()); jsonContent.Headers.Clear(); jsonContent.Headers.Add(this.ContentTypeHeader, this.MimeTypeJson); content.Add(jsonContent); stream.Flush(); } this.FileArchiveService.CreateZipArchive(folderPath); byte[] buffer; long fileSize; using (var fileStream = new FileStream(folderPath + ".zip", FileMode.Open)) { fileSize = fileStream.Length; buffer = new byte[(int)fileSize]; fileStream.Read(buffer, 0, (int)fileSize); } var binaryContent = new ByteArrayContent(buffer); binaryContent.Headers.Add(this.ContentTypeHeader, this.MimeTypeOctetStream); // use the file hash value to easily identify the multipart content for each respective filerevision hash entry binaryContent.Headers.Add( ContentDispositionHeader, $"attachment; filename={new DirectoryInfo(folderPath).Name + ".zip"}"); binaryContent.Headers.Add(ContentLengthHeader, fileSize.ToString()); content.Add(binaryContent); // stream the multipart content to the request contents target stream content.CopyToAsync(targetStream).Wait(); this.AddMultiPartMimeEndpoint(targetStream); } finally { this.FileArchiveService.DeleteFileStructureWithArchive(folderPath); } }
private static async Task <GoogleDriveResultDictionary> SendForm(HttpClient httpClient, Stream ms, Dictionary <string, object> dictMeta, string szMimeType, string idExisting) { string szResult = string.Empty; HttpResponseMessage response = null; // Create the form. The form itself needs the authtoken header using (MultipartContent form = new MultipartContent("related")) { // Next add the metadata - it is in Json format string szJSonMeta = JsonConvert.SerializeObject(dictMeta); using (StringContent metadata = new StringContent(szJSonMeta, System.Text.Encoding.UTF8)) { metadata.Headers.ContentType = new MediaTypeHeaderValue("application/json") { CharSet = "UTF-8" }; form.Add(metadata); // Finally, add the body, with its appropriate mime type. using (StreamContent body = new StreamContent(ms)) { body.Headers.ContentType = new MediaTypeHeaderValue(szMimeType); form.Add(body); try { response = (String.IsNullOrEmpty(idExisting)) ? await httpClient.PostAsync(new Uri(szURLUploadEndpoint), form).ConfigureAwait(false) : await httpClient.PatchAsync(new Uri(String.Format(CultureInfo.InvariantCulture, szURLUpdateEndpointTemplate, idExisting)), form, false).ConfigureAwait(false); szResult = await response.Content.ReadAsStringAsync().ConfigureAwait(false); response.EnsureSuccessStatusCode(); return((String.IsNullOrEmpty(szResult)) ? null : JsonConvert.DeserializeObject <GoogleDriveResultDictionary>(szResult)); } catch (HttpRequestException ex) { if (response == null) { throw new MyFlightbookException("Unknown error in GoogleDrive.PutFile", ex); } Dictionary <string, GoogleDriveError> d = String.IsNullOrEmpty(szResult) ? null : JsonConvert.DeserializeObject <Dictionary <string, GoogleDriveError> >(szResult); GoogleDriveError gde = (d == null || !d.ContainsKey("error")) ? null : d["error"]; if (gde != null && gde.code == 403 && gde.errors != null && gde.errors.Count > 0 && gde.errors[0].reason != null && gde.errors[0].reason.CompareCurrentCultureIgnoreCase("storageQuotaExceeded") == 0) { throw new MyFlightbookException(Resources.LocalizedText.GoogleDriveOutOfSpace); } else { throw new MyFlightbookException(response.ReasonPhrase + " " + (szResult ?? string.Empty)); } } finally { if (response != null) { response.Dispose(); } } } } } }
/// <summary> /// https://docs.microsoft.com/en-us/previous-versions/dynamicscrm-2016/developers-guide/mt607719(v=crm.8)?redirectedfrom=MSDN#example /// </summary> /// <param name="entities"></param> /// <returns></returns> public async Task ExecuteBatchUpdate(IEnumerable <IEntity> entities, Action <string> LogInfo = null, Action <string> LogError = null) { if (!entities.Any()) { return; } var httpVersion = new Version(1, 1); // divide entities into lists of 1000 each (max count within a batch) var batchEntityLists = new List <List <IEntity> >() { new List <IEntity>() }; foreach (var entity in entities) { if (batchEntityLists.Last().Count == 500) { batchEntityLists.Add(new List <IEntity>()); } batchEntityLists.Last().Add(entity); } LogInfo?.Invoke($"{batchEntityLists.Count} batches prepared, with a total of {entities.Count()} entities."); // create batch requests of maximum size 500 each foreach (var batchEntityList in batchEntityLists) //await batchEntityLists.ParallelForEachAsync(async batchEntityList => { var makeRequest = true; while (makeRequest) { makeRequest = false; HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "$batch"); request.Version = httpVersion; var batchId = Guid.NewGuid(); var batchContent = new MultipartContent("mixed", $"batch_{batchId}"); LogInfo?.Invoke($"Creating batch update {batchId} with {batchEntityList.Count} entities."); var changeSetId = Guid.NewGuid(); var changeSetContent = new MultipartContent("mixed", $"changeset_{changeSetId}"); int i = 1; foreach (var entity in batchEntityList) { var json = JsonHelper.ToJson(entity); var httpRequestMessage = new HttpRequestMessage(HttpMethod.Patch, $"{client.BaseAddress.AbsoluteUri}{entity.GetEntityLogicalName()}({entity.GetCrmId()})"); httpRequestMessage.Content = new StringContent(json, Encoding.UTF8, "application/json"); httpRequestMessage.Version = httpVersion; var httpMessageContent = new HttpMessageContent(httpRequestMessage); httpMessageContent.Headers.Remove("Content-Type"); httpMessageContent.Headers.Add("Content-Type", "application/http"); httpMessageContent.Headers.Add("Content-Transfer-Encoding", "binary"); httpMessageContent.Headers.Add("Content-ID", $"{i++}"); httpMessageContent.Headers.Add("OData-MaxVersion", "4.0"); httpMessageContent.Headers.Add("OData-Version", "4.0"); changeSetContent.Add(httpMessageContent); } batchContent.Add(changeSetContent); request.Content = batchContent; string responseText = string.Empty; try { var response = await client.SendAsync(request); responseText = await response.Content.ReadAsStringAsync(); response.EnsureSuccessStatusCode(); LogInfo?.Invoke($"Batch {batchId} successfully updated"); } catch (Exception ex) { LogError?.Invoke($"Batch update failed: {responseText}. Exception: {ex.ToString()}"); var input = ""; while (input != "0" && input != "1") { Console.WriteLine($"Choose action:"); Console.WriteLine($"0: Retry the failed batch"); Console.WriteLine($"1: Continue with next batch"); input = Console.ReadLine(); } if (input == "0") { LogInfo?.Invoke("Retrying batch..."); makeRequest = true; } else { LogInfo?.Invoke("Continuing with next batch..."); } } } } //maxDegreeOfParallelism: 5); }
/// <summary> /// This method is to call batch web API on behalf of user. /// </summary> /// <param name="webApiUrl">Batch Web API URL to call.</param> /// <param name="batchWebApiUrls">Batch of Web API URLs to call.</param> /// <param name="accessToken">Access token for authentication.</param> /// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns> public async Task <List <JObject> > CallBatchWebApiOnBehalfOfUserAsync(string webApiUrl, List <string> batchWebApiUrls, string accessToken) { List <JObject> peoplePresenceDataJObjectList = new List <JObject>(); if (!string.IsNullOrEmpty(accessToken)) { using MultipartContent batchContent = new MultipartContent("batching", Guid.NewGuid().ToString()); foreach (string url in batchWebApiUrls) { batchContent.Add( new HttpMessageContent( new HttpRequestMessage( HttpMethod.Get, url))); } using var request = new HttpRequestMessage(HttpMethod.Post, webApiUrl) { Content = batchContent, }; request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("multipart/batching")); using HttpResponseMessage httpResponseMessageMultiPart = HttpClient.SendAsync(request).Result; if (httpResponseMessageMultiPart.IsSuccessStatusCode) { MultipartMemoryStreamProvider multipartContent = await httpResponseMessageMultiPart.Content.ReadAsMultipartAsync(); foreach (HttpContent currentContent in multipartContent.Contents) { if (currentContent.Headers.ContentType.MediaType.Equals("application/http", StringComparison.OrdinalIgnoreCase)) { if (!currentContent.Headers.ContentType.Parameters.Any(parameter => parameter.Name.Equals("msgtype", StringComparison.OrdinalIgnoreCase) && parameter.Value.Equals("response", StringComparison.OrdinalIgnoreCase))) { currentContent.Headers.ContentType.Parameters.Add(new NameValueHeaderValue("msgtype", "response")); } using HttpResponseMessage httpResponseMessage = currentContent.ReadAsHttpResponseMessageAsync().Result; if (httpResponseMessage.IsSuccessStatusCode) { peoplePresenceDataJObjectList.Add(JObject.Parse(httpResponseMessage.Content.ReadAsStringAsync().Result)); } else if (httpResponseMessage.StatusCode == HttpStatusCode.TooManyRequests) { this.telemetryClient.TrackTrace($"HttpException:CallBatchWebApiOnBehalfOfUserAsync : Too many requests. Parameter: {string.Join(",", batchWebApiUrls)}", SeverityLevel.Error); break; } else { this.telemetryClient.TrackTrace( HttpResponseMessageTrace.GetHttpResponseMessageTrace( httpResponseMessage, $"HttpException:CallBatchWebApiOnBehalfOfUserAsync.WebApiUrl: {string.Join(",", batchWebApiUrls).Truncate(9500)}"), SeverityLevel.Error); } } } } else if (httpResponseMessageMultiPart.StatusCode == HttpStatusCode.TooManyRequests) { this.telemetryClient.TrackTrace($"BatchHttpException:CallBatchWebApiOnBehalfOfUserAsync : Too many requests. Parameter: {string.Join(",", batchWebApiUrls)}", SeverityLevel.Error); } else { this.telemetryClient.TrackTrace( HttpResponseMessageTrace.GetHttpResponseMessageTrace( httpResponseMessageMultiPart, $"BatchHttpException:CallBatchWebApiOnBehalfOfUserAsync.WebApiUrl: {string.Join(",", batchWebApiUrls)}"), SeverityLevel.Error); } } return(peoplePresenceDataJObjectList); }
public override async Task ExecuteResultAsync(IWebDavResponse response, CancellationToken ct) { await base.ExecuteResultAsync(response, ct).ConfigureAwait(false); response.Headers["Accept-Ranges"] = new[] { "bytes" }; var properties = await _document.GetProperties(response.Dispatcher).ToList(ct).ConfigureAwait(false); var etagProperty = properties.OfType <GetETagProperty>().FirstOrDefault(); if (etagProperty != null) { var propValue = await etagProperty.GetValueAsync(ct).ConfigureAwait(false); response.Headers["ETag"] = new[] { propValue.ToString() }; } if (!_returnFile) { var lastModifiedProp = properties.OfType <LastModifiedProperty>().FirstOrDefault(); if (lastModifiedProp != null) { var propValue = await lastModifiedProp.GetValueAsync(ct).ConfigureAwait(false); response.Headers["Last-Modified"] = new[] { propValue.ToString("R") }; } return; } var views = new List <StreamView>(); try { foreach (var rangeItem in _rangeItems) { var baseStream = await _document.OpenReadAsync(ct).ConfigureAwait(false); var streamView = await StreamView .CreateAsync(baseStream, rangeItem.From, rangeItem.Length, ct) .ConfigureAwait(false); views.Add(streamView); } string contentType; var contentTypeProp = properties.OfType <GetContentTypeProperty>().FirstOrDefault(); if (contentTypeProp != null) { contentType = await contentTypeProp.GetValueAsync(ct).ConfigureAwait(false); } else { contentType = MimeTypesMap.DefaultMimeType; } if (_rangeItems.Count == 1) { // No multipart content var rangeItem = _rangeItems.Single(); var streamView = views.Single(); using (var streamContent = new StreamContent(streamView)) { streamContent.Headers.ContentRange = new ContentRangeHeaderValue( rangeItem.From, rangeItem.To, _document.Length); streamContent.Headers.ContentLength = rangeItem.Length; streamContent.Headers.ContentType = MediaTypeHeaderValue.Parse(contentType); await SetPropertiesToContentHeaderAsync(streamContent, properties, ct) .ConfigureAwait(false); foreach (var header in streamContent.Headers) { response.Headers.Add(header.Key, header.Value.ToArray()); } // Use the CopyToAsync function of the stream itself, because // we're able to pass the cancellation token. This is a workaround // for issue dotnet/corefx#9071 and fixes FubarDevelopment/WebDavServer#47. await streamView.CopyToAsync(response.Body, 81920, ct) .ConfigureAwait(false); } } else { // Multipart content using (var multipart = new MultipartContent("byteranges")) { var index = 0; foreach (var rangeItem in _rangeItems) { var streamView = views[index++]; var partContent = new StreamContent(streamView); partContent.Headers.ContentRange = new ContentRangeHeaderValue( rangeItem.From, rangeItem.To, _document.Length); partContent.Headers.ContentType = MediaTypeHeaderValue.Parse(contentType); partContent.Headers.ContentLength = rangeItem.Length; multipart.Add(partContent); } await SetPropertiesToContentHeaderAsync(multipart, properties, ct) .ConfigureAwait(false); foreach (var header in multipart.Headers) { response.Headers.Add(header.Key, header.Value.ToArray()); } // TODO: Workaround for issue dotnet/corefx#9071 await multipart.CopyToAsync(response.Body).ConfigureAwait(false); } } } finally { foreach (var streamView in views) { streamView.Dispose(); } } }
/// <summary> /// <see cref="HttpContent"/> implementation which provides a byte range view over a stream used to generate HTTP /// 206 (Partial Content) byte range responses. If none of the requested ranges overlap with the current extend /// of the selected resource represented by the <paramref name="content"/> parameter then an /// <see cref="InvalidByteRangeException"/> is thrown indicating the valid Content-Range of the content. /// </summary> /// <param name="content">The stream over which to generate a byte range view.</param> /// <param name="range">The range or ranges, typically obtained from the Range HTTP request header field.</param> /// <param name="mediaType">The media type of the content stream.</param> /// <param name="bufferSize">The buffer size used when copying the content stream.</param> public ByteRangeStreamContent(Stream content, RangeHeaderValue range, MediaTypeHeaderValue mediaType, int bufferSize) { if (content == null) { throw new ArgumentNullException("content"); } if (!content.CanSeek) { throw new ArgumentException("content", RS.Format(Resources.ByteRangeStreamNotSeekable, typeof(ByteRangeStreamContent).Name)); } if (range == null) { throw new ArgumentNullException("range"); } if (mediaType == null) { throw new ArgumentNullException("mediaType"); } if (bufferSize < MinBufferSize) { throw new ArgumentOutOfRangeException("bufferSize", bufferSize, RS.Format(Resources.ArgumentMustBeGreaterThanOrEqualTo, MinBufferSize)); } if (!range.Unit.Equals(SupportedRangeUnit, StringComparison.OrdinalIgnoreCase)) { throw new ArgumentException(RS.Format(Resources.ByteRangeStreamContentNotBytesRange, range.Unit, SupportedRangeUnit), "range"); } try { // If we have more than one range then we use a multipart/byteranges content type as wrapper. // Otherwise we use a non-multipart response. if (range.Ranges.Count > 1) { // Create Multipart content and copy headers to this content MultipartContent rangeContent = new MultipartContent(ByteRangesContentSubtype); _byteRangeContent = rangeContent; foreach (RangeItemHeaderValue rangeValue in range.Ranges) { try { ByteRangeStream rangeStream = new ByteRangeStream(content, rangeValue); HttpContent rangeBodyPart = new StreamContent(rangeStream, bufferSize); rangeBodyPart.Headers.ContentType = mediaType; rangeBodyPart.Headers.ContentRange = rangeStream.ContentRange; rangeContent.Add(rangeBodyPart); } catch (ArgumentOutOfRangeException) { // We ignore range errors until we check that we have at least one valid range } } // If no overlapping ranges were found then stop if (!rangeContent.Any()) { ContentRangeHeaderValue actualContentRange = new ContentRangeHeaderValue(content.Length); string msg = RS.Format(Resources.ByteRangeStreamNoneOverlap, range.ToString()); throw new InvalidByteRangeException(actualContentRange, msg); } } else if (range.Ranges.Count == 1) { try { ByteRangeStream rangeStream = new ByteRangeStream(content, range.Ranges.First()); _byteRangeContent = new StreamContent(rangeStream, bufferSize); _byteRangeContent.Headers.ContentType = mediaType; _byteRangeContent.Headers.ContentRange = rangeStream.ContentRange; } catch (ArgumentOutOfRangeException) { ContentRangeHeaderValue actualContentRange = new ContentRangeHeaderValue(content.Length); string msg = RS.Format(Resources.ByteRangeStreamNoOverlap, range.ToString()); throw new InvalidByteRangeException(actualContentRange, msg); } } else { throw new ArgumentException(Resources.ByteRangeStreamContentNoRanges, "range"); } // Copy headers from byte range content so that we get the right content type etc. foreach (KeyValuePair <string, IEnumerable <string> > header in _byteRangeContent.Headers) { Headers.TryAddWithoutValidation(header.Key, header.Value); } _content = content; _start = content.Position; } catch { if (_byteRangeContent != null) { _byteRangeContent.Dispose(); } throw; } }
private async Task <List <HttpResponseMessage> > SendBatchAndCheckReturnResponsesSuccessAsync(MultipartContent content) { HttpResponseMessage response = await SendContentAsBatchRequest(content); List <HttpResponseMessage> responses = await ExtractResponsesFromBatch(response); foreach (HttpResponseMessage individualResponse in responses) { individualResponse.EnsureSuccessStatusCode(); } return(responses); }
private bool UploadMultipartRevision(RevisionInternal revision) { MultipartContent multiPart = null; var length = default(double); var revProps = revision.GetProperties(); var attachments = revProps.Get("_attachments").AsDictionary <string, object>(); if (attachments == null) { return(false); } foreach (var attachmentKey in attachments.Keys) { var attachment = attachments.Get(attachmentKey).AsDictionary <string, object>(); if (attachment.ContainsKey("follows")) { if (multiPart == null) { multiPart = new MultipartContent("related"); try { var json = Manager.GetObjectMapper().WriteValueAsString(revProps); var utf8charset = Encoding.UTF8; //multiPart.Add(new StringContent(json, utf8charset, "application/json"), "param1"); var jsonContent = new StringContent(json, utf8charset, "application/json"); //jsonContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); multiPart.Add(jsonContent); length += json.Length; } catch (Exception e) { throw Misc.CreateExceptionAndLog(Log.To.Sync, e, TAG, "Not able to serialize revision properties into a multipart request content."); } } var blobStore = LocalDatabase.Attachments; var base64Digest = (string)attachment.Get("digest"); var blobKey = new BlobKey(base64Digest); var inputStream = blobStore.BlobStreamForKey(blobKey); if (inputStream == null) { Log.To.Sync.W(TAG, "Unable to find blob file for blobKey: {0} - Skipping upload of multipart revision.", blobKey); multiPart = null; length = 0; } else { string contentType = null; if (attachment.ContainsKey("content_type")) { contentType = (string)attachment.Get("content_type"); } else { if (attachment.ContainsKey("content-type")) { var message = string.Format("Found attachment that uses content-type" + " field name instead of content_type (see couchbase-lite-android" + " issue #80): " + attachment); Log.To.Sync.W(TAG, message); } } var content = new StreamContent(inputStream); content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = attachmentKey }; if (!String.IsNullOrEmpty(contentType)) { content.Headers.ContentType = new MediaTypeHeaderValue(contentType); } multiPart.Add(content); length += attachment.GetCast <double>("length"); } } } if (multiPart == null) { return(false); } var path = string.Format("/{0}?new_edits=false", revision.DocID); // TODO: need to throttle these requests Log.To.Sync.D(TAG, "{0} uploading multipart request. Revision: {1}", this, revision); SafeAddToChangesCount(1); _remoteSession.SendAsyncMultipartRequest(HttpMethod.Put, path, multiPart, (result, e) => { if (e != null) { var httpError = Misc.Flatten(e).FirstOrDefault(ex => ex is HttpResponseException) as HttpResponseException; if (httpError != null) { if (httpError.StatusCode == System.Net.HttpStatusCode.UnsupportedMediaType) { _dontSendMultipart = true; UploadJsonRevision(revision); } else if (httpError.StatusCode == System.Net.HttpStatusCode.Forbidden) { LastError = Misc.CreateExceptionAndLog(Log.To.Sync, StatusCode.Forbidden, TAG, $"{revision.DocID} was rejected by the endpoint with message"); } } else { LastError = e; RevisionFailed(); } } else { Log.To.Sync.V(TAG, "{0} sent multipart {1}", this, revision); SafeIncrementCompletedChangesCount(); RemovePending(revision); } }); Log.To.Sync.V(TAG, "{0} queuing revision (multipart, ~{1}kb)", this, length / 1024.0); return(true); }
/// <summary> /// Put's a file as a stream using the REST API documented at https://developers.google.com/drive/v3/web/manage-uploads#multipart /// </summary> /// <param name="szFileName">The file name to use</param> /// <param name="ms">The stream of the data</param> /// <param name="szMimeType">The mime type for the data</param> /// <returns>True for success</returns> /// <exception cref="MyFlightbookException"></exception> /// <exception cref="System.Net.Http.HttpRequestException"></exception> public async Task <IReadOnlyDictionary <string, string> > PutFile(Stream ms, string szFileName, string szMimeType) { if (!CheckAccessToken()) { throw new MyFlightbookException("Google drive: access token missing or expired"); } bool fIsCSV = szMimeType.CompareCurrentCultureIgnoreCase("text/csv") == 0; ms.Seek(0, SeekOrigin.Begin); // write out the whole stream. UploadAsync appears to pick up from the current location, which is the end-of-file after writing to a ZIP. string szResult = string.Empty; HttpResponseMessage response = null; using (HttpClient httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + AuthState.AccessToken); if (String.IsNullOrEmpty(RootFolderID)) { RootFolderID = await IDForFolder(RootPath); if (String.IsNullOrEmpty(RootFolderID)) { RootFolderID = await CreateFolder(Branding.CurrentBrand.AppName); } } // CSV loses its extension when uploaded because we map it to a google spreadsheet. So if it's CSV AND we are patching an existing file, drop the extension so that we ov // update the existing file if it is present. If CSV, strip the extension string szFileNameToCheck = fIsCSV ? Path.GetFileNameWithoutExtension(szFileName) : szFileName; string idExisting = null; if (!String.IsNullOrEmpty(RootFolderID)) { idExisting = await IDForFile(szFileNameToCheck, RootFolderID); } // If we got a hit, use that filename for the udpate if (!String.IsNullOrEmpty(idExisting)) { szFileName = szFileNameToCheck; } // Create the metadata. Name is most important, but we can also specify mimeType for CSV to import into GoogleDocs Dictionary <string, object> dictMeta = new Dictionary <string, object>() { { "name", szFileName } }; if (fIsCSV) { dictMeta["mimeType"] = "application/vnd.google-apps.spreadsheet"; // get it to show up in google drive sheets. } if (String.IsNullOrEmpty(idExisting) && !String.IsNullOrEmpty(RootFolderID)) { dictMeta["parents"] = new List <string>() { RootFolderID } } ; // Create the form. The form itself needs the authtoken header using (MultipartContent form = new MultipartContent("related")) { // Next add the metadata - it is in Json format string szJSonMeta = JsonConvert.SerializeObject(dictMeta); StringContent metadata = new StringContent(szJSonMeta, System.Text.Encoding.UTF8); metadata.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json") { CharSet = "UTF-8" }; form.Add(metadata); // Finally, add the body, with its appropriate mime type. StreamContent body = new StreamContent(ms); body.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(szMimeType); form.Add(body); try { response = (String.IsNullOrEmpty(idExisting)) ? await httpClient.PostAsync(szURLUploadEndpoint, form) : await httpClient.PatchAsync(new Uri(String.Format(szURLUpdateEndpointTemplate, idExisting)), form); szResult = response.Content.ReadAsStringAsync().Result; response.EnsureSuccessStatusCode(); return((String.IsNullOrEmpty(szResult)) ? null : JsonConvert.DeserializeObject <Dictionary <string, string> >(szResult)); } catch (System.Net.Http.HttpRequestException ex) { if (response == null) { throw new MyFlightbookException("Unknown error in GoogleDrive.PutFile", ex); } Dictionary <string, GoogleDriveError> d = String.IsNullOrEmpty(szResult) ? null : JsonConvert.DeserializeObject <Dictionary <string, GoogleDriveError> >(szResult); GoogleDriveError gde = (d == null || !d.ContainsKey("error")) ? null : d["error"]; if (gde != null && gde.code == 403 && gde.errors != null && gde.errors.Length > 0 && gde.errors[0].reason != null && gde.errors[0].reason.CompareCurrentCultureIgnoreCase("storageQuotaExceeded") == 0) { throw new MyFlightbookException(Resources.LocalizedText.GoogleDriveOutOfSpace); } else { throw new MyFlightbookException(response.ReasonPhrase + " " + (szResult ?? string.Empty)); } } } } } }
public void DeleteMultipleBlobs(List <string> blobs) { if (blobs.Count == 0) { return; } const string xMsDate = "x-ms-date"; const string xMsClientRequestId = "x-ms-client-request-id"; const string xMsReturnClientRequestId = "x-ms-return-client-request-id"; var now = SystemTime.UtcNow.ToString("R"); var url = $"{GetBaseServerUrl()}/?comp=batch"; var requestMessage = new HttpRequestMessage(HttpMethods.Post, url) { Headers = { { xMsDate, now }, { "x-ms-version", AzureStorageVersion } } }; var batchContent = new MultipartContent("mixed", $"batch_{Guid.NewGuid()}"); requestMessage.Content = batchContent; var blobsWithIds = new Dictionary <string, string>(); for (var i = 0; i < blobs.Count; i++) { using var ms = new MemoryStream(); using var writer = new StreamWriter(ms); var clientRequestId = Guid.NewGuid().ToString(); blobsWithIds[clientRequestId] = blobs[i]; writer.WriteLine($"{HttpMethods.Delete} /{_containerName}/{blobs[i]} HTTP/1.1"); writer.WriteLine($"{xMsDate}: {now}"); writer.WriteLine($"{xMsClientRequestId}: {clientRequestId}"); writer.WriteLine($"{xMsReturnClientRequestId}: true"); using (var hash = new HMACSHA256(_accountKey)) { var uri = new Uri($"{GetBaseServerUrl()}/{_containerName}/{blobs[i]}", UriKind.Absolute); var hashStr = $"{HttpMethods.Delete}\n\n\n\n\n\n\n\n\n\n\n\n{xMsClientRequestId}:{clientRequestId}\n{xMsDate}:{now}\n{xMsReturnClientRequestId}:true\n/{_accountName}{uri.AbsolutePath}"; writer.WriteLine($"Authorization: SharedKey {_accountName}:{Convert.ToBase64String(hash.ComputeHash(Encoding.UTF8.GetBytes(hashStr)))}"); } writer.WriteLine("Content-Length: 0"); writer.Flush(); batchContent.Add(new ByteArrayContent(ms.ToArray()) { Headers = { { "Content-Type", "application/http" }, { "Content-Transfer-Encoding", "binary" }, { "Content-ID", $"{i}" } } }); } var client = GetClient(); if (batchContent.Headers.ContentLength.HasValue == false) { // we need the ContentLength to CalculateAuthorizationHeaderValue // the ContentLength is calculated on the fly, it gets added to Headers only when we try to access it. throw new ArgumentException($"{nameof(MultipartContent)} should have content length"); } client.DefaultRequestHeaders.Authorization = CalculateAuthorizationHeaderValue(HttpMethods.Post, url, requestMessage.Headers, batchContent.Headers); var response = client.SendAsync(requestMessage).Result; if (response.IsSuccessStatusCode == false) { throw StorageException.FromResponseMessage(response); } using var stream = response.Content.ReadAsStreamAsync().Result; using var reader = new StreamReader(stream); const string statusCode = "StatusCode"; const string status = "Status"; var responseBoundary = $"--{response.Content.Headers.ContentType.Parameters.First().Value}"; var result = new Dictionary <string, Dictionary <string, string> >(); while (reader.Peek() >= 0) { var line = reader.ReadLine(); // read batch response if (line != responseBoundary && line != $"{responseBoundary}--") { throw new InvalidDataException("Got invalid response from server."); } while (string.IsNullOrEmpty(line) == false) { line = reader.ReadLine(); } line = reader.ReadLine(); // read sub-response block if (string.IsNullOrEmpty(line)) { break; } string[] res = line.Split(" "); var responseDictionary = new Dictionary <string, string> { { statusCode, res[1] }, { status, res[1] == "202" ? res[res.Length - 1] : string.Join(" ", res, 2, res.Length - 2) } }; line = reader.ReadLine(); while (string.IsNullOrEmpty(line) == false) { var r = line.Split(": "); responseDictionary[r.First()] = r.Last(); line = reader.ReadLine(); } result[blobsWithIds[responseDictionary["x-ms-client-request-id"]]] = responseDictionary; if (responseDictionary.TryGetValue("x-ms-error-code", out _)) { // read the error message body line = reader.ReadLine(); if (line.StartsWith("<?xml")) { while (line.EndsWith("</Error>") == false) { line = reader.ReadLine(); } } else { while (string.IsNullOrEmpty(line) == false) { line = reader.ReadLine(); } } } } var errors = result.Keys.Where(key => result[key][status] != "Accepted").ToDictionary(key => key, key => result[key][status]); var canLog = _logger != null && _logger.IsInfoEnabled; if (errors.Count == 0) { if (canLog) { _logger.Info($"Successfully deleted {result.Count} blob{Pluralize(result.Count)} from container: {_containerName}."); } return; } var reasons = errors.Values.Distinct().ToArray(); var failedToDeleteReasons = reasons.Aggregate(string.Empty, (current, r) => current + $"Reason: {r} Blobs ({errors.Count(x => x.Value == r)}): {string.Join(", ", errors.Where(x => x.Value == r).Select(y => y.Key))}. "); var message = $"Failed to delete {errors.Count} blob{Pluralize(errors.Count)} from container: {_containerName}. Successfully deleted {result.Count - errors.Count} blob{Pluralize(result.Count - errors.Count)}. {failedToDeleteReasons}"; if (canLog) { _logger.Info(message); } string Pluralize(int num) { return(num == 0 || num > 1 ? "s" : string.Empty); } throw new InvalidOperationException(message); }
internal void SendAsyncMultipartRequest(HttpMethod method, String relativePath, MultipartContent multiPartEntity, RemoteRequestCompletionBlock completionHandler) { Uri url = null; try { var urlStr = BuildRelativeURLString(relativePath); url = new Uri(urlStr); } catch (UriFormatException e) { throw new ArgumentException("Invalid URI format.", e); } var message = new HttpRequestMessage(method, url); message.Content = multiPartEntity; message.Headers.Add("Accept", "*/*"); var client = clientFactory.GetHttpClient(false); var authHeader = AuthUtils.GetAuthenticationHeaderValue(Authenticator, message.RequestUri); if (authHeader != null) { client.DefaultRequestHeaders.Authorization = authHeader; } client.SendAsync(message, CancellationTokenSource.Token).ContinueWith(response=> { multiPartEntity.Dispose(); if (response.Status != TaskStatus.RanToCompletion) { Log.E(TAG, "SendAsyncRequest did not run to completion.", response.Exception); client.Dispose(); return null; } if ((Int32)response.Result.StatusCode > 300) { LastError = new HttpResponseException(response.Result.StatusCode); Log.E(TAG, "Server returned HTTP Error", LastError); client.Dispose(); return null; } return response.Result.Content.ReadAsStreamAsync(); }, CancellationTokenSource.Token).ContinueWith(response=> { try { var hasEmptyResult = response.Result == null || response.Result.Result == null || response.Result.Result.Length == 0; if (response.Status != TaskStatus.RanToCompletion) { Log.E (TAG, "SendAsyncRequest did not run to completion.", response.Exception); } else if (hasEmptyResult) { Log.E (TAG, "Server returned an empty response.", response.Exception ?? LastError); } if (completionHandler != null) { object fullBody = null; if (!hasEmptyResult) { var mapper = Manager.GetObjectMapper(); fullBody = mapper.ReadValue<Object> (response.Result.Result); } completionHandler (fullBody, response.Exception); } } finally { client.Dispose(); } }, CancellationTokenSource.Token); }
public static IEnumerable <object[]> PostAsync_Cancel_CancellationTokenPassedToContent_MemberData() { // Note: For HTTP2, the actual token will be a linked token and will not be an exact match for the original token. // Verify that it behaves as expected by cancelling it and validating that cancellation propagates. // StreamContent { CancellationTokenSource tokenSource = new CancellationTokenSource(); var actualToken = new StrongBox <CancellationToken>(); bool called = false; var content = new StreamContent(new DelegateStream( canReadFunc: () => true, readAsyncFunc: (buffer, offset, count, cancellationToken) => { int result = 1; if (called) { result = 0; Assert.False(cancellationToken.IsCancellationRequested); tokenSource.Cancel(); Assert.True(cancellationToken.IsCancellationRequested); } called = true; return(Task.FromResult(result)); } )); yield return(new object[] { content, tokenSource }); } // MultipartContent { CancellationTokenSource tokenSource = new CancellationTokenSource(); var actualToken = new StrongBox <CancellationToken>(); bool called = false; var content = new MultipartContent(); content.Add(new StreamContent(new DelegateStream( canReadFunc: () => true, canSeekFunc: () => true, lengthFunc: () => 1, positionGetFunc: () => 0, positionSetFunc: _ => {}, readAsyncFunc: (buffer, offset, count, cancellationToken) => { int result = 1; if (called) { result = 0; Assert.False(cancellationToken.IsCancellationRequested); tokenSource.Cancel(); Assert.True(cancellationToken.IsCancellationRequested); } called = true; return(Task.FromResult(result)); } ))); yield return(new object[] { content, tokenSource }); } // MultipartFormDataContent { CancellationTokenSource tokenSource = new CancellationTokenSource(); var actualToken = new StrongBox <CancellationToken>(); bool called = false; var content = new MultipartFormDataContent(); content.Add(new StreamContent(new DelegateStream( canReadFunc: () => true, canSeekFunc: () => true, lengthFunc: () => 1, positionGetFunc: () => 0, positionSetFunc: _ => {}, readAsyncFunc: (buffer, offset, count, cancellationToken) => { int result = 1; if (called) { result = 0; Assert.False(cancellationToken.IsCancellationRequested); tokenSource.Cancel(); Assert.True(cancellationToken.IsCancellationRequested); } called = true; return(Task.FromResult(result)); } ))); yield return(new object[] { content, tokenSource }); } }
private void ClearRequestData() { _content = null; _addedUrlParams = null; _addedParams = null; _addedMultipartContent = null; _addedHeaders = null; }
public async Task PushAsync_ExecutesThePendingOperations_InOrder_BatchedOne() { var hijack = new TestHttpHandler(); IMobileServiceClient service = new MobileServiceClient(MobileAppUriValidator.DummyMobileApp, hijack); var store = new MobileServiceLocalStoreMock(); await service.SyncContext.InitializeAsync(store, new MobileServiceSyncHandler()); IMobileServiceSyncTable table = service.GetSyncTable("someTable"); JObject item1 = new JObject() { { "id", "abc" } }, item2 = new JObject() { { "id", "def" } }; await table.InsertAsync(item1); await table.InsertAsync(item2); Assert.AreEqual(hijack.Requests.Count, 0); // create a new service to test that operations are loaded from store hijack = new TestHttpHandler(); var content = new MultipartContent("mixed", "6f078995-ef2a-4617-a4c9-5d8746b26d32"); var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{\"id\":\"abc\",\"String\":\"Hey\"}", Encoding.UTF8, "application/json") }; content.Add(new HttpMessageContent(response)); hijack.AddResponseContent(content); content = new MultipartContent("mixed", "6f078995-ef2a-4617-a4c9-5d8746b26d32"); response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{\"id\":\"def\",\"String\":\"What\"}", Encoding.UTF8, "application/json") }; content.Add(new HttpMessageContent(response)); hijack.AddResponseContent(content); service = new MobileServiceClient(MobileAppUriValidator.DummyMobileApp, hijack); await service.SyncContext.InitializeAsync(store, new MobileServiceSyncHandler()); service.SyncContext.BatchApiEndpoint = "batch"; service.SyncContext.BatchSize = 1; Assert.AreEqual(hijack.Requests.Count, 0); await service.SyncContext.PushAsync(); Assert.AreEqual(hijack.Requests.Count, 2); Assert.IsTrue(hijack.RequestContents[0].Contains(item1.ToString(Formatting.None))); Assert.IsTrue(hijack.RequestContents[0].Contains("X-ZUMO-FEATURES: TU,OL")); Assert.IsTrue(hijack.RequestContents[1].Contains(item2.ToString(Formatting.None))); Assert.IsTrue(hijack.RequestContents[1].Contains("X-ZUMO-FEATURES: TU,OL")); // create yet another service to make sure the old items were purged from queue hijack = new TestHttpHandler(); hijack.AddResponseContent("{\"id\":\"abc\",\"String\":\"Hey\"}"); service = new MobileServiceClient(MobileAppUriValidator.DummyMobileApp, hijack); await service.SyncContext.InitializeAsync(store, new MobileServiceSyncHandler()); Assert.AreEqual(hijack.Requests.Count, 0); await service.SyncContext.PushAsync(); Assert.AreEqual(hijack.Requests.Count, 0); }
public async Task<bool> CreatePostWithMedia(string messageUserId, string post, String path, byte[] fileStream, UserAccountEntity userAccountEntity) { try { var user = userAccountEntity.GetUserEntity(); var url = string.Format(EndPoints.CreatePost, user.Region, messageUserId); const string boundary = "abcdefghijklmnopqrstuvwxyz"; var messageJson = new SendMessage { message = new Message() { body = post, fakeMessageUid = 1384958573288, messageKind = 1 } }; var json = JsonConvert.SerializeObject(messageJson); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); stringContent.Headers.Add("Content-Description", "message"); var form = new MultipartContent("mixed", boundary) { stringContent }; Stream stream = new MemoryStream(fileStream); var t = new StreamContent(stream); var s = Path.GetExtension(path); if (s != null && s.Equals(".png")) { t.Headers.ContentType = new MediaTypeHeaderValue("image/png"); } else { var extension = Path.GetExtension(path); if (extension != null && (extension.Equals(".jpg") || extension.Equals(".jpeg"))) { t.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg"); } else { t.Headers.ContentType = new MediaTypeHeaderValue("image/gif"); } } t.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg"); t.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); t.Headers.Add("Content-Description", "image-data-0"); t.Headers.Add("Content-Transfer-Encoding", "binary"); t.Headers.ContentLength = stream.Length; form.Add(t); var result = await _webManager.PostData(new Uri(url), form, null, userAccountEntity); return result.IsSuccess; } catch (Exception ex) { throw new Exception("Failed to send post (with media)", ex); } }
public async Task<bool> CreatePost(string messageUserId, string post, UserAccountEntity userAccountEntity) { try { var user = userAccountEntity.GetUserEntity(); var url = string.Format(EndPoints.CreatePost, user.Region, messageUserId); const string boundary = "abcdefghijklmnopqrstuvwxyz"; var messageJson = new SendMessage { message = new Message() { body = post, fakeMessageUid = 1384958573288, messageKind = 1 } }; var json = JsonConvert.SerializeObject(messageJson); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); stringContent.Headers.Add("Content-Description", "message"); var form = new MultipartContent("mixed", boundary) { stringContent }; var result = await _webManager.PostData(new Uri(url), form, null, userAccountEntity); return result.IsSuccess; } catch (Exception ex) { throw new Exception("Failed to send post", ex); } }
private void ClearRequestData() { _content = null; _temporaryUrlParams = null; _temporaryParams = null; _temporaryMultipartContent = null; _temporaryHeaders = null; }
/// <summary> /// Create a batching request content. The content contains three body part requests. /// </summary> /// <param name="baseAddress"></param> /// <param name="batchContent"></param> private static void CreateBatchedRequest(string baseAddress, MultipartContent batchContent) { batchContent.Add( new HttpMessageContent( new HttpRequestMessage( HttpMethod.Get, baseAddress + "/api/demo" ) ) ); // this request is expected to recieve a 404 batchContent.Add( new HttpMessageContent( new HttpRequestMessage( HttpMethod.Get, baseAddress + "/not/found" ) ) ); batchContent.Add( new HttpMessageContent( new HttpRequestMessage( HttpMethod.Get, baseAddress + "/api/demo/1" ) ) ); }
public void Add_Invalid() { var m = new MultipartContent ("a", "b"); try { m.Add (null); Assert.Fail ("#1"); } catch (ArgumentNullException) { } }