Skip to content

Instantly share code, notes, and snippets.

@agc93
Created December 16, 2016 17:19
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 agc93/f3a2ce2f515faf67e6fb7f16c7eedecf to your computer and use it in GitHub Desktop.
Save agc93/f3a2ce2f515faf67e6fb7f16c7eedecf to your computer and use it in GitHub Desktop.
A simple Cake script to upload packages to MyGet using the .NET HttpClient
#addin "System.Net.Http"
using System.Net.Http;
public class MyGetClient : HttpClient
{
public string ApiKey { get; set; }
public Uri FeedUri { get; set; }
private Action<string> Log { get; set; }
public static MyGetClient GetClient(string uri, string key)
{
return GetClient(uri, key, s => { });
}
public static MyGetClient GetClient(string uri, string key, Action<string> log) {
return new MyGetClient
{
FeedUri = uri.TrimEnd('/').EndsWith("/upload")
? new Uri(uri)
: new Uri(uri.TrimEnd('/') + "/upload"),
ApiKey = key,
Log = log
};
}
public HttpResponseMessage UploadVsix(IFile file)
{
Log = Log ?? (s => { });
using (var content = new StreamContent(file.Open(FileMode.Open, FileAccess.Read, FileShare.Read)))
{
DefaultRequestHeaders.Add("X-NuGet-ApiKey", ApiKey);
Log.Invoke(string.Format("Issuing POST request to {0}", FeedUri));
using (var message =
PostAsync(FeedUri, content).Result)
{
return message;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment