Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dependency Injections on Azure Functions V2
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