public void CreateCodeSearchRequest() { var link = new CodeSearchLink(); var request = link.CreateRequest(); Assert.Equal(HttpMethod.Get, request.Method); Assert.Null(request.RequestUri); // URL provided by hypermedia }
public void CreateCodeSearchRequestWithUriTemplate() { var link = new CodeSearchLink() { Template = new UriTemplate("http://api.github.com/search{?query,sort,page,per_page}"), Query = "panic" }; var request = link.CreateRequest(); Assert.Equal(HttpMethod.Get, request.Method); Assert.Equal("http://api.github.com/search?query=panic", request.RequestUri.AbsoluteUri); // URL provided by hypermedia }
private static async Task CodeSearchUsingLink() { var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders .UserAgent.Add(new ProductInfoHeaderValue("DarrelsGitClient", "1.0")); // API Specific type acts as Request Factory var codeSearchLink = new CodeSearchLink() { Template = new UriTemplate("https://api.github.com/search/code?q={query}{&page,per_page,sort_order}"), Query = ".Result user:darrelmiller" }; var requestMessage = codeSearchLink.CreateRequest(); var responseMessage = await httpClient.SendAsync(requestMessage); // ReadAs extension takes advantage of media type defined by API var githubdoc = await responseMessage.Content.ReadAsGithubDocumentAsync(); // API Specific Link Type knows how to interpret generic media type var searchResults = CodeSearchLink.InterpretMessageBody(githubdoc); foreach (var result in searchResults.Items) { Console.WriteLine(result.Path); } // Harvest strongly typed links from strongly typed searchresults var fileLinks = searchResults.Items .Where(i => i.Name.Contains("Slack") && i.CodeFileLink != null) .Select(i => i.CodeFileLink); foreach (var fileLink in fileLinks) { // Following Links is a way of navigating the API's object graph var fileResponse = await httpClient.FollowLinkAsync(fileLink); // Reuse media type parser var fileDoc = await fileResponse.Content.ReadAsGithubDocumentAsync(); // Use Type Specific Link to interpret results var codeFile = CodeFileLink.InterpretMessageBody(fileDoc); Console.WriteLine(codeFile.Content); } }