// Returns object so you can have you own application-specific // metadata for your routes. protected override MyRouteMetadata GetRouteMetadata(INancyModule module, RouteDescription routeDescription) { // Return the same metadata for all routes in this sample // You would use the Path & Method of the routeDescription // to determine route specific metadata return(new MyRouteMetadata(routeDescription.Method, routeDescription.Path)); }
/// <summary> /// Add a new route to the trie /// Adds itself as a normal capture node, but also adds this node's /// children as children of the parent too /// (so it can effectively be "skipped" during matching) /// </summary> /// <param name="segments">The segments of the route definition</param> /// <param name="currentIndex">Current index in the segments array</param> /// <param name="currentScore">Current score for this route</param> /// <param name="nodeCount">Number of nodes added for this route</param> /// <param name="moduleType">The module key the route comes from</param> /// <param name="routeIndex">The route index in the module</param> /// <param name="routeDescription">The route description</param> public override void Add(string[] segments, int currentIndex, int currentScore, int nodeCount, Type moduleType, int routeIndex, RouteDescription routeDescription) { base.Add(segments, currentIndex, currentScore, nodeCount, moduleType, routeIndex, routeDescription); // Keep the same index, reduce the node count and the score this.Parent.Add(segments, currentIndex, currentScore - this.Parent.Score, nodeCount - 1, moduleType, routeIndex, routeDescription); }
public SearchDescription(RouteDescription routing, PreferKey preferKey, DateTime time, TimeType type) { this._route = routing; this.PreferKey = preferKey; this.Time = time; this.TimeType = type; }
/// <summary> /// Returns an instance of <see cref="PathItem"/> representing this route. /// </summary> /// <param name="description">The <see cref="RouteDescription"/>.</param> /// <param name="action">An <see cref="Action{PathItemBuilder}"/> for building the <see cref="PathItem"/>.</param> /// <returns>An instance of <see cref="PathItem"/> constructed using <paramref name="description"/> and by invoking <paramref name="action"/>.</returns> public static PathItem AsSwagger(this RouteDescription description, Action <PathItemBuilder> action) { var builder = new PathItemBuilder(description.Method.ToHttpMethod()); action.Invoke(builder); return(builder.Build()); }
public static RouteOperation AsSwagger2(this RouteDescription desc, Action <V2SwaggerRouteDataBuilder> action) { var builder = new V2SwaggerRouteDataBuilder(desc.Name, /*Convert(*/ desc.Method /*)*/, desc.Path); action.Invoke(builder); return(builder.Data); }
/// <summary> /// 调用服务 /// </summary> /// <param name="serializer">序列化器</param> /// <param name="route">路由信息</param> /// <param name="paramList">服务参数列表</param> /// <returns></returns> public async Task <byte[]> Invoke(ISerializer serializer, RouteDescription route, IList <byte[]> paramList) { var actionType = route.ActionType; var paramArr = await GetParams(serializer, actionType, paramList); return(await Invoke(serializer, route.ServiceType, actionType, paramArr)); }
public CustomMetadata(RouteDescription route, string description) { Name = route.Name; Path = route.Path; Method = route.Method; Segments = route.Segments; Description = description; }
/// <summary> /// Returns an instance of <see cref="SwaggerRouteData"/> representing this route. /// </summary> /// <param name="desc">The <see cref="RouteDescription"/>.</param> /// <param name="action">An <see cref="Action{SwaggerRouteDataBuilder}"/> for building the <see cref="SwaggerRouteData"/>.</param> /// <returns>An instance of <see cref="SwaggerRouteData"/> constructed using <paramref name="desc"/> and by invoking <paramref name="action"/>.</returns> public static SwaggerRouteData AsSwagger(this RouteDescription desc, Action <SwaggerRouteDataBuilder> action) { var builder = new SwaggerRouteDataBuilder(desc.Name, Convert(desc.Method), desc.Path); action.Invoke(builder); return(builder.Data); }
/// <summary> /// Creates new instance of the <see cref="CustomMetadata"/> /// </summary> /// <param name="route">The original route description</param> /// <param name="description">The route description</param> /// <param name="responseType">The response type</param> /// <param name="responseSuccess">The http status code in case of success</param> /// <param name="responseFailure">The http status code in case of failure</param> public CustomMetadata(RouteDescription route, string description, string responseType, HttpStatusCode[] responseSuccess, HttpStatusCode[] responseFailure) { Name = route.Name; Path = route.Path; Method = route.Method; Segments = route.Segments != null?string.Join(", ", route.Segments) : "/"; MainSegment = route.Segments != null?route.Segments.FirstOrDefault() : ""; Description = description; ResponseType = responseType; var tmpResponseSuccess = responseSuccess == null ? new List <HttpStatusCode>() : responseSuccess.ToList(); ResponseSuccess = string.Join("<br/>", tmpResponseSuccess.OrderBy(o => (int)o).Select(s => $"{(int)s} - {s}")); var tmpResponseFailure = responseFailure == null ? new List <HttpStatusCode>() : responseFailure.ToList(); if (tmpResponseFailure.All(a => a != HttpStatusCode.InternalServerError)) { tmpResponseFailure.Add(HttpStatusCode.InternalServerError); } ResponseFailure = string.Join("<br/>", tmpResponseFailure.OrderBy(o => (int)o).Select(s => $"{(int)s} - {s}")); IsGet = false; IsHead = false; IsPost = false; IsPut = false; IsDelete = false; switch (Method.ToLower()) { case "get": IsGet = true; break; case "head": IsHead = true; break; case "post": IsPost = true; break; case "put": IsPut = true; break; case "delete": IsDelete = true; break; } }
/// <summary> /// Add a new route to the trie /// </summary> /// <param name="routeDescription">The route description</param> public void Add(RouteDescription routeDescription) { var segments = new string[routeDescription.Segments.Length + 1]; segments[0] = routeDescription.PackageIdentity.Id; Array.Copy(routeDescription.Segments, 0, segments, 1, routeDescription.Segments.Length); Add(segments, -1, 0, 0, routeDescription); }
/// <summary> /// Returns metadata for the given <see cref="RouteDescription"/>. /// </summary> /// <param name="description">The route to obtain metadata for.</param> /// <returns>An instance of <see cref="MetadataType"/> if one exists, otherwise null.</returns> public object GetMetadata(RouteDescription description) { if (this.metadata.ContainsKey(description.Name)) { return(this.metadata[description.Name].Invoke(description)); } return(null); }
public void Should_set_return_type_property(Type returnType) { // Given, When var description = new RouteDescription(string.Empty, "GET", "/", null, returnType); // Then description.ReturnType.ShouldEqual(returnType); }
/// <summary> /// Add a new route to the trie /// Adds itself as a normal capture node, but also sets a default capture /// on the parent and adds this node's children as children of the parent /// too (so it can effectively be "skipped" during matching) /// </summary> /// <param name="segments">The segments of the route definition</param> /// <param name="currentIndex">Current index in the segments array</param> /// <param name="currentScore">Current score for this route</param> /// <param name="nodeCount">Number of nodes added for this route</param> /// <param name="moduleKey">The module key the route comes from</param> /// <param name="routeIndex">The route index in the module</param> /// <param name="routeDescription">The route description</param> public override void Add(string[] segments, int currentIndex, int currentScore, int nodeCount, string moduleKey, int routeIndex, RouteDescription routeDescription) { base.Add(segments, currentIndex, currentScore, nodeCount, moduleKey, routeIndex, routeDescription); this.Parent.AdditionalParameters[this.parameterName] = this.defaultValue; // Keep the same index, reduce the node count and the score this.Parent.Add(segments, currentIndex, currentScore - this.Parent.Score, nodeCount - 1, moduleKey, routeIndex, routeDescription); }
/// <summary> /// Build the node data that will be used to create the <see cref="MatchResult" /> /// We calculate/store as much as possible at build time to reduce match time. /// </summary> /// <param name="nodeCount">Number of nodes in the route</param> /// <param name="score">Score for the route</param> /// <param name="routeDescription">The route description</param> /// <returns>A NodeData instance</returns> protected virtual NodeData BuildNodeData(int nodeCount, int score, RouteDescription routeDescription) { return(new NodeData { Method = routeDescription.Method, RouteLength = nodeCount, Score = score, RouteDescription = routeDescription }); }
public MetadataModuleRouteMetadataProviderFixture() { this.resolver = A.Fake <IMetadataModuleResolver>(); this.module = A.Fake <INancyModule>(); this.route = new RouteDescription("NamedDescription", "GET", "/things", ctx => true); this.metadataModule = new FakeNancyMetadataModule(); this.metadataModule.Describe[this.route.Name] = desc => { return(Metadata); }; this.provider = new MetadataModuleRouteMetadataProvider(this.resolver); }
/// <summary> /// Returns metadata for the given <see cref="RouteDescription"/>. /// </summary> /// <param name="description">The route to obtain metadata for.</param> /// <returns>An instance of <see cref="MetadataType"/> if one exists, otherwise null.</returns> public object GetMetadata(RouteDescription description) { Func <RouteDescription, TMetadata> func; if (this.metadata.TryGetValue(description.Name, out func)) { return(func.Invoke(description)); } return(null); }
public TrieNode GetNodeForSegment(TrieNode parent, string segment, RouteDescription routeDescription) { var chars = segment.ToCharArray(); var start = chars[0]; var end = chars[chars.Length - 1]; if (start == '{' && end == '}') { return(new CaptureNode(parent, segment, this)); } return(new LiteralNode(parent, segment, this)); }
public void Generate_openapi_metadata() { //Arrange var endpoint = new FakeEndpoint(); var fakeRouteDescription = new RouteDescription(endpoint.Operation, endpoint.Method, endpoint.Path, null); //Act var metadata = new OpenApiRouteMetadata(fakeRouteDescription); //Assert Assert.Equal(metadata.Path, fakeRouteDescription.Path); Assert.Equal(metadata.Method, fakeRouteDescription.Method.ToLowerInvariant()); Assert.Equal(metadata.Name, fakeRouteDescription.Name); }
public SearchViewModel() { if (this.IsInDesignMode) { _route = new RouteDescription(); _preferType = 0; } else { _route = Setting.CurrentSearchConstraint; if (_route == null) _route = new RouteDescription(); _preferType = (int)Setting.LastPreferKey; } }
public void Generate_method_description() { //Arrange var endpoint = new FakeEndpoint(); var fakeRouteDescription = new RouteDescription(endpoint.Operation, endpoint.Method, endpoint.Path, null); //Act var metadata = new OpenApiRouteMetadata(fakeRouteDescription) .With(i => i.WithResponseModel("200", typeof(FakeResponseModel), "Sample response") .WithSummary(endpoint.Summary)); //Assert Assert.Equal(metadata.Path, fakeRouteDescription.Path); Assert.Equal(metadata.Method, fakeRouteDescription.Method.ToLowerInvariant()); Assert.Equal(metadata.Name, fakeRouteDescription.Name); Assert.Equal(metadata.Info.OperationId, endpoint.Operation); Assert.Equal(metadata.Info.Summary, endpoint.Summary); }
/// <summary> /// Gets the <see cref="T:System.Type" /> of the metadata that is created by the provider. /// </summary> /// <param name="module">The <see cref="T:Nancy.INancyModule" /> instance that the route is declared in.</param> /// <param name="routeDescription">A <see cref="T:Nancy.Routing.RouteDescription" /> for the route.</param> /// <returns> /// A <see cref="T:System.Type" /> instance, or <see langword="null" /> if nothing is found. /// </returns> public Type GetMetadataType(INancyModule module, RouteDescription routeDescription) { if (String.IsNullOrWhiteSpace(routeDescription.Name)) { return(null); } // TODO: Yikes, what an ugly hack. We need to figure out a better way to identify routes than their name. [asbjornu] switch (routeDescription.Name) { case JsonSchema: case ClientAssembly: case ClientNugetPackage: case ClientNugetPackageVersioned: return(typeof(PomonaRouteMetadata)); } return(null); }
/// <summary> /// Add a new route to the trie /// </summary> /// <param name="segments">The segments of the route definition</param> /// <param name="currentIndex">Current index in the segments array</param> /// <param name="currentScore">Current score for this route</param> /// <param name="nodeCount">Number of nodes added for this route</param> /// <param name="moduleType">The module key the route comes from</param> /// <param name="routeIndex">The route index in the module</param> /// <param name="routeDescription">The route description</param> public virtual void Add(string[] segments, int currentIndex, int currentScore, int nodeCount, Type moduleType, int routeIndex, RouteDescription routeDescription) { if (this.NoMoreSegments(segments, currentIndex)) { this.NodeData.Add(this.BuildNodeData(nodeCount, currentScore + this.Score, moduleType, routeIndex, routeDescription)); return; } nodeCount++; currentIndex++; TrieNode child; if (!this.Children.TryGetValue(segments[currentIndex], out child)) { child = this.nodeFactory.GetNodeForSegment(this, segments[currentIndex]); this.Children.Add(segments[currentIndex], child); } child.Add(segments, currentIndex, currentScore + this.Score, nodeCount, moduleType, routeIndex, routeDescription); }
/// <summary> /// Add a new route to the trie /// </summary> /// <param name="segments">The segments of the route definition</param> /// <param name="currentIndex">Current index in the segments array</param> /// <param name="currentScore">Current score for this route</param> /// <param name="nodeCount">Number of nodes added for this route</param> /// <param name="routeDescription">The route description</param> public virtual void Add(string[] segments, int currentIndex, int currentScore, int nodeCount, RouteDescription routeDescription) { if (NoMoreSegments(segments, currentIndex)) { NodeData.Add(BuildNodeData(nodeCount, currentScore + Score, routeDescription)); return; } nodeCount++; currentIndex++; if (!Children.TryGetValue(segments[currentIndex], out var child)) { child = _nodeFactory.GetNodeForSegment(this, segments[currentIndex], routeDescription); Children.Add(segments[currentIndex], child); } child.Add(segments, currentIndex, currentScore + Score, nodeCount, routeDescription); }
public object GetMetadata(INancyModule module, RouteDescription routeDescription) { var entryAssembly = Assembly.GetEntryAssembly(); if (module.GetType().Assembly != entryAssembly) { return(null); } DocumentationObject val = null; var eps = BuildDocumentation(Types); string path; var basePath = "/"; if (module.ModulePath.Length > 1) { basePath = module.ModulePath; path = routeDescription.Path.Replace(module.ModulePath, ""); if (string.IsNullOrEmpty(path)) { path = "/"; } } else { path = routeDescription.Path; } val = eps.FirstOrDefault(p => string.Equals(routeDescription.Method, p.HttpMethod.ToString(), StringComparison.CurrentCultureIgnoreCase) && p.BasePath == basePath && path == p.Path); if (val == null) { //TODO:(drose) throw exception to enfore usage? Console.ForegroundColor = ConsoleColor.Red; Console.Out.WriteLine($"No Documentation Found For {module.GetModuleName()}Module : {routeDescription.Method} {path}"); Console.ForegroundColor = ConsoleColor.White; return(null); } val.BasePath = basePath; val.ModuleName = module.GetModuleName().ToLower(); return(val); }
/// <summary> /// 构造函数 /// </summary> /// <param name="host">服务地址</param> /// <param name="port">服务端口</param> /// <param name="identity">身份标识</param> /// <param name="remoteAddress">客户端地址</param> /// <param name="route">服务路由信息</param> /// <param name="actionParamList">Action请求所传递的参数列表</param> /// <param name="attachments">服务请求所传递的附加数据</param> public ServiceContext(string host, int port, string identity, IPEndPoint remoteAddress, RouteDescription route, IList <byte[]> actionParamList, IDictionary <string, byte[]> attachments) { if (current.Value != null) { throw new InvalidOperationException("ServiceContext has instantiated."); } Identity = identity; RemoteAddress = remoteAddress; Route = route; ActionParamList = actionParamList; Attachments = attachments; current.Value = this; }
public object GetMetadata(INancyModule module, RouteDescription routeDescription) { var type = GetModuleMetadataType(module); if (type == null) { return(null); } ModuleMetadata metadata; if (MetadataItems.TryGetValue(type, out metadata)) { return(metadata[routeDescription.Method, routeDescription.Path]); } metadata = (ModuleMetadata)(_container.Resolve(type)); MetadataItems.Add(type, metadata); return(metadata?[routeDescription.Method, routeDescription.Path]); }
/// <summary> /// Gets the metadata for the provided route. /// </summary> /// <param name="module">The <see cref="T:Nancy.INancyModule" /> instance that the route is declared in.</param> /// <param name="routeDescription">A <see cref="T:Nancy.Routing.RouteDescription" /> for the route.</param> /// <returns> /// An object representing the metadata for the given route, or <see langword="null" /> if nothing is found. /// </returns> public object GetMetadata(INancyModule module, RouteDescription routeDescription) { if (String.IsNullOrWhiteSpace(routeDescription.Name)) { return(null); } // TODO: Yikes, what an ugly hack. We need to figure out a better way to identify routes than their name. [asbjornu] switch (routeDescription.Name) { case JsonSchema: return(new PomonaRouteMetadata { ContentType = "application/json", Method = HttpMethod.Get, Relation = "json-schema", }); case ClientAssembly: return(new PomonaRouteMetadata { ContentType = "binary/octet-stream", Method = HttpMethod.Get, Relation = "client-assembly", }); case ClientNugetPackage: case ClientNugetPackageVersioned: return(new PomonaRouteMetadata { ContentType = "application/zip", Method = HttpMethod.Get, Relation = "nuget-package", }); } return(null); }
public SearchDescription() { this._route = new RouteDescription(); }
/// <summary> /// Add a new route to the trie /// </summary> /// <param name="segments">The segments of the route definition</param> /// <param name="moduleType">The module key the route comes from</param> /// <param name="routeIndex">The route index in the module</param> /// <param name="routeDescription">The route description</param> public void Add(string[] segments, Type moduleType, int routeIndex, RouteDescription routeDescription) { this.Add(segments, -1, 0, 0, moduleType, routeIndex, routeDescription); }
/// <summary> /// Build the node data that will be used to create the <see cref="MatchResult"/> /// We calculate/store as much as possible at build time to reduce match time. /// </summary> /// <param name="nodeCount">Number of nodes in the route</param> /// <param name="score">Score for the route</param> /// <param name="moduleType">The module key the route comes from</param> /// <param name="routeIndex">The route index in the module</param> /// <param name="routeDescription">The route description</param> /// <returns>A NodeData instance</returns> protected virtual NodeData BuildNodeData(int nodeCount, int score, Type moduleType, int routeIndex, RouteDescription routeDescription) { return new NodeData { Method = routeDescription.Method, RouteIndex = routeIndex, RouteLength = nodeCount, Score = score, Condition = routeDescription.Condition, ModuleType = moduleType, }; }
public MetadataModuleFixture() { this.route = new RouteDescription("NamedDescription", "GET", "/things", ctx => true, typeof(object)); this.metadataModule = new FakeNancyMetadataModule(); }
public MetadataModuleFixture() { this.route = new RouteDescription("NamedDescription", "GET", "/things", ctx => true); this.metadataModule = new FakeLegacyNancyMetadataModule(); }
public OpenApiRouteMetadata(RouteDescription desc) : this(desc.Path, desc.Method, desc.Name) { }
public SwaggerRouteMetadata(RouteDescription desc) : this(desc.Path, desc.Method) { }
/// <summary> /// Add a new route to the trie /// Adds itself as a normal capture node, but also sets a default capture /// on the parent and adds this node's children as children of the parent /// too (so it can effectively be "skipped" during matching) /// </summary> /// <param name="segments">The segments of the route definition</param> /// <param name="currentIndex">Current index in the segments array</param> /// <param name="currentScore">Current score for this route</param> /// <param name="nodeCount">Number of nodes added for this route</param> /// <param name="moduleType">The module key the route comes from</param> /// <param name="routeIndex">The route index in the module</param> /// <param name="routeDescription">The route description</param> public override void Add(string[] segments, int currentIndex, int currentScore, int nodeCount, Type moduleType, int routeIndex, RouteDescription routeDescription) { base.Add(segments, currentIndex, currentScore, nodeCount, moduleType, routeIndex, routeDescription); this.Parent.AdditionalParameters[this.parameterName] = this.defaultValue; // Keep the same index, reduce the node count and the score this.Parent.Add(segments, currentIndex, currentScore - this.Parent.Score, nodeCount - 1, moduleType, routeIndex, routeDescription); }
/// <summary> /// Build the node data that will be used to create the <see cref="MatchResult"/> /// We calculate/store as much as possible at build time to reduce match time. /// </summary> /// <param name="nodeCount">Number of nodes in the route</param> /// <param name="score">Score for the route</param> /// <param name="moduleType">The module key the route comes from</param> /// <param name="routeIndex">The route index in the module</param> /// <param name="routeDescription">The route description</param> /// <returns>A NodeData instance</returns> protected virtual NodeData BuildNodeData(int nodeCount, int score, Type moduleType, int routeIndex, RouteDescription routeDescription) { return(new NodeData { Method = routeDescription.Method, RouteIndex = routeIndex, RouteLength = nodeCount, Score = score, Condition = routeDescription.Condition, ModuleType = moduleType, }); }