Created
December 16, 2016 17:19
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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