Skip to content

kristofferandreasen/wayback-machine-dotnet-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Wayback Machine C# .NET Core SDK/Client

💾 Wayback Machine C# .NET Rest Client 💾

This API wrapper is built to make it easier to use the Wayback Machine API from Internet Archive. It provides a simple interface to get snapshots from websites back in time.

A simple C# .NET Core rest client for the Wayback Machine API. Built with ❤︎ by Kristoffer Andreasen

Installation

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 with Package Manager

Install-Package WaybackMachine.DotNet.Client

Install with .NET CLI

dotnet add package WaybackMachine.DotNet.Client

Using the package

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.

Dependency Injection: .NET Core Web Application

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");
        }
    }
}

Dependency Injection: Azure Function

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

Startup.cs file

[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>();
        }
    }
}

GetSnapshotFunction.cs file

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");
    }
}

Developing

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.

Restore Dependencies

dotnet restore

Run Project

dotnet run

Test Project

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.

Contributing

Your contributions are always welcome! Please have a look at the contribution guidelines first 🎉

License

MIT © kristofferandreasen

About

A .NET Rest client for the Internet Archive Wayback Machine API using C# and .NET Core. You can use this library to access website snapshots from the past 💾

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages