Created
April 1, 2018 14:46
-
-
Save justinyoo/dec45c3ddc6642d06a0cd84f1737714a to your computer and use it in GitHub Desktop.
Dependency Injections on Azure Functions V2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IFunction | |
{ | |
Task<TOutput> InvokeAsync<TInput, TOutput>(TInput input); | |
} | |
public interface IGitHubRepositoriesFunction : IFunction | |
{ | |
} | |
public class CoreGitHubRepositoriesFunction : IGitHubRepositoriesFunction | |
{ | |
private readonly GitHub _github; | |
private readonly HttpClient _httpClient; | |
public CoreGitHubRepositoriesFunction(GitHub github, HttpClient httpClient) | |
{ | |
this._github = github; | |
this._httpClient = httpClient; | |
} | |
public async Task<TOutput> InvokeAsync<TInput, TOutput>(TInput input) | |
{ | |
var req = input as HttpRequest; | |
var type = req.Query["type"]; | |
var name = req.Query["name"]; | |
var requestUrl = $"{this._github.BaseUrl}{string.Format(this._github.Endpoints.Repositories, type, name)}"; | |
using (var message = await this._httpClient.GetAsync(requestUrl).ConfigureAwait(false)) | |
{ | |
var result = await message.Content.ReadAsStringAsync().ConfigureAwait(false); | |
var res = JsonConvert.DeserializeObject<object>(result); | |
return (TOutput)res; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment