Exemple #1
0
        public int AddContent(string url, string method = "POST", string body = "{}")
        {
            HttpRequestMessage request = new HttpRequestMessage(new HttpMethod(method), $"{this.crmClient.CrmOdataUrl}{url}");

            request.Version = new Version(1, 1); // HTTP/2 breaks
            request.Content = new JsonContent(body);
            HttpMessageContent content = new HttpMessageContent(request);

            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/http");
            content.Headers.Add("Content-Transfer-Encoding", "binary");
            this.ProceedWhenUnlocked();
            int index = (Interlocked.Increment(ref this.contentCount) - 1);

            this.contents.AddOrUpdate(index, content, (oldKey, oldValue) => content);
            if (this.AutoCompleteOnCount > 0 && this.contentCount >= this.AutoCompleteOnCount)
            {
                this.Complete().Wait();
            }
            return(index);
        }
        public override HttpActionDescriptor SelectAction(
            HttpControllerContext context)
        {
            HttpMessageContent requestContent = new HttpMessageContent(
                context.Request);
            var json = requestContent.HttpRequestMessage.Content
                       .ReadAsStringAsync().Result;
            string type = (string)JObject.Parse(json)["Type"];

            var actionMethod = context.ControllerDescriptor.ControllerType
                               .GetMethods(BindingFlags.Instance | BindingFlags.Public)
                               .FirstOrDefault(m => m.Name == "Create");

            if (actionMethod != null)
            {
                return(new ReflectedHttpActionDescriptor(
                           context.ControllerDescriptor, actionMethod));
            }

            return(base.SelectAction(context));
        }
Exemple #3
0
        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)
        {
            HttpMessageContent requestContent = new HttpMessageContent(request);
            string             requestMessage = await requestContent.ReadAsStringAsync();

            var response = await base.SendAsync(request, cancellationToken);

            HttpMessageContent responseContent = new HttpMessageContent(response);
            string             responseMessage = await responseContent.ReadAsStringAsync();

            GlobalConfiguration.Configuration.Services.GetTraceWriter()
            .Trace(request, "System.Web.Http.MessageHandlers",
                   System.Web.Http.Tracing.TraceLevel.Info,
                   (t) =>
            {
                t.Message = String.Format("\n{0}\n{1}\n", requestMessage, responseMessage);
            });

            return(response);
        }
        public override HttpActionDescriptor SelectAction(HttpControllerContext controllerContext)
        {
            if (!controllerContext.ControllerDescriptor.ControllerName.Contains("Redox"))
            {
                return(base.SelectAction(controllerContext));
            }

            var requestContent = new HttpMessageContent(controllerContext.Request);
            var json           = requestContent.HttpRequestMessage.Content.ReadAsStringAsync().Result;
            var body           = JObject.Parse(json);

            // If we're using the redox controller due to a route like /api/redox, let's check out what's in the body
            // 1. This could be a dang ol' verification request
            var actions   = controllerContext.ControllerDescriptor.ControllerType.GetMethods(BindingFlags.Instance | BindingFlags.Public);
            var challenge = (string)body.SelectToken("challenge");

            if (!string.IsNullOrEmpty(challenge))
            {
                var action = actions.Single(x => x.Name == "Verify");
                return(new ReflectedHttpActionDescriptor(controllerContext.ControllerDescriptor, action));
            }

            // 2. This could be a datamodel then, just call the method named datamodel_eventtype
            var datamodel = (string)body.SelectToken("Meta.DataModel");
            var eventType = (string)body.SelectToken("Meta.EventType");

            if (!string.IsNullOrEmpty(datamodel) && !string.IsNullOrEmpty(eventType))
            {
                // Expect there to be an action with the right name
                var action = actions.SingleOrDefault(x => x.Name == datamodel + "_" + eventType);
                if (action != null)
                {
                    return(new ReflectedHttpActionDescriptor(controllerContext.ControllerDescriptor, action));
                }
            }

            // Idunno what this is, just let MVC figure it out.
            return(base.SelectAction(controllerContext));
        }
Exemple #6
0
        /// <summary>
        /// Builds the multipart content for a batch request using multiple change sets.
        /// </summary>
        private MultipartContent BuildMixedMultipartContent(SLBatchRequest batchRequest, string boundary)
        {
            var multipartContent = new MultipartContent("mixed", boundary);
            var request          = new HttpRequestMessage(batchRequest.HttpMethod, Url.Combine(ServiceLayerRoot.ToString(), batchRequest.Resource));

            if (batchRequest.Data != null)
            {
                request.Content = new StringContent(JsonConvert.SerializeObject(batchRequest.Data, batchRequest.JsonSerializerSettings), batchRequest.Encoding, "application/json");
            }

            if (batchRequest.ContentID.HasValue)
            {
                request.Content.Headers.Add("Content-ID", batchRequest.ContentID.ToString());
            }

            var innerContent = new HttpMessageContent(request);

            innerContent.Headers.Add("content-transfer-encoding", "binary");
            multipartContent.Add(innerContent);

            return(multipartContent);
        }
Exemple #7
0
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            HttpMessageContent requestContent = new HttpMessageContent(request);
            string             requestMessage = requestContent.ReadAsStringAsync().Result;

            var response = await base.SendAsync(request, cancellationToken);

            HttpMessageContent responseContent = new HttpMessageContent(response);
            string             responseMessage = responseContent.ReadAsStringAsync().Result;

            GlobalConfiguration.Configuration.Services.GetTraceWriter()
            .Trace(request, "MyCategory", System.Web.Http.Tracing.TraceLevel.Info,
                   (t) =>
            {
                t.Operation = request.Method.Method;
                t.Operator  = Thread.CurrentPrincipal.Identity.Name;
                t.Message   = requestMessage + Environment.NewLine + responseMessage;
            });


            return(response);
        }
        public Task SerializeAsync(Task <HttpResponseMessage> response, Stream stream)
        {
            return(response.Then(r =>
            {
                if (r.Content != null)
                {
                    TraceWriter.WriteLine("SerializeAsync - before load",
                                          TraceLevel.Verbose);

                    return r.Content.LoadIntoBufferAsync()
                    .Then(() =>
                    {
                        TraceWriter.WriteLine("SerializeAsync - after load", TraceLevel.Verbose);
                        var httpMessageContent2 = new HttpMessageContent(r);
                        // All in-memory and CPU-bound so no need to async
                        return httpMessageContent2.ReadAsByteArrayAsync();
                    })
                    .Then(buffer =>
                    {
                        TraceWriter.WriteLine("SerializeAsync - after ReadAsByteArrayAsync",
                                              TraceLevel.Verbose);
                        return Task.Factory.FromAsync(stream.BeginWrite, stream.EndWrite,
                                                      buffer, 0, buffer.Length, null, TaskCreationOptions.AttachedToParent);
                    }
                          );

                    ;
                }
                TraceWriter.WriteLine("Content NULL - before load",
                                      TraceLevel.Verbose);

                var httpMessageContent = new HttpMessageContent(r);
                // All in-memory and CPU-bound so no need to async
                var buffer2 = httpMessageContent.ReadAsByteArrayAsync().Result;
                return Task.Factory.FromAsync(stream.BeginWrite, stream.EndWrite,
                                              buffer2, 0, buffer2.Length, null, TaskCreationOptions.AttachedToParent);
            }
                                 ));
        }
        public async Task SerializeAsync(HttpRequestMessage request, Stream stream)
        {
            if (request.Content != null)
            {
                await request.Content.LoadIntoBufferAsync();

                var httpMessageContent = new HttpMessageContent(request);
                // All in-memory and CPU-bound so no need to async
                var buffer = await httpMessageContent.ReadAsByteArrayAsync();

                await Task.Factory.FromAsync(stream.BeginWrite, stream.EndWrite,
                                             buffer, 0, buffer.Length, null, TaskCreationOptions.AttachedToParent);
            }
            else
            {
                var httpMessageContent = new HttpMessageContent(request);
                // All in-memory and CPU-bound so no need to async
                var buffer = await httpMessageContent.ReadAsByteArrayAsync();

                await Task.Factory.FromAsync(stream.BeginWrite, stream.EndWrite,
                                             buffer, 0, buffer.Length, null, TaskCreationOptions.AttachedToParent);
            }
        }
Exemple #10
0
        public async Task GetDisplaysBatchAsync()
        {
            //A batch API endpoint can be used to send multiple requests together. This cuts down on excess HTTP traffic.
            //This Get display batch request can be done as a normal GET request if a batch request is not required.

            //Create the multipart/mixed message content
            MultipartContent content = new MultipartContent("mixed", "batch_" + Guid.NewGuid());

            foreach (Display display in displaysToUse)
            {
                //Create a message to get a display.
                //Create the different parts of the multipart content
                HttpMessageContent getDisplayContent = new HttpMessageContent(new HttpRequestMessage(HttpMethod.Get, $"{ApiServer}/API/api/displays/{display.SerialNumber}"));
                content.Add(getDisplayContent);
            }

            var responses = await SendBatchAndCheckReturnResponsesSuccessAsync(content);

            //Take the response list and read them into a display list
            List <GetDisplaysResponse> displays = new List <GetDisplaysResponse> {
            };

            foreach (HttpResponseMessage individualResponse in responses)
            {
                GetDisplaysResponse displayDetails = await individualResponse.Content.ReadAsAsync <GetDisplaysResponse>();

                displays.Add(displayDetails);
            }

            //Check the results
            Assert.AreEqual(displaysToUse.Count(), displays.Count());

            for (int index = 0; index < displaysToUse.Count; index++)
            {
                Assert.AreEqual(displaysToUse[index].SerialNumber, displays[index].SerialNumber);
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="args"></param>
        /// <param name="context"></param>
        /// <param name="connection"></param>
        /// <returns></returns>
        public async Task ProcessResponse(MsgHandlerEventArgs args, HttpContext context, IConnection connection)
        {
            if (args.Message.Reply != null)
            {
                _log.LogTrace($"Create response to reply {args.Message.Reply}");

                if (context.Response.Body.CanSeek)
                {
                    context.Response.Body.Position = 0;
                }

                var response = new HttpResponseMessage
                {
                    StatusCode = (HttpStatusCode)context.Response.StatusCode,
                    Content    = new StreamContent(context.Response.Body),
                };

                response.Content.Headers.ContentType   = MediaTypeHeaderValue.Parse(context.Response.ContentType);
                response.Content.Headers.ContentLength = context.Response.Body.Length;

                foreach (var header in context.Response.Headers)
                {
                    response.Headers.TryAddWithoutValidation(header.Key, header.Value.AsEnumerable());
                }

                var content = new HttpMessageContent(response);
                var bytes   = await content.ReadAsByteArrayAsync();

                _log.LogTrace($"Send response to reply {args.Message.Reply}");
                connection.Publish(args.Message.Reply, bytes);
                _log.LogTrace($"Response to reply {args.Message.Reply} sended");
            }
            else
            {
                _log.LogTrace($"reply is empty, response not be sended. request url {context.Request.GetDisplayUrl()}");
            }
        }
        private static HttpRequestMessage CreateBatch(HttpClient client, string endpoint)
        {
            HttpRequestMessage get1Values = new HttpRequestMessage(HttpMethod.Get, client.BaseAddress + "get1");
            HttpRequestMessage get2Values = new HttpRequestMessage(HttpMethod.Get, client.BaseAddress + "get2");
            HttpRequestMessage get3Values = new HttpRequestMessage(HttpMethod.Get, client.BaseAddress + "get3");


            HttpMessageContent get1Content = new HttpMessageContent(get1Values);
            HttpMessageContent get2Content = new HttpMessageContent(get2Values);
            HttpMessageContent get3Content = new HttpMessageContent(get3Values);

            MultipartContent content = new MultipartContent("mixed", "batch_" + Guid.NewGuid().ToString());

            content.Add(get1Content);
            content.Add(get2Content);
            content.Add(get3Content);

            HttpRequestMessage batchRequest = new HttpRequestMessage(HttpMethod.Post, endpoint);

            //Associate the content with the message
            batchRequest.Content = content;

            return(batchRequest);
        }
Exemple #13
0
        public async Task SerializeAsync(HttpResponseMessage response, Stream stream)
        {
            if (response.Content != null)
            {
                TraceWriter.WriteLine("SerializeAsync - before load",
                                      TraceLevel.Verbose);
                if (_bufferContent)
                {
                    await response.Content.LoadIntoBufferAsync();
                }
                TraceWriter.WriteLine("SerializeAsync - after load", TraceLevel.Verbose);
            }
            else
            {
                TraceWriter.WriteLine("Content NULL - before load",
                                      TraceLevel.Verbose);
            }

            var httpMessageContent = new HttpMessageContent(response);
            var buffer             = await httpMessageContent.ReadAsByteArrayAsync();

            TraceWriter.WriteLine("SerializeAsync - after ReadAsByteArrayAsync", TraceLevel.Verbose);
            stream.Write(buffer, 0, buffer.Length);
        }
Exemple #14
0
 public override void SetContent(HttpMessageContent content)
 => _requestContent = content;
Exemple #15
0
        public static async Task <string> ShowAsync(this HttpRequestMessage message)
        {
            var content = new HttpMessageContent(message);

            return(await content.ReadAsStringAsync());
        }
Exemple #16
0
        private static async Task ExecuteBatchRequest()
        {
            HttpClient client = new HttpClient();

            HttpRequestMessage  request;
            HttpResponseMessage response;

            request  = new HttpRequestMessage(HttpMethod.Get, serviceUrl + "/Customers");
            response = await client.SendAsync(request);

            if (!response.IsSuccessStatusCode)
            {
                Console.WriteLine("Couldn't get the list of Customers");
                return;
            }
            IList <Customer> customers = await response.Content.ReadAsAsync <IList <Customer> >();

            Customer updatedCustomer = customers.First();

            updatedCustomer.Name = "Peter";
            Customer removedCustomer = customers.ElementAt(1);
            Customer addedCustomer   = new Customer {
                Id = 10, Name = "Name " + 10
            };
            JsonMediaTypeFormatter formatter = new JsonMediaTypeFormatter();
            //Create a request to query for customers
            HttpRequestMessage queryCustomersRequest = new HttpRequestMessage(HttpMethod.Get, serviceUrl + "/Customers");
            //Create a message to add a customer
            HttpRequestMessage addCustomerRequest = new HttpRequestMessage(HttpMethod.Post, serviceUrl + "/Customers");

            addCustomerRequest.Content = new ObjectContent <Customer>(addedCustomer, formatter);
            //Create a message to update a customer
            HttpRequestMessage updateCustomerRequest = new HttpRequestMessage(HttpMethod.Put, string.Format(serviceUrl + "/Customers/{0}", updatedCustomer.Id));

            updateCustomerRequest.Content = new ObjectContent <Customer>(updatedCustomer, formatter);
            //Create a message to remove a customer.
            HttpRequestMessage removeCustomerRequest = new HttpRequestMessage(HttpMethod.Delete, string.Format(serviceUrl + "/Customers/{0}", removedCustomer.Id));

            //Create the different parts of the multipart content
            HttpMessageContent queryContent          = new HttpMessageContent(queryCustomersRequest);
            HttpMessageContent addCustomerContent    = new HttpMessageContent(addCustomerRequest);
            HttpMessageContent updateCustomerContent = new HttpMessageContent(updateCustomerRequest);
            HttpMessageContent removeCustomerContent = new HttpMessageContent(removeCustomerRequest);

            //Create the multipart/mixed message content
            MultipartContent content = new MultipartContent("mixed", "batch_" + Guid.NewGuid().ToString());

            content.Add(queryContent);
            content.Add(addCustomerContent);
            content.Add(updateCustomerContent);
            content.Add(removeCustomerContent);

            //Create the request to the batch service
            HttpRequestMessage batchRequest = new HttpRequestMessage(HttpMethod.Post, serviceUrl + "/batch");

            //Associate the content with the message
            batchRequest.Content = content;

            //Send the message
            HttpResponseMessage batchResponse = await client.SendAsync(batchRequest);

            //Check that the batch response is correct
            if (!batchResponse.IsSuccessStatusCode)
            {
                Console.WriteLine("There was an error executing the batch request.");
                Console.WriteLine(await batchResponse.Content.ReadAsStringAsync());
            }
            //Check that the content in the response is multipart/mixed
            if (!batchResponse.Content.IsMimeMultipartContent("mixed"))
            {
                Console.WriteLine("The returned content is not multipart/mixed");
                Console.WriteLine(await batchResponse.Content.ReadAsStringAsync());
            }
            //Reads the individual parts in the content and loads them in memory
            MultipartMemoryStreamProvider responseContents = await batchResponse.Content.ReadAsMultipartAsync();

            if (!(responseContents.Contents.Count == 4))
            {
                Console.WriteLine("There wrong number of responses came back.");
            }

            HttpResponseMessage queryResponse = await responseContents.Contents[0].ReadAsHttpResponseMessageAsync();

            if (!queryResponse.IsSuccessStatusCode || queryResponse.Content == null)
            {
                Console.WriteLine("The query for the customers failed");
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("Query result:");
                Console.WriteLine(await queryResponse.Content.ReadAsStringAsync());
                Console.WriteLine();
            }

            HttpResponseMessage addResponse = await responseContents.Contents[1].ReadAsHttpResponseMessageAsync();

            if (!addResponse.IsSuccessStatusCode || addResponse.Content == null)
            {
                Console.WriteLine("The add customer operation failed");
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("Add result:");
                Console.WriteLine(await addResponse.Content.ReadAsStringAsync());
                Console.WriteLine();
            }

            HttpResponseMessage updateResponse = await responseContents.Contents[2].ReadAsHttpResponseMessageAsync();

            if (!updateResponse.IsSuccessStatusCode || updateResponse.Content == null)
            {
                Console.WriteLine("The update customer operation failed");
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("Update result:");
                Console.WriteLine(await updateResponse.Content.ReadAsStringAsync());
                Console.WriteLine();
            }

            HttpResponseMessage removeResponse = await responseContents.Contents[3].ReadAsHttpResponseMessageAsync();

            if (!removeResponse.IsSuccessStatusCode)
            {
                Console.WriteLine("The delete customer operation failed");
            }
            else
            {
                Console.WriteLine("The delete operation was successful");
            }
        }
Exemple #17
0
        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);
        }
