Example #1
0
 /// <param name='operations'>
 /// Reference to the ZtherApiIntegration.API.IProductReviews.
 /// </param>
 /// <param name='code'>
 /// Required.
 /// </param>
 /// <param name='model'>
 /// Required.
 /// </param>
 public static string Create(this IProductReviews operations, string code, ProductReviewCreate model)
 {
     return(Task.Factory.StartNew((object s) =>
     {
         return ((IProductReviews)s).CreateAsync(code, model);
     }
                                  , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult());
 }
        public static bool Create(ReviewItemModel reviewItemModel)
        {
            using (var client = new LandauPortalWebAPI())
            {
                var productReview = new ProductReviewCreate();

                productReview.Comments = reviewItemModel.Comments;

                Char[]    whitespace = new char[] { ' ', '\t' };
                String [] names      = reviewItemModel.CommentatorName.Split(null);

                if (names.Count() > 0)
                {
                    if (names.Count() == 1)
                    {
                        productReview.FirstName = names[0].Trim();
                        productReview.LastName  = names[0].Trim();
                    }
                    else if (names.Count() == 2 || names.Count() > 2)
                    {
                        productReview.FirstName = names[0].Trim();
                        productReview.LastName  = reviewItemModel.CommentatorName.Replace(names[0], "").Trim();
                    }
                }
                productReview.ProductCode = reviewItemModel.ProductCode;
                productReview.Rating      = Convert.ToInt16(reviewItemModel.Rating);
                /// Landau: Added Title, Location and Email.
                productReview.Title    = reviewItemModel.Title;
                productReview.Location = reviewItemModel.Location;
                productReview.Email    = reviewItemModel.Email;

                try
                {
                    client.ProductReviews.Create(productReview.ProductCode, productReview);
                    return(true);
                }
                catch (Exception e) {
                    if (e is HttpOperationException)
                    {
                        var httpEx = (HttpOperationException)e;
                        return(httpEx.Response.IsSuccessStatusCode);
                    }
                }
                return(false);
            }
        }
Example #3
0
        /// <param name='operations'>
        /// Reference to the ZtherApiIntegration.API.IProductReviews.
        /// </param>
        /// <param name='code'>
        /// Required.
        /// </param>
        /// <param name='model'>
        /// Required.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        public static async Task <string> CreateAsync(this IProductReviews operations, string code, ProductReviewCreate model, CancellationToken cancellationToken = default(System.Threading.CancellationToken))
        {
            Microsoft.Rest.HttpOperationResponse <string> result = await operations.CreateWithOperationResponseAsync(code, model, cancellationToken).ConfigureAwait(false);

            return(result.Body);
        }
Example #4
0
        /// <param name='code'>
        /// Required.
        /// </param>
        /// <param name='model'>
        /// Required.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        public async Task <HttpOperationResponse <string> > CreateWithOperationResponseAsync(string code, ProductReviewCreate model, CancellationToken cancellationToken = default(System.Threading.CancellationToken))
        {
            // Validate
            if (code == null)
            {
                throw new ArgumentNullException("code");
            }
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }

            // Tracing
            bool   shouldTrace  = ServiceClientTracing.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = ServiceClientTracing.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("code", code);
                tracingParameters.Add("model", model);
                ServiceClientTracing.Enter(invocationId, this, "CreateAsync", tracingParameters);
            }

            // Construct URL
            string url = "";

            url = url + "/products/";
            url = url + Uri.EscapeDataString(code);
            url = url + "/reviews";
            string baseUrl = this.Client.BaseUri.AbsoluteUri;

            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");

            // Create HTTP transport objects
            HttpRequestMessage httpRequest = new HttpRequestMessage();

            httpRequest.Method     = HttpMethod.Post;
            httpRequest.RequestUri = new Uri(url);

            // Set Headers

            // Set Credentials
            if (this.Client.Credentials != null)
            {
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);
            }

            // Serialize Request
            string requestContent = null;
            JToken requestDoc     = model.SerializeJson(null);

            requestContent      = requestDoc.ToString(Newtonsoft.Json.Formatting.Indented);
            httpRequest.Content = new StringContent(requestContent, Encoding.UTF8);
            httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");

            // Send Request
            if (shouldTrace)
            {
                ServiceClientTracing.SendRequest(invocationId, httpRequest);
            }
            cancellationToken.ThrowIfCancellationRequested();
            HttpResponseMessage httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);

            if (shouldTrace)
            {
                ServiceClientTracing.ReceiveResponse(invocationId, httpResponse);
            }
            HttpStatusCode statusCode = httpResponse.StatusCode;

            cancellationToken.ThrowIfCancellationRequested();
            string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

            if (statusCode != HttpStatusCode.OK)
            {
                HttpOperationException <object> ex = new HttpOperationException <object>();
                ex.Request  = httpRequest;
                ex.Response = httpResponse;
                ex.Body     = null;
                if (shouldTrace)
                {
                    ServiceClientTracing.Error(invocationId, ex);
                }
                throw ex;
            }

            // Create Result
            HttpOperationResponse <string> result = new HttpOperationResponse <string>();

            result.Request  = httpRequest;
            result.Response = httpResponse;

            // Deserialize Response
            if (statusCode == HttpStatusCode.OK)
            {
                string resultModel = default(string);
                JToken responseDoc = null;
                if (string.IsNullOrEmpty(responseContent) == false)
                {
                    responseDoc = JToken.Parse(responseContent);
                }
                if (responseDoc != null)
                {
                    resultModel = responseDoc.ToString(Newtonsoft.Json.Formatting.Indented);
                }
                result.Body = resultModel;
            }

            if (shouldTrace)
            {
                ServiceClientTracing.Exit(invocationId, result);
            }
            return(result);
        }