纽威
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.9 KiB

  1. using System.Collections.Generic;
  2. using System.Collections.ObjectModel;
  3. using System.Net.Http.Headers;
  4. using System.Web.Http.Description;
  5. using ICSSoft.WebAPI.Areas.HelpPage.ModelDescriptions;
  6. namespace ICSSoft.WebAPI.Areas.HelpPage.Models
  7. {
  8. /// <summary>
  9. /// The model that represents an API displayed on the help page.
  10. /// </summary>
  11. public class HelpPageApiModel
  12. {
  13. /// <summary>
  14. /// Initializes a new instance of the <see cref="HelpPageApiModel"/> class.
  15. /// </summary>
  16. public HelpPageApiModel()
  17. {
  18. UriParameters = new Collection<ParameterDescription>();
  19. SampleRequests = new Dictionary<MediaTypeHeaderValue, object>();
  20. SampleResponses = new Dictionary<MediaTypeHeaderValue, object>();
  21. ErrorMessages = new Collection<string>();
  22. }
  23. /// <summary>
  24. /// Gets or sets the <see cref="ApiDescription"/> that describes the API.
  25. /// </summary>
  26. public ApiDescription ApiDescription { get; set; }
  27. /// <summary>
  28. /// Gets or sets the <see cref="ParameterDescription"/> collection that describes the URI parameters for the API.
  29. /// </summary>
  30. public Collection<ParameterDescription> UriParameters { get; private set; }
  31. /// <summary>
  32. /// Gets or sets the documentation for the request.
  33. /// </summary>
  34. public string RequestDocumentation { get; set; }
  35. /// <summary>
  36. /// Gets or sets the <see cref="ModelDescription"/> that describes the request body.
  37. /// </summary>
  38. public ModelDescription RequestModelDescription { get; set; }
  39. /// <summary>
  40. /// Gets the request body parameter descriptions.
  41. /// </summary>
  42. public IList<ParameterDescription> RequestBodyParameters
  43. {
  44. get
  45. {
  46. return GetParameterDescriptions(RequestModelDescription);
  47. }
  48. }
  49. /// <summary>
  50. /// Gets or sets the <see cref="ModelDescription"/> that describes the resource.
  51. /// </summary>
  52. public ModelDescription ResourceDescription { get; set; }
  53. /// <summary>
  54. /// Gets the resource property descriptions.
  55. /// </summary>
  56. public IList<ParameterDescription> ResourceProperties
  57. {
  58. get
  59. {
  60. return GetParameterDescriptions(ResourceDescription);
  61. }
  62. }
  63. /// <summary>
  64. /// Gets the sample requests associated with the API.
  65. /// </summary>
  66. public IDictionary<MediaTypeHeaderValue, object> SampleRequests { get; private set; }
  67. /// <summary>
  68. /// Gets the sample responses associated with the API.
  69. /// </summary>
  70. public IDictionary<MediaTypeHeaderValue, object> SampleResponses { get; private set; }
  71. /// <summary>
  72. /// Gets the error messages associated with this model.
  73. /// </summary>
  74. public Collection<string> ErrorMessages { get; private set; }
  75. private static IList<ParameterDescription> GetParameterDescriptions(ModelDescription modelDescription)
  76. {
  77. ComplexTypeModelDescription complexTypeModelDescription = modelDescription as ComplexTypeModelDescription;
  78. if (complexTypeModelDescription != null)
  79. {
  80. return complexTypeModelDescription.Properties;
  81. }
  82. CollectionModelDescription collectionModelDescription = modelDescription as CollectionModelDescription;
  83. if (collectionModelDescription != null)
  84. {
  85. complexTypeModelDescription = collectionModelDescription.ElementDescription as ComplexTypeModelDescription;
  86. if (complexTypeModelDescription != null)
  87. {
  88. return complexTypeModelDescription.Properties;
  89. }
  90. }
  91. return null;
  92. }
  93. }
  94. }