public static string ToQueryString(this IEnumerable <PropertyInfo> propertyInfos, object request) { var parameters = String.Empty; foreach (var property in propertyInfos) { var value = property.GetValue(request, null); if (value == null) { continue; } parameters += "&{0}={1}".Fmt(property.Name.ToCamelCase(), RestRoute.FormatQueryParameterValue(value)); } if (!String.IsNullOrEmpty(parameters)) { parameters = parameters.Substring(1); } return(parameters); }
internal static string GetQueryString(object request, IDictionary <string, RouteMember> propertyMap) { var result = new StringBuilder(); foreach (var queryProperty in propertyMap) { var value = queryProperty.Value.GetValue(request, true); if (value == null) { continue; } result.Append(queryProperty.Key) .Append('=') .Append(RestRoute.FormatQueryParameterValue(value)) .Append('&'); } if (result.Length > 0) { result.Length -= 1; } return(result.ToString()); }
public static RouteResolutionResult Success(RestRoute route, string uri) { return new RouteResolutionResult { Route = route, Uri = uri }; }
public static RouteResolutionResult Error(RestRoute route, string errorMsg) { return new RouteResolutionResult { Route = route, FailReason = errorMsg }; }
public static RouteResolutionResult Success(RestRoute route, string uri) { return(new RouteResolutionResult { Route = route, Uri = uri }); }
public static RouteResolutionResult Error(RestRoute route, string errorMsg) { return(new RouteResolutionResult { Route = route, FailReason = errorMsg }); }
public static string ToUrl(this IReturn request, string httpMethod, string formatFallbackToPredefinedRoute = null) { httpMethod = httpMethod.ToUpper(); var requestType = request.GetType(); var requestRoutes = routesCache.GetOrAdd(requestType, GetRoutesForType); if (requestRoutes.Count == 0) { if (formatFallbackToPredefinedRoute == null) { throw new InvalidOperationException("There are no rest routes mapped for '{0}' type. ".Fmt(requestType) + "(Note: The automatic route selection only works with [Route] attributes on the request DTO and" + "not with routes registered in the IAppHost!)"); } var predefinedRoute = "/{0}/syncreply/{1}".Fmt(formatFallbackToPredefinedRoute, requestType.Name); if (httpMethod == "GET" || httpMethod == "DELETE" || httpMethod == "OPTIONS" || httpMethod == "HEAD") { var queryProperties = RestRoute.GetQueryProperties(request.GetType()); predefinedRoute += "?" + RestRoute.GetQueryString(request, queryProperties); } return(predefinedRoute); } var routesApplied = requestRoutes.Select(route => route.Apply(request, httpMethod)).ToList(); var matchingRoutes = routesApplied.Where(x => x.Matches).ToList(); if (matchingRoutes.Count == 0) { var errors = string.Join(String.Empty, routesApplied.Select(x => "\r\n\t{0}:\t{1}".Fmt(x.Route.Path, x.FailReason)).ToArray()); var errMsg = "None of the given rest routes matches '{0}' request:{1}" .Fmt(requestType.Name, errors); throw new InvalidOperationException(errMsg); } RouteResolutionResult matchingRoute; if (matchingRoutes.Count > 1) { matchingRoute = FindMostSpecificRoute(matchingRoutes); if (matchingRoute == null) { var errors = String.Join(String.Empty, matchingRoutes.Select(x => "\r\n\t" + x.Route.Path).ToArray()); var errMsg = "Ambiguous matching routes found for '{0}' request:{1}".Fmt(requestType.Name, errors); throw new InvalidOperationException(errMsg); } } else { matchingRoute = matchingRoutes[0]; } var url = matchingRoute.Uri; if (httpMethod == HttpMethods.Get || httpMethod == HttpMethods.Delete || httpMethod == HttpMethods.Head) { var queryParams = matchingRoute.Route.FormatQueryParameters(request); if (!String.IsNullOrEmpty(queryParams)) { url += "?" + queryParams; } } return(url); }
private IEnumerable<PostmanRequest> GetRequests(IHttpRequest request, ServiceMetadata metadata, string parentId, IEnumerable<Operation> operations) { var feature = EndpointHost.GetPlugin<PostmanFeature>(); var label = request.GetParam("label") ?? feature.DefaultLabel; var customHeaders = request.GetParam("headers"); var headers = customHeaders == null ? feature.DefaultHeaders : customHeaders.Split(','); foreach (var op in metadata.OperationsMap.Values.Where(o => metadata.IsVisible(request, o))) { var exampleObject = ReflectionUtils.PopulateObject(op.RequestType.CreateInstance()).ToStringDictionary(); var data = op.RequestType.GetSerializableFields().Select(f => f.Name) .Concat(op.RequestType.GetSerializableProperties().Select(p => p.Name)) .ToDictionary(f => f, f => exampleObject.GetValueOrDefault(f)); foreach (var route in op.Routes) { var routeVerbs = route.AllowsAllVerbs ? new[] { "POST" } : route.AllowedVerbs.Split(new [] { ',' }, StringSplitOptions.RemoveEmptyEntries); var restRoute = new RestRoute(route.RequestType, route.Path, route.AllowedVerbs); foreach (var verb in routeVerbs) { yield return new PostmanRequest { Id = Guid.NewGuid().ToString(), Headers = string.Join("\n", headers), Method = verb, Url = CalculateAppUrl(request, _aspnetSubPath) + restRoute.Path.ReplaceVariables(), Name = label.FormatLabel(op.RequestType, restRoute.Path), Description = op.RequestType.GetDescription(), PathVariables = restRoute.Variables.ToDictionary(v => v, v => data.GetValueOrDefault(v)), Data = data.Keys.Except(restRoute.Variables).Select(v => new PostmanData { Key = v, Value = data[v], Type = "text", }).ToArray(), DataMode = "params", Version = 2, Time = DateTime.UtcNow.ToUnixTimeMs(), CollectionId = parentId, Folder = restRoute.Path.GetFolderName() }; } } } }