Exemple #18
0
 public override void SetContent(HttpMessageContent content)
 {
     throw new NotImplementedException();
 }
Exemple #19
0
        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 ExecuteTransactionResponse GetTransactionResponse(ExecuteTransactionRequest request)
        {
            var requestDictionary = new Dictionary <int, string>();

            var batchid          = "batch_" + Guid.NewGuid().ToString();
            var batchContent     = new MultipartContent("mixed", batchid);
            var changesetID      = "changeset_" + Guid.NewGuid().ToString();
            var changeSetContent = new MultipartContent("mixed", changesetID);

            for (int contentId = 1; contentId <= request.Requests.Count; contentId++)
            {
                HttpMessageContent content = new HttpMessageContent(GetRequestMessage((OrganizationRequest)request.Requests[contentId - 1]));
                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());
                changeSetContent.Add(content);
                requestDictionary.Add(contentId, ((OrganizationRequest)request.Requests[contentId - 1]).RequestName);
            }

            batchContent.Add(changeSetContent);

            var batchRequest = new HttpRequestMessage(HttpMethod.Post, _endpoint + "$batch")
            {
                Content = batchContent
            };

            var batchstring = batchRequest.Content.ReadAsStringAsync();

            var httpClient = new HttpClient(new HttpClientHandler {
                Credentials = _credential ?? CredentialCache.DefaultNetworkCredentials
            });
            var response = httpClient.SendAsync(batchRequest)?.Result;

            if (response == null)
            {
                throw new Exception("Сервер вернул пустой ответ");
            }

            if (!response.IsSuccessStatusCode)
            {
                var exception = JsonSerializer.Deserialize <CrmException>(response.Content.ReadAsStringAsync().Result);
                throw new CrmException(exception.Error.Message, exception);
            }

            var responseString = response.Content.ReadAsStringAsync();
            MultipartMemoryStreamProvider batchStream = response.Content.ReadAsMultipartAsync().Result;
            var batchStreamContent = batchStream.Contents.FirstOrDefault();

            MultipartMemoryStreamProvider changesetStream = batchStreamContent.ReadAsMultipartAsync().Result;

            var transactionResponse = new ExecuteTransactionResponse();

            foreach (var changesetContent in changesetStream.Contents)
            {
                changesetContent.Headers.Remove("Content-Type");
                changesetContent.Headers.Add("Content-Type", "application/http; msgtype=response");

                var indivdualResponse = changesetContent.ReadAsHttpResponseMessageAsync().Result;

                if (!indivdualResponse.IsSuccessStatusCode)
                {
                    var exception = JsonSerializer.Deserialize <CrmException>(response.Content.ReadAsStringAsync().Result);
                    throw new CrmException(exception.Error.Message, exception);
                }

                var operationName = requestDictionary.FirstOrDefault(dic => dic.Key == int.Parse(changesetContent.Headers.GetValues("Content-ID").FirstOrDefault())).Value;

                if (operationName == Constants.CREATE)
                {
                    var idString = indivdualResponse.Headers.GetValues("OData-EntityId").FirstOrDefault();
                    idString = idString.Replace(_endpoint, "").Replace("(", "").Replace(")", "");
                    idString = idString.Substring(idString.Length - 36);

                    var createResponse = new CreateResponse {
                        Id = Guid.Parse(idString), ResponseName = operationName
                    };
                    transactionResponse.Responses.Add(createResponse);
                }

                if (operationName == Constants.UPDATE)
                {
                    var updateResponse = new UpdateResponse {
                        ResponseName = operationName
                    };
                    transactionResponse.Responses.Add(updateResponse);
                }

                if (operationName == Constants.DELETE)
                {
                    var deleteResponse = new DeleteResponse {
                        ResponseName = operationName
                    };
                    transactionResponse.Responses.Add(deleteResponse);
                }

                if (operationName == Constants.DISASSOCIATE)
                {
                    var deleteResponse = new DissacociateResponse {
                        ResponseName = operationName
                    };
                    transactionResponse.Responses.Add(deleteResponse);
                }

                if (operationName == Constants.ASSOCIATE)
                {
                    var deleteResponse = new AssociateResponse {
                        ResponseName = operationName
                    };
                    transactionResponse.Responses.Add(deleteResponse);
                }
            }

            return(transactionResponse);
        }
