public static string GetFullUrlFor(this NancyContext context, Guid accessToken) { var urlEncodedAccessToken = HttpUtility.UrlEncode(accessToken.ToString()); string returnUrl = context.Request.Query["returnUrl"].Value; if (string.IsNullOrWhiteSpace(returnUrl)) { return(context.GetFullUrlFor("/api" + "?accessToken=" + urlEncodedAccessToken)); } if (returnUrl.Contains("?")) { return(returnUrl + "&accessToken=" + urlEncodedAccessToken); } return(returnUrl + "?accessToken=" + urlEncodedAccessToken); }
public ActionsFactory With <T>(T request, params IWithAction <T>[] overrides) where T : ApiAction { if (request.Claim != Claim.Public) { if (_context.HasUserClaim(request.Claim) == false) { return(this); } } var href = request.Href; IList <Field> fields = null; var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (var property in properties) { if (!property.CanRead) { continue; } var propertyGetMethod = property.GetGetMethod(false); var propertySetMethod = property.GetSetMethod(false); if (propertyGetMethod == null || propertySetMethod == null) { continue; } var value = property.GetValue(request, null); if (value != null && value.Equals(DefaultOf(property.PropertyType))) { value = null; } var propertyName = property.Name.FormatAsName(); var propertyValueTemplate = "{" + propertyName + "}"; if (href.Contains(propertyValueTemplate)) { if (value != null) { href = href.Replace(propertyValueTemplate, HttpUtility.UrlEncode(value.ToString())); } else { href = href.Replace("&" + propertyName + "=" + propertyValueTemplate, "") .Replace(propertyName + "=" + propertyValueTemplate, ""); } } else { if (fields == null) { fields = new List <Field>(); } var fieldType = "string"; if (property.PropertyType == typeof(decimal)) { fieldType = "number"; } var field = new Field(property.Name, fieldType, value, IsRequired <T>(property.Name)); var fieldOverrides = overrides.Where(x => x is IHavingActionFieldOverride <T>).Cast <IHavingActionFieldOverride <T> >().SingleOrDefault(x => x.GetFieldName() == property.Name); if (fieldOverrides != null) { fieldOverrides.Apply(field); } fields.Add(field); } } var action = new Action(request.Title, request.Method, _context.GetFullUrlFor(href), fields); var actionOverrides = overrides.Where(x => x is IHavingActionOverride <T>).Cast <IHavingActionOverride <T> >(); foreach (var @override in actionOverrides) { @override.Apply(action); } _actions.Add(action); return(this); }