radiostasis/api/B2ApiClient.cs

30 lines
903 B
C#
Raw Normal View History

2023-03-26 11:37:20 -05:00
using B2Net;
namespace Api;
public class EpisodeUrl {
public required string Url { get; set; }
public required string Token { get; set; }
}
public class B2ApiClient {
private static string? KeyId => Environment.GetEnvironmentVariable("KEY_ID");
private static string? Key => Environment.GetEnvironmentVariable("SECRET_KEY");
private static string? BucketId => Environment.GetEnvironmentVariable("BUCKET_ID");
private static string? Bucket => Environment.GetEnvironmentVariable("BUCKET");
private readonly B2Client _client;
public B2ApiClient() {
_client = new B2Client(KeyId, Key);
}
public async Task<EpisodeUrl> GetDownloadLink(string file) {
var auth = await _client.Files.GetDownloadAuthorization(file, 60, BucketId);
return new EpisodeUrl {
Url = _client.Files.GetFriendlyDownloadUrl(file, Bucket),
Token = auth.AuthorizationToken,
};
}
}