Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="strategy"></param>
        /// <param name="dp"></param>
        public void InitializeD(string strategy, params object[] dp)
        {
            //StrategyValidationCheck(strategy, dp);

            string querystr = "";

            if (strategy == "SeeParams_Single")
            {
                if (dp != null && dp.Length > 0)
                {
                    //resolve querystring
                    var attrs = GetType().GetCustomAttributes(typeof(QueryStringProvider), false);
                    if (attrs != null && attrs.Length > 0)
                    {
                        querystr    = QueryStringResolver.GetQueryString(((QueryStringProvider)attrs[0]).ProviderName, Config["destination"]["querystring"].ToString(), dp);
                        Destination = destinationq.GetData(querystr);
                    }
                    else
                    {
                        //throw error
                    }
                }
            }
            //other strategy
            else
            {
                //no params
                Destination = destinationq.GetData("");
            }

            if (Destination == null)
            {
                // add log and throw exception
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="ds"></param>
        /// <param name="extras"></param>
        /// <returns></returns>
        public void UpdateData(JObject ds, params string[] extras)
        {
            //need extras to thell httpclient the exact address of update
            //and this method has the knowledge of msg body format

            //opertaion of HttpClient
            using (var client = new HttpClient())
            {
                try
                {
                    var changeset = ds["_changeset"];

                    if (changeset != null)
                    {
                        foreach (var rd in changeset)
                        {
                            var querystring = "";

                            if (rd["_rdstatus"].ToString() == "update")
                            {
                                ((JObject)rd).Remove("_rdstatus");
                                querystring = QueryStringResolver.GetQueryString("CommonRegex", extras[0], new JValue[] { (JValue)rd["_id"] });
                                ((JObject)rd).Remove("_id");
                            }
                            else if (rd["_rdstatus"].ToString() == "add")
                            {
                                ((JObject)rd).Remove("_rdstatus");
                                querystring = extras[1];
                            }

                            AddMetaData(client, "update");
                            var updatetask = client.PostAsync(querystring, new StringContent(((JObject)rd).ToString(), Encoding.UTF8, "application/json"));
                            updatetask.Wait();
                        }
                    }
                }
                catch (Exception ex)
                {
                }
            } //end of using
        }     //end of method
Exemplo n.º 3
0
        public async Task CreateRequestContentAsync(RequestContext requestContext,
                                                    HttpRequestMessage request,
                                                    ProxyMethodDescriptor descriptor,
                                                    ProxyUriDefinition proxyUriDefinition)
        {
            await Task.CompletedTask;

            var uriBuilder = proxyUriDefinition.UriBuilder;
            var argsDic    = descriptor.Resolve(requestContext.Args);
            var argsCount  = argsDic.Count;
            var keys       = new List <string>(argsDic.Keys);

            if (descriptor.HttpMethod == HttpMethod.Post)
            {
                if (descriptor.IsMultiPartFormData)
                {
                    request.Content = GetMultipartFormDataContent(argsDic);
                    return;
                }

                if (argsCount == 1)
                {
                    request.Content = SerializeToString(argsDic.First().Value);
                }
                else
                {
                    request.Content = SerializeToString(argsDic);
                }

                return;
            }
            else if (descriptor.HttpMethod == HttpMethod.Put)
            {
                EnsureTemplate(descriptor, proxyUriDefinition, requestContext, argsDic, keys);
                argsCount          = argsDic.Count;
                request.RequestUri = uriBuilder.Uri;
                if (argsCount == 1)
                {
                    request.Content = SerializeToString(argsDic.First().Value);
                }
                else if (argsCount == 2)
                {
                    var firstParameter  = argsDic[keys[0]];
                    var secondParameter = argsDic[keys[1]];

                    // PUT Request first parameter should be Id or Key
                    if (firstParameter.GetType().IsPrimitive())
                    {
                        uriBuilder.Query += string.Format("&{0}={1}", keys[0], firstParameter);
                    }

                    request.RequestUri = uriBuilder.Uri;
                    request.Content    = SerializeToString(secondParameter);
                }
            }
            if (descriptor.HttpMethod == HttpMethod.Get || descriptor.HttpMethod == HttpMethod.Delete)
            {
                EnsureTemplate(descriptor, proxyUriDefinition, requestContext, argsDic, keys);
                request.RequestUri = QueryStringResolver.Parse(uriBuilder, argsDic);
            }
        }