Skip to content

Instantly share code, notes, and snippets.

@markekraus
Created March 31, 2018 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markekraus/61016e81bde2198cdc4faf330c1e80d9 to your computer and use it in GitHub Desktop.
Save markekraus/61016e81bde2198cdc4faf330c1e80d9 to your computer and use it in GitHub Desktop.
Demo of the Download Cmdlet Interfaces and a fake mock up the the cmdlet and how it would use them
using System;
using System.Collections.Generic;
using System.IO;
using System.Management.Automation;
using System.Text;
namespace System.Management.Automation
{
internal interface IInvokeDownloadProtocolFactory
{
IInvokeDownloadProtocol NewInvokeDownloadProtocol();
}
internal interface IInvokeDownloadProtocol
{
Uri RequestUri { get; set; }
FileInfo TargetFile { get; set; }
PSCmdlet Cmdlet { get; set; }
Stream GetStream();
}
internal interface IInvokeDownloadResumable
{
bool Resume { get; set; }
bool IsResumeSuccessful();
bool IsResumeComplete();
}
internal interface IInvokeDownloadAuthenticator
{
PSCredential Credential { get; set; }
}
internal interface IInvokeDownloadProxyable
{
Uri ProxyUri { get; set; }
PSCredential ProxyCredential { get; set; }
}
internal interface IInvokeDownloadRedirectable : IInvokeDownloadProtocol
{
IList<Uri> GetRedirectionChain();
}
internal class InvokeDownloadHttpHandler : IInvokeDownloadAuthenticator, IInvokeDownloadProtocol, IInvokeDownloadProxyable, IInvokeDownloadRedirectable, IInvokeDownloadResumable
{
public PSCredential Credential { get; set; }
public Uri RequestUri { get; set; }
public FileInfo TargetFile { get; set; }
public PSCmdlet Cmdlet { get; set; }
public Uri ProxyUri { get; set; }
public PSCredential ProxyCredential { get; set; }
public bool Resume { get; set; }
private bool isResumeSuccessful = false;
private bool isResumeComplete = false;
public IList<Uri> GetRedirectionChain()
{
var result = new List<Uri>();
result.Add(RequestUri);
return result;
}
public Stream GetStream()
{
var stream = new MemoryStream();
var writer = new StreamWriter(stream, Encoding.UTF8, 8, true);
writer.WriteLine("This is a test");
writer.Flush();
writer.Dispose();
stream.Position = 0;
return stream;
}
public bool IsResumeComplete()
{
return isResumeComplete;
}
public bool IsResumeSuccessful()
{
return isResumeSuccessful;
}
}
internal class InvokeDownloadHttpHandlerFactory : IInvokeDownloadProtocolFactory
{
public IInvokeDownloadProtocol NewInvokeDownloadProtocol()
{
return new InvokeDownloadHttpHandler();
}
}
public class InvokeDownloadCmdlet : PSCmdlet
{
static Dictionary<string,IInvokeDownloadProtocolFactory> ProtocolHandlers;
protected override void ProcessRecord()
{
if (ProtocolHandlers == null)
{
ProtocolHandlers = new Dictionary<string,IInvokeDownloadProtocolFactory>()
{
{"http", new InvokeDownloadHttpHandlerFactory()},
{"https", new InvokeDownloadHttpHandlerFactory()}
};
}
Uri requestUri = new Uri("https://i.imgur.com/gXnRf4J.jpg");
IInvokeDownloadProtocolFactory factory;
if (! ProtocolHandlers.TryGetValue(requestUri.Scheme, out factory))
{
throw new ArgumentException();
}
var handler = factory.NewInvokeDownloadProtocol();
handler.RequestUri = requestUri;
handler.TargetFile = new FileInfo(@"c:\temp\test.txt");
if (handler is IInvokeDownloadAuthenticator auth)
{
auth.Credential = PSCredential.Empty;
}
if (handler is IInvokeDownloadProxyable proxy)
{
proxy.ProxyUri = new Uri("http://someproxy.com:8080/");
proxy.ProxyCredential = PSCredential.Empty;
}
if(handler is IInvokeDownloadRedirectable redirect)
{
var chain = redirect.GetRedirectionChain();
handler.RequestUri = chain[chain.Count - 1];
}
if (handler is IInvokeDownloadResumable resume)
{
resume.Resume = true;
}
var response = handler.GetStream();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment