Exemplo n.º 1
0
        /// <inheritdoc/>
        public virtual Task <Entity> RetrieveAsync(string entityName, Guid id, [AllowNull] QueryOptions options = null,
                                                   CancellationToken cancellationToken = default)
        {
            var requestId = Guid.NewGuid();

            var queryString = (options ?? new QueryOptions()).BuildQueryString(WebApiMetadata, entityName);

            var entityMd = WebApiMetadata.GetEntityMetadata(entityName);

            var request = $"{entityMd.EntitySetName}({id}){queryString}";

            return(GetAsync <Entity>(request, requestId, cancellationToken));
        }
Exemplo n.º 2
0
        public virtual Task <EntityCollection> RetrieveMultipleAsync(FetchXmlExpression fetchXml,
                                                                     CancellationToken cancellationToken = default)
        {
            if (fetchXml is null)
            {
                throw new ArgumentNullException(nameof(fetchXml));
            }

            var entityMetadata = WebApiMetadata.GetEntityMetadata(fetchXml.EntityName);

            var query = $"{entityMetadata.EntitySetName}?fetchXml={System.Net.WebUtility.UrlEncode(fetchXml)}";

            return(ExecuteFunctionAsync <EntityCollection>(query, cancellationToken));
        }
Exemplo n.º 3
0
        public virtual Task <EntityCollection> RetrieveMultipleAsync(string entityName,
                                                                     [AllowNull] QueryOptions options    = null,
                                                                     CancellationToken cancellationToken = default)
        {
            _logger.LogInformation($"Start RetrieveMultipleAsync at {entityName}");

            var queryString = (options ?? new QueryOptions())
                              .BuildQueryString(WebApiMetadata, entityName);

            var entityMd = WebApiMetadata.GetEntityMetadata(entityName);

            var request = $"{entityMd.EntitySetName}{queryString}";

            return(ExecuteFunctionAsync <EntityCollection>(request, cancellationToken));
        }
Exemplo n.º 4
0
        public virtual async Task <Guid> CreateAsync(Entity entity)
        {
            if (entity is null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            var collectionName = WebApiMetadata.GetEntitySetName(entity.LogicalName);
            var json           = JsonConvert.SerializeObject(entity, SerializerSettings);

            using var httpRequest = new HttpRequestMessage(HttpMethod.Post, $"{collectionName}")
                  {
                      Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json")
                  };

            using var httpResponse = await SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead, default)
                                     .ConfigureAwait(false);

            httpRequest.Dispose();

            Guid entityId = default;

            if (httpResponse.StatusCode == System.Net.HttpStatusCode.NoContent)
            {
                if (httpResponse.Headers.TryGetValues("OData-EntityId", out var headersRaw))
                {
                    var headers = headersRaw as string[] ?? headersRaw.ToArray();

                    var rawValue = headers.First().Split("(").Last().Split(")").First();
                    if (!Guid.TryParse(rawValue, out entityId))
                    {
                        throw new WebApiException(
                                  $"Error parsing response header 'OData-EntityId'. Value: {headers.First()}");
                    }
                }
                else
                {
                    throw new WebApiException("Response header 'OData-EntityId' not present.");
                }
            }
            else
            {
                ODataResponseReader.EnsureSuccessStatusCode(httpResponse, _logger);
            }

            return(entityId);
        }
Exemplo n.º 5
0
        /// <inheritdoc/>
        public virtual Task <EntityCollection> RetrieveMultipleAsync(string entityName,
                                                                     [AllowNull] QueryOptions options    = null,
                                                                     CancellationToken cancellationToken = default)
        {
            _logger.LogDebug("Starting {WebApiOperationName} at {TargetEntity}",
                             "RetrieveMultiple", entityName);

            var requestId = Guid.NewGuid();

            var queryString = (options ?? new QueryOptions())
                              .BuildQueryString(WebApiMetadata, entityName);

            var entityMd = WebApiMetadata.GetEntityMetadata(entityName);

            var request = $"{entityMd.EntitySetName}{queryString}";

            return(GetAsync <EntityCollection>(request, requestId, cancellationToken));
        }
Exemplo n.º 6
0
        public virtual Task <Entity> RetrieveAsync(string entityName, Guid id, [AllowNull] QueryOptions options = null,
                                                   CancellationToken cancellationToken = default)
        {
            _logger.LogInformation($"Start RetrieveAsync at {entityName} with id = {id}");

            var queryString = (options ?? new QueryOptions())
                              .BuildQueryString(WebApiMetadata, entityName);

            var entityMd = WebApiMetadata.GetEntityMetadata(entityName);

            var request = $"{entityMd.EntitySetName}({id}){queryString}";


            var entity = ExecuteFunctionAsync <Entity>(request, cancellationToken);

            _logger.LogInformation($"Finish RetrieveAsync at {entityName} with id = {id}");

            return(entity);
        }
Exemplo n.º 7
0
        public virtual async Task <Guid> CreateAsync(Entity entity)
        {
            if (entity is null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            if (string.IsNullOrEmpty(entity.LogicalName))
            {
                throw new ArgumentException("Entity Logical name cannot be empty.");
            }

            var watch = Stopwatch.StartNew();

            _logger.LogDebug("Starting {WebApiOperationName} {TargetEntity}", "CREATE", entity.LogicalName);

            var collectionName = WebApiMetadata.GetEntitySetName(entity.LogicalName);
            var json           = JsonConvert.SerializeObject(entity, SerializerSettings);

            using var httpRequest = new HttpRequestMessage(HttpMethod.Post, $"{collectionName}")
                  {
                      Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json")
                  };

            using var httpResponse =
                      await SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead, default)
                      .ConfigureAwait(false);

            httpRequest.Dispose();

            Guid entityId = default;

            if (httpResponse.StatusCode == System.Net.HttpStatusCode.NoContent)
            {
                if (httpResponse.Headers.TryGetValues("OData-EntityId", out var headersRaw))
                {
                    var headers = headersRaw as string[] ?? headersRaw.ToArray();

                    var rawValue = headers.First().Split("(").Last().Split(")").First();

                    if (!Guid.TryParse(rawValue, out entityId))
                    {
                        throw new WebApiException(
                                  $"Error parsing response header 'OData-EntityId'. Value: {headers.First()}");
                    }
                }
                else
                {
                    _logger.LogError("Response header 'OData-EntityId' not present.");
                    throw new WebApiException("Response header 'OData-EntityId' not present.");
                }
            }
            else
            {
                ODataResponseReader.EnsureSuccessStatusCode(httpResponse, _logger);
            }

            watch.Stop();
            _logger.LogInformation("Complete {WebApiOperationName} {TargetEntity} in {Elapsed:0.0}ms - {EntityId}",
                                   "CREATE", entity.LogicalName, watch.Elapsed.TotalMilliseconds, entityId);

            return(entityId);
        }