You need to install the NuGet Package to use the library. You can use on of the following ways are install it through Visual Studio.
Install-Package WaybackMachine.DotNet.Client
dotnet add package WaybackMachine.DotNet.Client
The easiest way to use the library is by using dependency injection. In the following sections you can see the easiest ways to use the library.
- Register the WaybackMachine.DotNet.Client interface in the startup file
- Inject the service in the class where you want to use it
- See full example in Example folder
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
// Register the wayback machine client
services.AddSingleton<IWaybackMachineService, WaybackMachineService>();
}
namespace RazorPages.Example.Pages
{
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
private readonly IWaybackMachineService _waybackMachineService;
public IndexModel(
ILogger<IndexModel> logger,
IWaybackMachineService waybackMachineService
)
{
_logger = logger;
_waybackMachineService = waybackMachineService;
}
public Snapshot Snapshot { get; set; }
public async Task OnGet()
{
Snapshot = await _waybackMachineService.GetMostRecentSnapshotAsync("google.com");
}
}
}
The pattern for using dependency injection in an Azure Function is similar to a web application.
- Create a startup.cs file to enable dependency injection
- Register the WaybackMachine.DotNet.Client interface in the startup file
- Inject the service in the class where you want to use it
- See full example in Example folder
[assembly: FunctionsStartup(typeof(Azure.Function.Example.Startup))]
namespace Azure.Function.Example
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddHttpClient();
builder.Services.AddLogging();
// Register the wayback machine client
builder.Services.AddSingleton<IWaybackMachineService, WaybackMachineService>();
}
}
}
public class GetSnapshotFunction
{
private readonly IWaybackMachineService _waybackMachineService;
public GetSnapshotFunction(IWaybackMachineService waybackMachineService)
{
_waybackMachineService = waybackMachineService;
}
[FunctionName(nameof(GetSnapshotFunction))]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string url = req.Query["url"];
var snapshot = await _waybackMachineService.GetMostRecentSnapshotAsync(url);
return snapshot != null
? (ActionResult)new OkObjectResult(snapshot)
: new BadRequestObjectResult("Please pass a url on the query string or in the request body");
}
}
The project is developed as a .NET Core Class Library. The current framework version used is .NET Core 3.1.
To start developing, you need to clone the repo on your local workstation.
dotnet restore
dotnet run
The project is using XUnit for testing.
dotnet test
This will start up the development server allowing you to see the results. Be aware that the solution is setting a cookie and this cookie will be stored in your browser. In order to see the banner again, you will need to open the localhost link in incognito or clear your browser cookies.
Your contributions are always welcome! Please have a look at the contribution guidelines first 🎉
MIT © kristofferandreasen