🥺
This commit is contained in:
parent
4279114543
commit
1a14beda3a
3 changed files with 47 additions and 3 deletions
|
@ -1,9 +1,10 @@
|
|||
using System.Net;
|
||||
|
||||
namespace Nyanlabs.Pubresolv;
|
||||
namespace Nyanlabs.Pubresolv.Access;
|
||||
|
||||
public interface IResolver
|
||||
public interface IAccessor
|
||||
{
|
||||
public Task Resolve(Doi doi);
|
||||
public string Host { get; }
|
||||
public Task<bool> Authenticate(ICredentials credentials);
|
||||
}
|
41
src/Nyanlabs.Pubresolv/DocumentResolver.cs
Normal file
41
src/Nyanlabs.Pubresolv/DocumentResolver.cs
Normal 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();
|
||||
}
|
||||
}
|
|
@ -1,8 +1,10 @@
|
|||
using Nyanlabs.Pubresolv.Access;
|
||||
|
||||
namespace Nyanlabs.Pubresolv;
|
||||
|
||||
public class Pubresolv
|
||||
{
|
||||
public ICollection<IResolver> GetResolvers()
|
||||
public static ICollection<IAccessor> GetResolvers()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue