private Uri Transform(StringBuilder uriBuilder) { var uri = new Uri(uriBuilder.ToString(), UriKind.Absolute); if (_uriTransformer != null) { uri = _uriTransformer.TransformUri(uri); } return(uri); }
private Uri CreateRequestUri(string operationName, JsonObject parameters) { StringBuilder uriBuilder = new StringBuilder(); List <object> values = new List <object>(); HashSet <string> addedParameters = null; string rewrittenUriFormat = TokenFormatRewriteRegex.Replace(_uriFormat, delegate(Match m) { Group startGroup = m.Groups["start"]; Group propertyGroup = m.Groups["property"]; Group formatGroup = m.Groups["format"]; Group endGroup = m.Groups["end"]; if ((operationName.Length != 0) && String.CompareOrdinal(propertyGroup.Value, "operation") == 0) { values.Add(operationName); } else if (_parameters != null) { values.Add(_parameters[propertyGroup.Value]); if (addedParameters == null) { addedParameters = new HashSet <string>(StringComparer.Ordinal); } addedParameters.Add(propertyGroup.Value); } return(new string('{', startGroup.Captures.Count) + (values.Count - 1) + formatGroup.Value + new string('}', endGroup.Captures.Count)); }); if (values.Count != 0) { uriBuilder.AppendFormat(CultureInfo.InvariantCulture, rewrittenUriFormat, values.ToArray()); } else { uriBuilder.Append(rewrittenUriFormat); } if (rewrittenUriFormat.IndexOf('?') < 0) { uriBuilder.Append("?"); } if (parameters != null) { foreach (KeyValuePair <string, object> param in (IDictionary <string, object>)parameters) { string name = param.Key; object value = param.Value; if ((addedParameters != null) && addedParameters.Contains(name)) { // Already consumed above to substitute a named token // in the URI format. continue; } if (value is Delegate) { // Ignore callbacks in the async scenario. continue; } if (value is JsonObject) { // Nested object... use name.subName=value format. foreach (KeyValuePair <string, object> nestedParam in (IDictionary <string, object>)value) { uriBuilder.AppendFormat("&{0}.{1}={2}", name, nestedParam.Key, FormatUriParameter(nestedParam.Value)); } continue; } uriBuilder.AppendFormat("&{0}={1}", name, FormatUriParameter(value)); } } Uri uri = new Uri(uriBuilder.ToString(), UriKind.Absolute); if (_uriTransformer != null) { uri = _uriTransformer.TransformUri(uri); } return(uri); }