ServiceUri.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // -
  2. // <copyright file="ServiceUri.cs" company="Microsoft Corporation">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. // -
  6. namespace Microsoft.Hawaii
  7. {
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Net;
  11. using System.Text;
  12. /// <summary>
  13. /// Helper class used to create a Service Uri during a service invocation.
  14. /// </summary>
  15. public class ServiceUri
  16. {
  17. /// <summary>
  18. /// Initializes a new instance of the ServiceUri class.
  19. /// </summary>
  20. public ServiceUri()
  21. {
  22. this.Arguments = new List<KeyValuePair<string, string>>();
  23. }
  24. /// <summary>
  25. /// Initializes a new instance of the ServiceUri class.
  26. /// </summary>
  27. /// <param name="hostUrl">Specifies the host url. For example: http://ocr2.hawaii-services.net.</param>
  28. /// <param name="signature">Specifies the service signature.</param>
  29. public ServiceUri(string hostUrl, string signature)
  30. {
  31. this.HostUrl = hostUrl;
  32. this.Signature = signature;
  33. this.Arguments = new List<KeyValuePair<string, string>>();
  34. }
  35. /// <summary>
  36. /// Gets or sets service host url.
  37. /// </summary>
  38. public string HostUrl { get; set; }
  39. /// <summary>
  40. /// Gets or sets service's REST signature
  41. /// For Example :
  42. /// Service Url : http://hawaiispeech/SpeechRecognition
  43. /// Signature : SpeechRecognition
  44. /// Service Url : http://hawaiispeech/SpeechRecognition/GrammerName/
  45. /// Signature : SpeechRecognition/GrammarName.
  46. /// </summary>
  47. public string Signature { get; set; }
  48. /// <summary>
  49. /// Gets or sets arguments used for the Query String part of the Service Url.
  50. /// </summary>
  51. private List<KeyValuePair<string, string>> Arguments { get; set; }
  52. /// <summary>
  53. /// A Helper method to add a query string key-value pair.
  54. /// </summary>
  55. /// <param name="key">
  56. /// The key in the query string.
  57. /// </param>
  58. /// <param name="value">
  59. /// The value corresponding to the key.
  60. /// </param>
  61. public void AddQueryString(string key, string value)
  62. {
  63. this.Arguments.Add(new KeyValuePair<string, string>(key, value));
  64. }
  65. /// <summary>
  66. /// Returns the service uri string.
  67. /// </summary>
  68. /// <returns>A valid Service Uri string.</returns>
  69. public override string ToString()
  70. {
  71. StringBuilder address = new StringBuilder();
  72. // Create the service uri
  73. if (string.IsNullOrEmpty(this.Signature))
  74. {
  75. address.AppendFormat("{0}", this.HostUrl);
  76. }
  77. else
  78. {
  79. address.AppendFormat("{0}/{1}", this.HostUrl, this.Signature);
  80. }
  81. int count = this.Arguments.Count;
  82. // Add ? to start the query strings
  83. if (count != 0)
  84. {
  85. address.Append("?");
  86. }
  87. // Form the query string from the arguments collection.
  88. foreach (KeyValuePair<string, string> item in this.Arguments)
  89. {
  90. address.AppendFormat("{0}={1}", item.Key, item.Value);
  91. address.Append("&");
  92. }
  93. if (count != 0)
  94. {
  95. // Remove the last ambersand.
  96. address.Remove(address.Length - 1, 1);
  97. }
  98. // Return the service uri string.
  99. return address.ToString();
  100. }
  101. }
  102. }