Example #1
0
        public async Task <Guid> CreateAsync(Entity entity)
        {
            string  fullUrl = ApiUrl + WebApiMetadata.GetEntitySetName(entity.LogicalName);
            JObject jObject = RequestEntityParser.EntityToJObject(entity, WebApiMetadata);
            var     request = new HttpRequestMessage(new HttpMethod("Post"), fullUrl)
            {
                Content = new StringContent(JsonConvert.SerializeObject(jObject), Encoding.UTF8, "application/json")
            };

            HttpResponseMessage response = await Authorization.GetHttpClient().SendAsync(request);

            ResponseValidator.EnsureSuccessStatusCode(response);

            return(GetEntityIdFromResponse(fullUrl, response));
        }
        internal override string GetBatchString(BatchRequest batchRequest, WebApi webApi, int?lastContentId)
        {
            var entityUrl    = lastContentId != null ? "$" + lastContentId.ToString() : webApi.ApiUrl + RequestEntityParser.GetEntityApiUrl(EntityToCreate, webApi.WebApiMetadata);
            var jObject      = RequestEntityParser.EntityToJObject(EntityToCreate, webApi.WebApiMetadata);
            var entityString = JsonConvert.SerializeObject(jObject);

            var batchString = $"--changeset_{batchRequest.ChangeSetId.ToString("N")}" + NewLine;

            batchString += $"Content-Type: application/http" + NewLine;
            batchString += $"Content-Transfer-Encoding:binary" + NewLine;
            batchString += $"Content-ID: {batchRequest.ContentId}" + NewLine + NewLine;
            batchString += $"POST {entityUrl} HTTP/1.1" + NewLine;
            batchString += $"Content-Type: application/json;type=entry" + NewLine + NewLine;
            batchString += entityString + NewLine + NewLine;
            return(batchString);
        }
Example #3
0
        public async Task UpsertAsync(Entity entity, UpsertOptions upsertOptions = UpsertOptions.None)
        {
            string  fullUrl = ApiUrl + RequestEntityParser.GetEntityApiUrl(entity, WebApiMetadata);
            JObject jObject = RequestEntityParser.EntityToJObject(entity, WebApiMetadata);
            var     request = new HttpRequestMessage(new HttpMethod("PATCH"), fullUrl)
            {
                Content = new StringContent(JsonConvert.SerializeObject(jObject), Encoding.UTF8, "application/json")
            };

            if (upsertOptions == UpsertOptions.OnlyUpdate)
            {
                request.Headers.Add("If-Match", "*");
            }

            if (upsertOptions == UpsertOptions.OnlyCreate)
            {
                request.Headers.Add("If-None-Match", "*");
            }

            HttpResponseMessage response = await Authorization.GetHttpClient().SendAsync(request);

            ResponseValidator.EnsureSuccessStatusCode(response);
        }