Skip to content

Instantly share code, notes, and snippets.

@justinyoo
Created April 1, 2018 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinyoo/dec45c3ddc6642d06a0cd84f1737714a to your computer and use it in GitHub Desktop.
Save justinyoo/dec45c3ddc6642d06a0cd84f1737714a to your computer and use it in GitHub Desktop.
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