Example #1
0
        protected virtual void SetHeader(RequestDescriptor descriptor, HttpRequestHeaders header)
        {
            var headers = descriptor.Headers;

            foreach (var key in headers.Keys)
            {
                foreach (var v in headers[key])
                {
                    var value    = v;
                    var template = new StringTemplate(value);
                    if (!template.HaveVariable)
                    {
                        header.Add(key, value);
                        continue;
                    }
                    _logger.LogInformation($"{key}:{value}需要进行模板替换");
                    //尝试使用uri参数进行替换
                    value    = template.TryReplaceVariable(descriptor.UriQuery);
                    template = new StringTemplate(value);
                    if (!template.HaveVariable)
                    {
                        header.Add(key, value);
                        continue;
                    }
                    //尝试使用ExtendData进行替换
                    value    = template.TryReplaceVariable(descriptor.ExtendData);
                    template = new StringTemplate(value);
                    if (!template.HaveVariable)
                    {
                        header.Add(key, value);
                        continue;
                    }
                    //尝试使用Body进行替换
                    value = template.TryReplaceVariable(descriptor.Body, true);
                    header.Add(key, value);
                }
            }
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="descriptor"></param>
        /// <returns></returns>
        protected virtual Uri GetUri(RequestDescriptor descriptor)
        {
            var tempUri  = descriptor.HttpOption.Uri;
            var template = new StringTemplate(tempUri);

            if (template.HaveVariable) //使用query替换变量
            {
                tempUri  = template.TryReplaceVariable(descriptor.UriQuery);
                template = new StringTemplate(tempUri);
                if (template.HaveVariable) //使用context.ExtendData替换变量
                {
                    _logger.LogInformation($"uri:{tempUri} 使用{nameof(descriptor.UriQuery)}未完全替换");
                    tempUri  = template.TryReplaceVariable(descriptor.ExtendData);
                    template = new StringTemplate(tempUri);
                    if (template.HaveVariable) //使用body替换变量
                    {
                        _logger.LogInformation($"uri:{tempUri}使用{nameof(descriptor.ExtendData)}未完全替换");
                        tempUri = template.TryReplaceVariable(descriptor.Body, true);
                    }
                }
            }
            return(descriptor.UriQuery.Concat(tempUri));
        }