private static HomeDocument Parse(JObject jObject, LinkFactory linkFactory = null) { if (linkFactory == null) { linkFactory = new LinkFactory(); } var doc = new HomeDocument(); var resources = jObject["resources"] as JObject; if (resources != null) { foreach (var resourceProp in resources.Properties()) { var link = linkFactory.CreateLink(resourceProp.Name); var resource = resourceProp.Value as JObject; var hrefProp = resource.Property("href"); if (hrefProp != null) { link.Target = new Uri(hrefProp.Value.Value <string>(), UriKind.RelativeOrAbsolute); } else { link.Target = new Uri((String)resource["href-template"], UriKind.RelativeOrAbsolute); var hrefvars = resource.Value <JObject>("href-vars"); foreach (var hrefvar in hrefvars.Properties()) { var hrefUri = (string)hrefvar; if (string.IsNullOrEmpty(hrefUri)) { link.SetParameter(hrefvar.Name, null); } else { link.SetParameter(hrefvar.Name, null, new Uri(hrefUri, UriKind.RelativeOrAbsolute)); } } } var hintsProp = resource.Property("hints"); if (hintsProp != null) { var hintsObject = hintsProp.Value as JObject; foreach (var hintProp in hintsObject.Properties()) { var hint = linkFactory.HintFactory.CreateHint(hintProp.Name); hint.Content = hintProp.Value; link.AddHint(hint); } } doc.AddResource(link); } } return(doc); }
private async Task DiscoverResources() { var response = await _HttpClient.GetAsync("/"); _homeDocument = HomeDocument.Parse(await response.Content.ReadAsStreamAsync()); SpeakersLink = _homeDocument.GetResource<SpeakersLink>(); SessionsLink = _homeDocument.GetResource<SessionsLink>(); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="homeDocument"></param> /// <param name="configureLink"></param> public static void AddResource <T>(this HomeDocument homeDocument, Action <T> configureLink) where T : Link, new() { var link = new T(); configureLink(link); homeDocument.AddResource(link); }
private async Task DiscoverResources() { var response = await _HttpClient.GetAsync("/"); _homeDocument = HomeDocument.Parse(await response.Content.ReadAsStreamAsync()); AllSpeakers = _homeDocument.GetResource(LinkHelper.GetLinkRelationTypeName<SpeakersLink>()).Target.OriginalString; AllSessions = _homeDocument.GetResource(LinkHelper.GetLinkRelationTypeName<SessionsLink>()).Target.OriginalString; }
public void RoundTripHomeDocument() { var doc = new HomeDocument(); doc.AddResource(new AboutLink() {Target = new Uri("about",UriKind.Relative)}); var ms = new MemoryStream(); doc.Save(ms); ms.Position = 0; var outDoc = HomeDocument.Parse(ms); Assert.NotNull(outDoc.GetResource("about")); }
public IHttpActionResult Get() { var home = new HomeDocument(); home.AddResource(TopicsLinkHelper.CreateLink(Request).WithHints()); home.AddResource(DaysLinkHelper.CreateLink(Request).WithHints()); home.AddResource(SessionsLinkHelper.CreateLink(Request).WithHints()); home.AddResource(SpeakersLinkHelper.CreateLink(Request).WithHints()); return new OkResult(Request) .WithCaching(new CacheControlHeaderValue() { MaxAge = new TimeSpan(0, 0, 60) }) .WithContent(new HomeContent(home)); }
public HttpResponseMessage Get(HttpRequestMessage requestMessage) { var homeDocument = new HomeDocument(); var fooLink = new Link() { Relation = "http://example.org/rels/foo", Target = new Uri("foo", UriKind.Relative) }; var allowHint = new AllowHint(); allowHint.AddMethod(HttpMethod.Get); allowHint.AddMethod(HttpMethod.Post); fooLink.AddHint(allowHint); homeDocument.AddResource(fooLink); var barLink = new Link() { Relation = "http://example.org/rels/bar", Target = new Uri("bar", UriKind.Relative) }; var allowHint2 = new AllowHint(); allowHint2.AddMethod(HttpMethod.Get); barLink.AddHint(allowHint2); homeDocument.AddResource(barLink); var bar2Link = new Link() { Relation = "http://example.org/rels/bar2", Target = new Uri("bar/{id}", UriKind.Relative) }; // bar2Link.SetParameter("id","",new Uri("template/params/id", UriKind.Relative)); homeDocument.AddResource(bar2Link); var ms = new MemoryStream(); homeDocument.Save(ms); ms.Position = 0; var streamContent = new StreamContent(ms); return new HttpResponseMessage() { Content = streamContent }; }
public ApiHomeController() { Document = new HomeDocument(); // Series var series = new Link() { Relation = Relations.SERIES_SINGLE, Target = new Uri("series/{id}", UriKind.Relative) }; series.SetParameter("id", "", Parameters.SERIES_ID); var allowedMethods = new AllowHint(); allowedMethods.AddMethod(HttpMethod.Get); allowedMethods.AddMethod(HttpMethod.Post); series.AddHint(allowedMethods); Document.AddResource(series); }
public void CreateResourceWithMultipleQueryParameters() { var doc = new HomeDocument(); doc.AddResource<Link>(l => { l.Relation = "vnd.foo.about"; l.Target = new Uri("http://example.org:1001/about{?id,name}"); }); var ms = new MemoryStream(); doc.Save(ms); ms.Position = 0; var st = new StreamReader(ms); var s = st.ReadToEnd(); Assert.True(s.Contains("href-template")); }
public void CreateResource_with_extension_rel_and_template() { var doc = new HomeDocument(); doc.AddResource<Link>(l => { l.Relation = "http://webapibook.net/rels#issue-processor"; l.Target = new Uri("/issueprocessor/{id}{?action}", UriKind.Relative); }); var ms = new MemoryStream(); doc.Save(ms); ms.Position = 0; var st = new StreamReader(ms); var s = st.ReadToEnd(); Assert.True(s.Contains("href-template")); }
public void CreateResourceWithPathParameter() { var doc = new HomeDocument(); doc.AddResource<AboutLink>(l => { l.Target = new Uri("http://example.org:1001/about/{id}"); }); doc.AddResource<HelpLink>(l => { l.Target = new Uri("http://example.org:1001/help/{id}"); }); var ms = new MemoryStream(); doc.Save(ms); ms.Position = 0; var st = new StreamReader(ms); var s = st.ReadToEnd(); Assert.True(s.Contains("href-template")); }
public void CreateHomeDocumentWithLotsOfHints() { var doc = new HomeDocument(); doc.AddResource<AboutLink>(l => { l.Target = new Uri("about", UriKind.Relative); l.AddHint<AllowHint>(h => h.AddMethod(HttpMethod.Get)); l.AddHint<FormatsHint>(h => h.AddMediaType("application/json")); l.AddHint<AcceptPostHint>(h => h.AddMediaType("application/vnd.tavis.foo+json")); l.AddHint<AcceptPreferHint>(h => h.AddPreference("handling")); }); var ms = new MemoryStream(); doc.Save(ms); ms.Position = 0; var st = new StreamReader(ms); var s = st.ReadToEnd(); Assert.NotNull(s); }
/// <summary> /// /// </summary> /// <param name="document"></param> public HomeContent(HomeDocument document) { document.Save(_memoryStream); _memoryStream.Position = 0; Headers.ContentType = new MediaTypeHeaderValue("application/home+json"); }
public HttpResponseMessage Get() { var home = new Tavis.Home.HomeDocument(); var issueLink = new Link() { Relation = IssueLinkFactory.Rels.Issue, Target = new Uri("/issue/{id}", UriKind.Relative) }; issueLink.AddHint <AllowHint>(h => h.AddMethod(HttpMethod.Get)); issueLink.AddHint <FormatsHint>(h => { h.AddMediaType("application/json"); h.AddMediaType("application/vnd.issue+json"); }); home.AddResource((Link)issueLink); var issuesLink = new Link() { Relation = IssueLinkFactory.Rels.Issues, Target = new Uri("/issue", UriKind.Relative) }; issuesLink.AddHint <AllowHint>(h => h.AddMethod(HttpMethod.Get)); issuesLink.AddHint <FormatsHint>(h => { h.AddMediaType("application/json"); h.AddMediaType("application/vnd.collection+json"); }); home.AddResource(issuesLink); var searchLink = new Link() { Relation = IssueLinkFactory.Rels.SearchQuery, Target = new Uri("/issue{?searchtext}", UriKind.Relative) }; searchLink.AddHint <AllowHint>(h => h.AddMethod(HttpMethod.Get)); searchLink.AddHint <FormatsHint>(h => { h.AddMediaType("application/json"); h.AddMediaType("application/vnd.collection+json"); }); home.AddResource(searchLink); var issueProcessorLink = new Link() { Relation = IssueLinkFactory.Rels.IssueProcessor, Target = new Uri("/issueprocessor/{id}{?action}", UriKind.Relative) }; issueProcessorLink.AddHint <AllowHint>(h => h.AddMethod(HttpMethod.Post)); home.AddResource(issueProcessorLink); return(new HttpResponseMessage() { RequestMessage = Request, Content = new HomeContent(home) }); }
private JsonHomeResult Home(HomeDocument doc) { return new JsonHomeResult(doc); }
public void RoundTripHomeDocumentWithHint() { var doc = new HomeDocument(); var aboutLink = new AboutLink() {Target = new Uri("about", UriKind.Relative)}; var allowHint = new AllowHint(); allowHint.AddMethod(HttpMethod.Get); aboutLink.AddHint(allowHint); aboutLink.AddHint(new FormatsHint()); doc.AddResource(aboutLink); var ms = new MemoryStream(); doc.Save(ms); ms.Position = 0; var outDoc = HomeDocument.Parse(ms); var link = outDoc.GetResource("about"); Assert.IsType<AboutLink>(link); Assert.IsType<AllowHint>(link.GetHints().First()); Assert.IsType<FormatsHint>(link.GetHints().Last()); }
public JsonHomeResult(HomeDocument document) : base(new HttpResponseMessage(HttpStatusCode.OK) { Content = new HomeContent(document) }) { }
public void CreateHomeDocumentWithFormatsHints() { var doc = new HomeDocument(); var aboutLink = new AboutLink() { Target = new Uri("about", UriKind.Relative) }; aboutLink.AddHint<AllowHint>(h => h.AddMethod(HttpMethod.Get)); aboutLink.AddHint<FormatsHint>(h => h.AddMediaType("application/json")); doc.AddResource(aboutLink); var ms = new MemoryStream(); doc.Save(ms); ms.Position = 0; var outDoc = HomeDocument.Parse(ms); var link = outDoc.GetResource("about"); Assert.IsType<AboutLink>(link); Assert.IsType<AllowHint>(link.GetHints().First()); Assert.IsType<FormatsHint>(link.GetHints().Last()); Assert.IsType<AboutLink>(outDoc.Resources.First()); }
private static HomeDocument Parse(JObject jObject, LinkFactory linkFactory = null) { if (linkFactory == null) linkFactory = new LinkFactory(); var doc = new HomeDocument(); var resources = jObject["resources"] as JObject; if (resources != null) { foreach (var resourceProp in resources.Properties()) { var link = linkFactory.CreateLink(resourceProp.Name); var resource = resourceProp.Value as JObject; var hrefProp = resource.Property("href"); if (hrefProp != null) { link.Target = new Uri(hrefProp.Value.Value<string>(), UriKind.RelativeOrAbsolute); } else { link.Target = new Uri((String)resource["href-template"], UriKind.RelativeOrAbsolute); var hrefvars = resource.Value<JObject>("href-vars"); foreach (var hrefvar in hrefvars.Properties()) { var hrefUri = (string)hrefvar; if (string.IsNullOrEmpty(hrefUri)) { link.SetParameter(hrefvar.Name, null); } else { link.SetParameter(hrefvar.Name, null, new Uri(hrefUri, UriKind.RelativeOrAbsolute)); } } } var hintsProp = resource.Property("hints"); if (hintsProp != null) { var hintsObject = hintsProp.Value as JObject; foreach (var hintProp in hintsObject.Properties()) { var hint = linkFactory.HintFactory.CreateHint(hintProp.Name); hint.Content = hintProp.Value; link.AddHint(hint); } } doc.AddResource(link); } } return doc; }