Esempio n. 1
0
        private async Task <HttpRequestMessage> QueryToHttpRequest(QueryBase queryBase)
        {
            switch (queryBase)
            {
            case FetchExpression query:
            {
                using (var input = new StringReader(query.Query))
                    using (var reader = XmlReader.Create(input))
                    {
                        reader.ReadToFollowing("entity");
                        var entityName = reader.GetAttribute("name");

                        var metadata = await GetEntityMetadata(entityName);

                        var entity = metadata.LogicalCollectionName;

                        return(new HttpRequestMessage(HttpMethod.Get, $"{_baseUrl}/{entity}?fetchXml={query.Query}"));
                    }
            }

            case QueryExpression query:
            {
                var metadata = await GetEntityMetadata(query.EntityName);

                var entity = metadata.LogicalCollectionName;

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

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

            case QueryByAttribute query:
            {
                var queryExpression = new QueryExpression(query.EntityName)
                {
                    TopCount  = query.TopCount,
                    PageInfo  = query.PageInfo,
                    ColumnSet = query.ColumnSet
                };
                queryExpression.Orders.AddRange(query.Orders);

                for (var i = 0; i < query.Attributes.Count; i++)
                {
                    var attribute = query.Attributes[i];
                    var value     = query.Values[i];
                    queryExpression.Criteria.AddCondition(attribute, ConditionOperator.Equal, value);
                }

                return(await QueryToHttpRequest(queryExpression));
            }

            default:
            {
                throw new NotSupportedException("Query type is not supported.");
            }
            }
        }
Esempio n. 2
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);
        }