Exemple #21
0
        /// <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);
        }
Exemple #22
0
        private static async Task RunSample()
        {
            var accessToken    = ConfigurationManager.AppSettings["AccessToken"];
            var openApiBaseUrl = ConfigurationManager.AppSettings["OpenApiBaseUrl"];

            Console.WriteLine("Started Processing Batch...");

            var batchUrl         = $"{openApiBaseUrl}/ref/batch";
            var getCurrenciesUrl = $"{openApiBaseUrl}/ref/v1/currencies";
            var getTimezonesUrl  = $"{openApiBaseUrl}/ref/v1/timezones";

            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("Authorization", $"BEARER {accessToken}");
                var content = new MultipartContent("mixed", $"batch_{Guid.NewGuid()}");

                using (var batchRequest = new HttpRequestMessage(HttpMethod.Post, new Uri(batchUrl)))
                {
                    // Prepare contents of Urls to be batched
                    var getQuery1Content = new HttpMessageContent(new HttpRequestMessage(HttpMethod.Get, new Uri(getCurrenciesUrl)));
                    var getQuery2Content = new HttpMessageContent(new HttpRequestMessage(HttpMethod.Get, new Uri(getTimezonesUrl)));

                    content.Add(getQuery1Content);
                    content.Add(getQuery2Content);

                    // Add both the Get Request to Batch Request content
                    batchRequest.Content = content;

                    //**************************************************************************
                    // Example format of batch request from Fiddler
                    //**************************************************************************

                    // POST http://.../../../.. HTTP/1.1
                    // Authorization: BEARER eyJhbG.....
                    // Content - Type: multipart / mixed; boundary = "batch_b94ad337-036a-423d-8524-ec47b0ce857d"
                    // Host: ......
                    // Content - Length: 385
                    // Expect: 100 -continue
                    // Connection: Keep - Alive

                    // --batch_b94ad337 - 036a - 423d - 8524 - ec47b0ce857d
                    // Content - Type: application / http; msgtype = request

                    // GET / openapi /ref/ v1 / currencies HTTP / 1.1
                    // Host: ......

                    // --batch_b94ad337 - 036a - 423d - 8524 - ec47b0ce857d
                    // Content - Type: application / http; msgtype = request

                    // GET / openapi /ref/ v1 / timezones HTTP / 1.1
                    // Host: ........

                    // --batch_b94ad337 - 036a - 423d - 8524 - ec47b0ce857d--

                    var response = await client.SendAsync(batchRequest).ConfigureAwait(false);

                    var responseContents = await response.Content.ReadAsMultipartAsync().ConfigureAwait(false);

                    // Read Responses
                    foreach (var responseContent in responseContents.Contents)
                    {
                        var result = await responseContent.ReadAsHttpResponseMessageAsync().ConfigureAwait(false);

                        var resultString = await result.Content.ReadAsStringAsync().ConfigureAwait(false);

                        Console.WriteLine($"Response String of Request : {resultString}");
                        Console.WriteLine("");
                    }
                }

                //**************************************************************************
                // Example format of response from Fiddler
                //**************************************************************************

                // HTTP / 1.1 200 OK
                // Cache - Control: no - cache
                // Pragma: no - cache
                // Content - Length: 7827
                // Content - Type: multipart / mixed; boundary = "3810bf29-89fa-4f44-82a4-f02ba887de39"
                // Expires: -1
                // Server: Microsoft - IIS / 8.5
                // X - Correlation: f613d399702f4dcb9a54cd2b157019c9#123#d0970a69-9872-41ba-8ed0-d6c4b1e8075c#31
                // X-Remote - Host: ****
                // Date: Fri, 14 Oct 2016 09:40:02 GMT

                //-3810bf29 - 89fa - 4f44 - 82a4 - f02ba887de39
                // Content - Type: application / http; msgtype = response

                // HTTP / 1.1 200 OK
                // X - Correlation: f613d399702f4dcb9a54cd2b157019c9#123#d0970a69-9872-41ba-8ed0-d6c4b1e8075c#31
                // Content-Type: application / json; charset = utf - 8

                // { "Data":[{"CurrencyCode":"USD","Decimals":2,"Name":"US Dollar"},{"CurrencyCode":"GBP","Decimals":2,"Name":"British Pound"},{"CurrencyCode":"EUR","Decimals":2,"Name":"Euro"},{"CurrencyCode":"CHF","Decimals":2,"Name":"Swiss Franc"},{"CurrencyCode":"AUD","Decimals":2,"Name":"Australian Dollar"},{"CurrencyCode":"CAD","Decimals":2,"Name":"Canadian Dollar"},{"CurrencyCode":"NZD","Decimals":2,"Name":"New Zealand Dollar"},{"CurrencyCode":"JPY","Decimals":0,"Name":"Japanese Yen"},{"CurrencyCode":"DKK","Decimals":2,"Name":"Danish Krone"},{"CurrencyCode":"SEK","Decimals":2,"Name":"Swedish Krona"},{"CurrencyCode":"NOK","Decimals":2,"Name":"Norwegian Krone"},{"CurrencyCode":"ATS","Decimals":2,"Name":"Austrian Schilling"},{"CurrencyCode":"BEF","Decimals":2,"Name":"Belgian Franc"},{"CurrencyCode":"DEM","Decimals":2,"Name":"German Mark"},{"CurrencyCode":"ESP","Decimals":2,"Name":"Spanish Peseta"},{"CurrencyCode":"FIM","Decimals":2,"Name":"Finnish Mark"},{"CurrencyCode":"FRF","Decimals":2,"Name":"French Franc"},{"CurrencyCode":"GRD","Decimals":2,"Name":"Greek Drachma"},{"CurrencyCode":"IEP","Decimals":2,"Name":"Irish Punt"},{"CurrencyCode":"ITL","Decimals":2,"Name":"Italian Lira"},{"CurrencyCode":"LUF","Decimals":2,"Name":"Luxembourg Franc"},{"CurrencyCode":"NLG","Decimals":2,"Name":"Dutch Guilder"},{"CurrencyCode":"PTE","Decimals":2,"Name":"Portugese Escudo"},{"CurrencyCode":"CZK","Decimals":2,"Name":"Czech Koruna"},{"CurrencyCode":"ISK","Decimals":2,"Name":"Iceland Krona"},{"CurrencyCode":"PLN","Decimals":2,"Name":"Polish Zloty"},{"CurrencyCode":"SGD","Decimals":2,"Name":"Singapore Dollar"},{"CurrencyCode":"LTL","Decimals":2,"Name":"Lithuanian Litas"},{"CurrencyCode":"EEK","Decimals":2,"Name":"Estonian Kroon"},{"CurrencyCode":"HRK","Decimals":2,"Name":"Croatian Kuna"},{"CurrencyCode":"LVL","Decimals":2,"Name":"Latvian Lats"},{"CurrencyCode":"SIT","Decimals":2,"Name":"Slovenian Tolar"},{"CurrencyCode":"SKK","Decimals":2,"Name":"Slovak Koruna"},{"CurrencyCode":"AED","Decimals":2,"Name":"UAE Dirham"},{"CurrencyCode":"BHD","Decimals":2,"Name":"Bahrain Dinar"},{"CurrencyCode":"BRL","Decimals":2,"Name":"Brazilian Real"}]}
                // --3810bf29-89fa-4f44-82a4-f02ba887de39
                // Content-Type: application/http; msgtype=response

                // HTTP/1.1 200 OK
                // X-Correlation: f613d399702f4dcb9a54cd2b157019c9#123#d0970a69-9872-41ba-8ed0-d6c4b1e8075c#31
                // Content-Type: application/json; charset=utf-8

                // {"Data":[{"DisplayName":"GMT a.k.a. UTC","TimeZoneId":0,"ZoneName":"Etc/UTC"},{"DisplayName":"British Time","TimeZoneId":1,"ZoneName":"Europe/London"},{"DisplayName":"Singapore Time","TimeZoneId":2,"ZoneName":"Asia/Singapore"},{"DisplayName":"US Eastern Time","TimeZoneId":3,"ZoneName":"America/New_York"},{"DisplayName":"Central European Time","TimeZoneId":4,"ZoneName":"Europe/Paris"},{"DisplayName":"US Central Time","TimeZoneId":5,"ZoneName":"America/Chicago"},{"DisplayName":"US Pacific Time","TimeZoneId":6,"ZoneName":"America/Los_Angeles"},{"DisplayName":"Hong Kong Time","TimeZoneId":7,"ZoneName":"Asia/Hong_Kong"},{"DisplayName":"Sydney Time","TimeZoneId":8,"ZoneName":"Australia/Sydney"},{"DisplayName":"New Zealand Time","TimeZoneId":9,"ZoneName":"Pacific/Auckland"},{"DisplayName":"GMT +9 No Daylight S.","TimeZoneId":10,"ZoneName":"Etc/GMT-9"},{"DisplayName":"GMT +7 No Daylight S.","TimeZoneId":11,"ZoneName":"Etc/GMT-7"},{"DisplayName":"Russia Zone 2","TimeZoneId":12,"ZoneName":"Europe/Moscow"},{"DisplayName":"GMT +8 No Daylight S.","TimeZoneId":13,"ZoneName":"Etc/GMT-8"},{"DisplayName":"Eastern European Time","TimeZoneId":14,"ZoneName":"Europe/Helsinki"},{"DisplayName":"Hawaii Time","TimeZoneId":15,"ZoneName":"Pacific/Honolulu"},{"DisplayName":"South African Time","TimeZoneId":16,"ZoneName":"Africa/Johannesburg"},{"DisplayName":"GMT +10 No Daylight S.","TimeZoneId":17,"ZoneName":"Etc/GMT-10"},{"DisplayName":"GMT+3","TimeZoneId":18,"ZoneName":"Etc/GMT-3"},{"DisplayName":"GMT+4","TimeZoneId":19,"ZoneName":"Etc/GMT-4"},{"DisplayName":"Brazil Sao Paulo","TimeZoneId":20,"ZoneName":"America/Sao_Paulo"},{"DisplayName":"Africa/Cairo","TimeZoneId":33,"ZoneName":"Africa/Cairo"},{"DisplayName":"America/Caracas","TimeZoneId":104,"ZoneName":"America/Caracas"},{"DisplayName":"America/Halifax","TimeZoneId":130,"ZoneName":"America/Halifax"},{"DisplayName":"America/La_Paz","TimeZoneId":147,"ZoneName":"America/La_Paz"},{"DisplayName":"Asia/Kolkata","TimeZoneId":256,"ZoneName":"Asia/Kolkata"},{"DisplayName":"Atlantic/Azores","TimeZoneId":298,"ZoneName":"Atlantic/Azores"}]}
                // --3810bf29-89fa-4f44-82a4-f02ba887de39--
            }
        }
