コード例 #1
0
        /// <summary>
        /// Gets the response.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query">The query.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">query</exception>
        public override T GetResponse <T>(IBaseQuery query)
        {
            if (query == null)
            {
                throw new ArgumentNullException("query");
            }

            // build the query
            var uri = query.BuildUri(HostName);

            HttpWebRequest request;

            switch (query.QueryType)
            {
            case SitecoreQueryType.Create:
            case SitecoreQueryType.Update:
                request = CreateRequest(uri, query.QueryType,
                                        ((ISitecoreQuery)query).FieldsToUpdate.ToQueryString());
                break;

            default:
                request = CreateRequest(uri, query.QueryType);
                break;
            }

            // send the request
            return(Get(request, query.ResponseFormat, new T()));
        }
コード例 #2
0
        /// <summary>
        /// Gets the response.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query">The query.</param>
        /// <returns></returns>
        public virtual T GetResponse <T>(IBaseQuery query) where T : class, IBaseResponse, new()
        {
            if (query == null)
            {
                throw new ArgumentNullException("query");
            }

            if (query.QueryType == SitecoreQueryType.Create || query.QueryType == SitecoreQueryType.Update)
            {
                throw new InvalidOperationException("A create or update query must be used with an authenticated data context");
            }

            // build the query
            var uri     = query.BuildUri(_hostName);
            var request = CreateRequest(uri, query.QueryType);

            // send the request
            return(Get(request, query.ResponseFormat, new T()));
        }
        /// <summary>
        /// Gets the response.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query">The query.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">query</exception>
        public override T GetResponse <T>(IBaseQuery query)
        {
            if (query == null)
            {
                throw new ArgumentNullException("query");
            }
            // build the query
            var            uri = query.BuildUri(HostName);
            HttpWebRequest request = null; T response = null;

            contentType = string.Empty;
            string formDataBoundary = string.Empty;
            Dictionary <string, object> postParameters = null;

            byte[] formData = null;
            formDataBoundary = String.Format("----------{0:N}", Guid.NewGuid());
            contentType      = Mindtree.ItemWebApi.Pipelines.Configuration.Settings.ContentTypeMultipleFormData + " boundary=" + formDataBoundary;
            try
            {
                switch (query.QueryType)
                {
                case SitecoreQueryType.AdvanceCreate:
                    string loadOptions   = ((ISitecoreAdvanceQuery)query).loadOptions;
                    string syncItem      = ((ISitecoreAdvanceQuery)query).syncItem;
                    string encryptionKey = ((ISitecoreAdvanceQuery)query).EncryptionKey;
                    // Generate post objects
                    postParameters = FillMedia(query);
                    if (postParameters == null)
                    {
                        postParameters = new Dictionary <string, object>();
                    }
                    postParameters.Add(Mindtree.ItemWebApi.Pipelines.Configuration.Settings.loadOptions, loadOptions);
                    postParameters.Add(Mindtree.ItemWebApi.Pipelines.Configuration.Settings.syncItem, syncItem);
                    postParameters.Add(Mindtree.ItemWebApi.Pipelines.Configuration.Settings.enKey, encryptionKey);
                    formData = FormUpload.GetMultipartFormDataOne(postParameters, formDataBoundary);
                    request  = CreateRequest(uri, query.QueryType, formData, contentType);
                    break;

                case SitecoreQueryType.Create:
                case SitecoreQueryType.Update:
                case SitecoreQueryType.CreateVersion:
                    postParameters = FillMedia(query);
                    if (postParameters == null)
                    {
                        postParameters = new Dictionary <string, object>();
                    }
                    foreach (var item in ((ISitecoreQuery)query).FieldsToUpdate)
                    {
                        postParameters.Add(item.Key, item.Value);
                    }
                    formData = FormUpload.GetMultipartFormData(postParameters, formDataBoundary);
                    request  = CreateRequest(uri, query.QueryType, formData, contentType);
                    break;

                default:
                    //FileStream fs = null;
                    request             = CreateRequest(uri, query.QueryType);
                    request.ContentType = Mindtree.ItemWebApi.Pipelines.Configuration.Settings.ContentTypeFormData;
                    break;
                }
                // send the request
                if (request != null)
                {
                    response = Get(request, query.ResponseFormat, new T());
                }
            }
            catch (Exception ex)
            { Log.WriteError(ex.Message, ex); }
            finally { formData = null; request = null; formDataBoundary = null; contentType = null; }
            // return the response
            return(response);
        }