Class for handling optional parameters for methods. These options can then be directly applied to the web service parameterse.
Exemple #1
0
        /// <summary>
        /// Changes an array (hash table) of parameters to a url.  
        /// </summary>
        protected static string ParametersToUrl(string baseUrl, 
                                                OptionList parameters)
        {
            StringBuilder result = new StringBuilder();

            result.Append(Service.ServiceUrl).Append("/");
            result.Append(baseUrl).Append("?");

            foreach (string key in parameters.Keys)
            {
                string valueText = parameters[key];
                // Do not add keys with null or empty values
                if (valueText != null && valueText.Length > 0)
                {
                    result.Append(key).Append("=");
                    result.Append(Service.UrlEncode(valueText));
                    result.Append("&");
                }
            }

            return result.ToString();
        }
Exemple #2
0
        /// <summary>
        /// Finds all pages matching the given criteria.  
        /// </summary>
        public static Vuzit.Page[] FindAll(string webId, OptionList options)
        {
            if (webId == null)
            {
                throw new Vuzit.ClientException("webId cannot be null");
            }

            List<Vuzit.Page> result = new List<Vuzit.Page>();

            OptionList parameters = PostParameters(options, "index", webId);

            string url = ParametersToUrl("documents/" + webId + "/pages.xml", parameters);
            HttpWebRequest request = WebRequestBuild(url);
            request.UserAgent = Service.UserAgent;
            request.Method = "GET";

            // Catch 403, etc forbidden errors
            try
            {
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    if (response == null)
                    {
                        throw new Vuzit.ClientException("No XML response from server", 0);
                    }

                    XmlDocument doc = new XmlDocument();
                    try
                    {
                        doc.LoadXml(ReadHttpResponse(response));
                    }
                    catch (ClientException ex)
                    {
                        throw new Vuzit.ClientException("Incorrect XML response: " + ex.Message, 0);
                    }

                    XmlNode node = doc.SelectSingleNode("/err/code");
                    if (node != null)
                    {
                        string msg = doc.SelectSingleNode("/err/msg").InnerText;
                        throw new Vuzit.ClientException("Web service error: " + msg, node.InnerText);
                    }

                    XmlNodeList list = doc.SelectNodes("/pages/page");

                    foreach (XmlNode childNode in list)
                    {
                        result.Add(NodeToPage(childNode));
                    }
                }
            }
            catch (Vuzit.ClientException ex)
            {
                throw ex;  // Rethrow because I want to see this exception
            }
            catch (WebException ex)
            {
                throw new Vuzit.ClientException("HTTP response error", ex);
            }

            return result.ToArray();
        }
Exemple #3
0
        /// <summary>
        /// Returns the default HTTP post parameters array.  
        /// </summary>
        protected static OptionList PostParameters(OptionList options, string method, string id)
        {
            options.Add("method", method);
            options.Add("key", Service.PublicKey);
            DateTime date = DateTime.Now;
            string signature = Service.Signature(method, id, date, options);
            options.Add("signature", signature);
            options.Add("timestamp", Service.EpochTime(date).ToString());

            return options;
        }
Exemple #4
0
        /// <summary>
        /// Returns the signature string.  
        /// </summary>
        /// <param name="service">Service name (e.g. 'show').</param>
        /// <param name="id">ID of the document.</param>
        /// <param name="date">Date of the request</param>
        public static string Signature(string service, string id, 
                                       DateTime date, OptionList options)
        {
            StringBuilder result = new StringBuilder();

            if (PublicKey == null)
            {
                throw new ArgumentException("The PublicKey parameter cannot be null");
            }

            if (PrivateKey == null)
            {
                throw new ArgumentException("The PrivateKey parameter cannot be null");
            }

            if (date == DateTime.MinValue)
            {
                date = DateTime.Now;
            }

            if (id == null)
            {
                id = String.Empty;
            }

            result.Append(service).Append(id).Append(PublicKey)
                  .Append(EpochTime(date).ToString());

            if (options != null)
            {
                string[] optionsList = new string[] { "included_pages", 
                                                      "watermark", 
                                                      "query" };
                foreach (string item in optionsList)
                {
                    if (options.Contains(item))
                    {
                        result.Append(options[item]);
                    }
                }
            }

            return CalculateRFC2104HMAC(result.ToString(), PrivateKey);
        }