public static IAppBuilder ExposeRootNodeAsHypermediaApi(this IAppBuilder appBuilder, RootNode root, ServiceLocatorDelegate serviceLocatorDelegate, Representor <SemanticDocument>[] representors) { var router = NodeRouting.MakeHypermediaRouterFromRootNode(root, serviceLocatorDelegate); FindUriForTerm locatorDelegate = term => root.GlossaryNode.GetUriForTerm(term); return(appBuilder.ExposeRouterAsHypermediaApi(router, locatorDelegate, representors, root.Uri.ToString())); }
public static async Task Write <TRep>(OwinContext ctx, TRep representation, FindUriForTerm termUriFinder, Representor <TRep>[] settings) { Func <string, string, Task> writeStringToResponse = async(contentType, body) => { ctx.Response.ContentType = contentType; await ctx.Response.WriteAsync(body); }; var accept = ctx.Request.Accept; var representor = settings.FirstOrDefault(r => r.AcceptTypes.Intersect(ctx.Request.Accept.Split(',')).Select(h => h.Trim()).Any()); if (representor != null) { var response = await representor.GetResponse(representation, termUriFinder); await writeStringToResponse(response.Item1, response.Item2); } else { { throw new NotImplementedException(); } } }
public abstract Task <Tuple <string, string> > GetResponse(TRep hypermediaObject, FindUriForTerm termUriFinder);
public override async Task <Tuple <string, string> > GetResponse(SemanticDocument hypermediaObject, FindUriForTerm termUriFinder) { var sirenRep = new SirenRepresentor(); var response = await sirenRep.GetResponse(hypermediaObject, termUriFinder); var index = new HyperMapper.Siren.Index() { Model = JToken.Parse(response.Item2) }; var transformText = index.TransformText(); return(Tuple.Create("text/html", transformText)); }
public override Task <Tuple <string, string> > GetResponse(SemanticDocument hypermediaObject, FindUriForTerm termUriFinder) { var sirenRepresentor = new SirenRepresentor(); var sirenDoc = sirenRepresentor.BuildFromSemanticDocument(hypermediaObject, termUriFinder); var html = SirenToHtmlConverter.ReadSirenAndConvertToForm(sirenDoc); return(Task.FromResult(Tuple.Create("text/html", html))); }
private static async Task HandleRequest <TRep>(Representor <TRep>[] representors, OwinContext ctx, RequestHandler <TRep> requestHandler, FindUriForTerm termUriFinder) { RequestHandling.BindModel bindModel = args => ModelBinder.BindArgsFromRequest(args, ctx.Request); var request = new Request(Method.Parse(ctx.Request.Method), ctx.Request.Uri) { }; var response = await requestHandler(request, bindModel); var wrote = await response.Match <Task <bool> >( methodNotAllowed => Task.FromResult(false), notFoundResponse => Task.FromResult(false), modelBindingFailedResponse => Task.FromResult(false), async representationResponse => { await ResponseWriter.Write(ctx, representationResponse.Representation, termUriFinder, representors); return(true); }, async createdResponse => { await ResponseWriter.Write(ctx, createdResponse.Representation, termUriFinder, representors); return(true); }); }
public static Func <AppFunc, AppFunc> UseRepresentors <TRep>(Representor <TRep>[] representors, RequestHandler <TRep> requestHandler, FindUriForTerm termUriFinder, string basePath) { return(app => env => { var ctx = new OwinContext(env); if (!ctx.Request.Path.Value.StartsWith(basePath.ToString())) { return app(env); } return HandleRequest(representors, ctx, requestHandler, termUriFinder); }); }
public static IAppBuilder ExposeRouterAsHypermediaApi(this IAppBuilder appBuilder, Router <SemanticDocument> router, FindUriForTerm termUriFinder, Representor <SemanticDocument>[] representors, string basePath) { var poco = new RequestHandlerBuilder <SemanticDocument>(); var requestHandler = poco.MakeRequestHandler(new Uri(basePath, UriKind.Relative), router); return(appBuilder.Use(OwinInitializers.UseRepresentors(representors, requestHandler, termUriFinder, basePath))); }
public SirenDotNet.Entity BuildFromSemanticDocument(SemanticDocument semanticDocument, FindUriForTerm uriTermFinder) { var links = new List <SirenDotNet.Link>(); var actions = new List <Action>(); var subEntities = new List <SubEntity>(); var jo = new JObject(); string sirenTitle = null; foreach (var property in semanticDocument.Value) { property.Value.Switch(set => { if (property.Term.Means(TermFactory.From <Operation>())) { Func <SemanticProperty, Field> buildField = mp => { var fieldSet = mp.Value.AsT0; var fieldKind = fieldSet[TermFactory.From <Operation.FieldKind>()].Value.AsT2; var field = new Field() { Name = fieldSet[TermFactory.From <FieldName>()].Value.AsT1.Value <string>(), }; if (fieldKind == TermFactory.From <Operation.TextField>()) { field.Type = SirenDotNet.FieldTypes.Text; } else if (fieldKind == TermFactory.From <Operation.PasswordField>()) { field.Type = SirenDotNet.FieldTypes.Password; } else if (fieldKind == TermFactory.From <Operation.SelectField>()) { field.Type = new FieldTypes("select"); var options = fieldSet[TermFactory.From <Operation.Options>()].Value.AsT3; field.ExtensionData["options"] = options.Select(o => o.AsT0) .Select( o => new { name = o[TermFactory.From <DisplayText>()].Value.AsT1.Value <string>(), value = o[TermFactory.From <Operation.FieldValue>()].Value.AsT1.Value <string>() }) .Select(JObject.FromObject).ToArray(); } return(field); }; ; var href = new Uri(set[TermFactory.From <Operation.ActionUrl>()].Value.AsT1.Value <string>(), UriKind.Relative); var action = new SirenDotNet.Action(uriTermFinder(property.Term).ToString(), href) { Method = HttpVerbs.POST, Title = set[TermFactory.From <DisplayText>()].Value.AsT1.Value <string>(), Fields = set[TermFactory.From <Operation.Fields>()].Value.AsT0.Select(x => buildField(x)).ToArray() }; actions.Add(action); } }, value => { var isTitle = property.Term == Terms.Title; if (isTitle) { sirenTitle = value.ToObject <string>(); } else { jo[uriTermFinder(property.Term).ToString()] = value.ToString(); } }, term => { }, list => { if (property.Term == TermFactory.From <Vocab.Links>()) { foreach (var value in list) { var set = value.AsT0; var displayName = set[TermFactory.From <DisplayText>()].Value.AsT1.Value <string>(); Term rel = set[TermFactory.From <Vocab.Links.Rel>()].Value.AsT2; var href = new Uri(set[TermFactory.From <Vocab.Links.Href>()].Value.AsT1.Value <string>(), UriKind.Relative); var action = new SirenDotNet.Link(href, uriTermFinder(rel).ToString()) { Title = displayName }; links.Add(action); } } }); } if (sirenTitle == null) { throw new Exception("Title cannot be null for siren, attach a Term.Title property"); } var entity = new SirenDotNet.Entity { Title = sirenTitle, //Class = semanticDocument.Class?.Select(c => c.ToString()), Links = links.Any() ? links : null, Actions = actions.Any() ? actions : null, Entities = subEntities.Any() ? subEntities : null, Properties = jo.HasValues ? jo : null }; return(entity); }
public override Task <Tuple <string, string> > GetResponse(SemanticDocument hypermediaObject, FindUriForTerm termUriFinder) { var responseEntity = this.BuildFromSemanticDocument(hypermediaObject, termUriFinder); var serializerSettings = this.JsonSerializerSettings; var serializer = JsonSerializer.Create(serializerSettings); var objectAsJson = JToken.FromObject(responseEntity, serializer); return(Task.FromResult(Tuple.Create("application/vnd.siren+json", objectAsJson.ToString(Formatting.Indented)))); }