Exemple #23
0
        private async Task <HttpRequestMessage> ToHttpRequest(OrganizationRequest orgRequest)
        {
            switch (orgRequest)
            {
            case ExecuteMultipleRequest request:
            {
                var message = new HttpRequestMessage(HttpMethod.Post, $"{_baseUrl}/$batch");

                var batchId = $"batch_{Guid.NewGuid().ToString("N")}";
                var batch   = new MultipartContent("mixed", batchId);

                foreach (var req in request.Requests)
                {
                    var changesetId = $"changeset_{Guid.NewGuid().ToString("N")}";
                    var changeset   = new MultipartContent("mixed", changesetId);
                    changeset.Headers.Remove("Content-Type");
                    changeset.Headers.Add("Content-Type", $"multipart/mixed;boundary={changesetId}");

                    var change = await ToHttpRequest(req);

                    var content = new HttpMessageContent(change);
                    content.Headers.Remove("Content-Type");
                    content.Headers.Add("Content-Type", $"application/http");
                    content.Headers.Add("Content-Transfer-Encoding", "binary");
                    content.Headers.Add("Content-ID", "1");
                    changeset.Add(content);
                    batch.Add(changeset);
                }
                message.Content = batch;
                return(message);
            }

            case CreateRequest request:
            {
                var target   = request.Target;
                var metadata = await GetEntityMetadata(target.LogicalName);

                var entitySetName = metadata.LogicalCollectionName;
                var message       = new HttpRequestMessage(HttpMethod.Post, $"{_baseUrl}/{entitySetName}");

                var body = JsonConvert.SerializeObject(request.Target.Attributes.ToDictionary(x => x.Key, x => x.Value));
                message.Content = new StringContent(body, Encoding.UTF8, "application/json");
                return(message);
            }

            case RetrieveRequest request:
            {
                var target   = request.Target;
                var metadata = await GetEntityMetadata(target.LogicalName);

                var entity = metadata.LogicalCollectionName;
                var query  = new QueryExpression(target.LogicalName)
                {
                    ColumnSet = request.ColumnSet
                };

                var visitor = new ODataQueryExpressionVisitor();
                visitor.Visit(query);

                return(new HttpRequestMessage(HttpMethod.Get, $"{_baseUrl}/{entity}({target.Id})?{visitor.QueryString}"));
            }

            case RetrieveMultipleRequest request:
            {
                return(await QueryToHttpRequest(request.Query));
            }

            case DeleteRequest request:
            {
                var target   = request.Target;
                var metadata = await GetEntityMetadata(target.LogicalName);

                var entity = metadata.LogicalCollectionName;
                return(new HttpRequestMessage(HttpMethod.Delete, $"{_baseUrl}/{entity}({target.Id})"));
            }
            }
            return(null);
        }
