This commit is contained in:
femsci 2024-05-06 12:40:10 +02:00
parent 4279114543
commit 1a14beda3a
Signed by: femsci
GPG key ID: 08F7911F0E650C67
3 changed files with 47 additions and 3 deletions

View file

@ -1,9 +1,10 @@
using System.Net; using System.Net;
namespace Nyanlabs.Pubresolv; namespace Nyanlabs.Pubresolv.Access;
public interface IResolver public interface IAccessor
{ {
public Task Resolve(Doi doi); public Task Resolve(Doi doi);
public string Host { get; }
public Task<bool> Authenticate(ICredentials credentials); public Task<bool> Authenticate(ICredentials credentials);
} }

View file

@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Nyanlabs.Pubresolv.Access;
namespace Nyanlabs.Pubresolv;
public class DocumentResolver : IDisposable
{
private readonly HttpClient _cli = new();
public async Task<IAccessor> Resolve(Doi doi)
{
var resp = await _cli.GetAsync(new Uri($"https://doi.org/{doi}"));
resp.EnsureSuccessStatusCode();
Uri? redirect = resp.Headers.Location ?? throw new DataException("Cannot resolve DOI.");
return Resolve(redirect);
}
public IAccessor Resolve(Uri uri)
{
string host = uri.Host;
var accessor = Pubresolv.GetResolvers().SingleOrDefault(s => s.Host.Equals(host, StringComparison.OrdinalIgnoreCase));
if (accessor is null)
{
throw new DataException("This URI does not have a bound accessor.");
}
return accessor;
}
public void Dispose()
{
GC.SuppressFinalize(this);
_cli.Dispose();
}
}

View file

@ -1,8 +1,10 @@
using Nyanlabs.Pubresolv.Access;
namespace Nyanlabs.Pubresolv; namespace Nyanlabs.Pubresolv;
public class Pubresolv public class Pubresolv
{ {
public ICollection<IResolver> GetResolvers() public static ICollection<IAccessor> GetResolvers()
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }