/*
* Copyright 2013 Splunk, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"): you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
namespace Splunk
{
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;
using System.Web;
///
/// The class is a helper class for working with Splunk
/// REST API arguments. This extension is used mainly for encoding
/// arguments for UTF-8 transmission to a Splunk instance in a key/value
/// pairing for a string, or key=value1&key=value2 (and so on) for an
/// array of strings.
///
public class Args : Dictionary, ISerializable
{
///
/// Initializes a new instance of the class.
///
public Args()
{
}
///
/// Initializes a new instance of the class,
/// with a single key/value pair.
///
/// The key.
/// The value.
public Args(string key, object value)
{
base[key] = value;
}
///
/// Initializes a new instance of the class,
/// with an existing dictionary.
///
/// The existing dictionary.
public Args(Dictionary values)
{
foreach (KeyValuePair entry in values)
{
base[entry.Key] = entry.Value;
}
}
///
/// Adds a key/value pair to an object,
/// or overwrites the value if the key exists.
///
/// The key.
/// The value.
/// The object.
public Args Set(string key, object value)
{
base[key] = value;
return this;
}
///
/// Creates a new, empty object.
///
/// The new, empty object.
public static Args Create()
{
return new Args();
}
///
/// Creates a new instance and initializes it with
/// a single key/value pair.
///
/// The key.
/// The value.
/// The new initialized object.
public static Args Create(string key, object value)
{
return new Args(key, value);
}
///
/// Creates a new instance and initializes it with a
/// pre-existing dictionary.
///
/// The existing dictionary.
/// The new initialized object.
public static Args Create(Dictionary values)
{
return values == null ? new Args() : new Args(values);
}
///
/// Encodes a single string with UTF-8 encoding.
///
/// The string.
/// The UTF-8 encoded string.
public static string Encode(string value)
{
if (value == null)
{
return string.Empty;
}
return HttpUtility.UrlEncode(value);
}
///
/// Encodes a dictionary of strings or string arrays into a single
/// UTF-8-encoded string.
///
/// The string or string array.
/// The UTF-8-encoded string.
public static string Encode(Dictionary args)
{
return Args.Create(args).Encode();
}
///
/// Encodes an argument with a list-valued argument.
///
/// The string builder.
/// The key.
/// The string array.
private void
EncodeValues(StringBuilder builder, string key, string[] values)
{
key = Encode(key);
foreach (string value in values)
{
if ((builder.Length > 0) &&
(builder[builder.Length - 1] != '&'))
{
builder.Append('&');
}
builder.Append(key);
builder.Append('=');
builder.Append(Encode(value));
}
}
///
/// Encodes an instance into a UTF-8 encoded string.
///
/// The UTF-8 encoded string.
public string Encode()
{
StringBuilder builder = new StringBuilder();
foreach (KeyValuePair entry in this)
{
if (builder.Length > 0)
{
builder.Append('&');
}
string key = entry.Key;
object value = entry.Value;
if (value is string[])
{
this.EncodeValues(builder, key, (string[])value);
}
else
{
builder.Append(Encode(key));
builder.Append('=');
builder.Append(Encode(value.ToString()));
}
}
return builder.ToString();
}
///
/// Returns the dictionary value of a specific key, or the default
/// value if the key is not found.
///
/// The dictionary.
/// The key.
/// The default value.
/// The key's value in the dictionary, or the default value if
/// not found.
public static string
Get(Dictionary args, string key, string defaultValue)
{
if (!args.ContainsKey(key))
{
return defaultValue;
}
return (string)args[key];
}
}
}