Exemple #24
0
        public async Task Batch()
        {
            Skip.IfNot(RuntimeInformation.IsOSPlatform(OSPlatform.Windows));

            var batch     = new MultipartContent("mixed", "batch" + Guid.NewGuid());
            var changeset = new MultipartContent("mixed", "changeset" + Guid.NewGuid());

            {
                var message = new HttpRequestMessage(HttpMethod.Post, "http://localhost/sample/SalesPatterns")
                {
                    Content = Helpers.JSON(new
                    {
                        TaxRoundMode = "Round",
                        TaxRate      = 0.08m
                    })
                };
                message.Headers.Add("Content-ID", "1");

                var content = new HttpMessageContent(message);
                content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/http");
                content.Headers.Add("Content-Transfer-Encoding", "binary");

                changeset.Add(content);
            }

            {
                var message = new HttpRequestMessage(HttpMethod.Post, "http://localhost/sample/$1/Products")
                {
                    Content = Helpers.JSON(new
                    {
                        Name      = "Sample Batch",
                        UnitPrice = 9.99m
                    })
                };
                message.Headers.Add("Content-ID", "2");

                var content = new HttpMessageContent(message);
                content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/http");
                content.Headers.Add("Content-Transfer-Encoding", "binary");

                changeset.Add(content);
            }

            batch.Add(changeset);
            var response = await _client.PostAsync("/sample/$batch", batch);

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            Assert.Equal("multipart/mixed", response.Content.Headers.ContentType.MediaType);

            var parts = await Helpers.ParseMultipartMixedAsync(response);

            Assert.Equal(2, parts.Length);

            Assert.Equal(HttpStatusCode.Created, parts[0].StatusCode);
            var salesPattern = await parts[0].Content.ReadAsAsync <dynamic>();

            Assert.True((int)salesPattern.Id > 0);
            Assert.Equal("Round", (string)salesPattern.TaxRoundMode);

            Assert.Equal(HttpStatusCode.Created, parts[1].StatusCode);
            var product = await parts[1].Content.ReadAsAsync <dynamic>();

            Assert.True((long)product.Id > 0);
            Assert.Equal("Sample Batch", (string)product.Name);
            Assert.Equal((int)salesPattern.Id, (int)product.SalesPatternId);
        }
Exemple #25
0
 public CrmTransmission(int index, HttpMessageContent requestContent, JObject responseObject)
 {
     this.Index           = index;
     this.RequestContent  = requestContent;
     this.ResponseJObject = responseObject;
 }
Exemple #26
0
        /// <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();

            var httpClient = new HttpClient();

            httpClient.DefaultRequestHeaders.Authorization =
                new AuthenticationHeaderValue("Bearer", _crmWebAPIConfig.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, _crmWebAPIConfig.APIUrl + entityCollection);

                req.Content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json");
                req.Version = new Version(major: 1, minor: 1);

                var 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, _crmWebAPIConfig.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(_crmWebAPIConfig.APIUrl + entityCollection, "").Replace("(", "").Replace(")", "");
                CRMBatchResultItem resultItem = new CRMBatchResultItem();
                resultItem.EntityID = Guid.Parse(idString);
                finalResult.ResultItems.Add(resultItem);
            }

            return(finalResult);
        }
        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));
        }