-
Notifications
You must be signed in to change notification settings - Fork 0
Solid.Http.Extensions.SharpCompress Usage
HX-Rd edited this page May 30, 2018
·
7 revisions
Here is a small example on how to get a ZipArhive and read some of its contents. In this example we are going to call nuget.org. The nuget packages are zip files but do not have a zip ending so they do not have the default mimetype. Lets first start by setting up the startup class and then call the nuget enpoint from a controller.
public class Startup
{
public Startup(IConfiguration configuration)
{
_configuration = configuration;
}
public IConfiguration _configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services
.AddSolidHttpCore()
.AddSharpCompress(
new SharpCompressOptions(
mimeTypes: new List<string>()
{
"binary/octet-stream"
}
)
);
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
}
}Now lets call the nuget endpoint, get a ZipArchive and read the nuspec file
public class NuspecController : Controller
{
private SolidHttpClient _client;
public NuspecController(ISolidHttpClientFactory factory)
{
_client = factory.CreateWithBaseAddress("https://www.nuget.org");
}
[HttpGet]
public async Task GetAsync(string packageName, string versionNumber)
{
var arch = await _client
.GetAsync($"api/v2/package/{packageName}/{versionNumber}")
.As<ZipArchive>();
using (var estream = arch.Entries.Single(e => e.Key == $"{packageName}.nuspec").OpenEntryStream())
using (var reader = new StreamReader(estream))
{
var nuspecContent = reader.ReadToEnd();
return Content(nuspecContent, "application/xml");
}
}
}