refact
This commit is contained in:
@ -9,75 +9,9 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace nuget_cli
|
namespace nuget_cli
|
||||||
{
|
{
|
||||||
public static class Helpers
|
public static class Constants
|
||||||
{
|
{
|
||||||
static readonly string clientVersion = "nuget_cli v1.0";
|
public const string ClientVersion = "nuget_cli v1.0";
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Creates HTTP POST request & uploads database to server. Author : Farhan Ghumra
|
|
||||||
/// </summary>
|
|
||||||
static internal async Task UploadFilesToServer(
|
|
||||||
this PushReport report, Uri uri,
|
|
||||||
FileInfo fi, string apikey)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
|
|
||||||
|
|
||||||
// using (MultipartContent content = new MultipartContent("ascasc"))
|
|
||||||
|
|
||||||
using (var formdata = new MultipartFormDataContent("NKdKd9Yk"))
|
|
||||||
{
|
|
||||||
using (HttpClient client = new HttpClient())
|
|
||||||
{
|
|
||||||
// client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
|
||||||
|
|
||||||
// client.DefaultRequestHeaders.Add("X-NuGet-Client-Version", clientVersion);
|
|
||||||
// client.DefaultRequestHeaders.Add("X-NuGet-ApiKey", apikey);
|
|
||||||
var dispo = new ContentDispositionHeaderValue("file");
|
|
||||||
dispo.FileName = fi.Name;
|
|
||||||
dispo.CreationDate = fi.CreationTime;
|
|
||||||
dispo.DispositionType = "form-data";
|
|
||||||
dispo.Size = fi.Length;
|
|
||||||
dispo.ModificationDate = fi.LastAccessTime;
|
|
||||||
|
|
||||||
Stream fileStream = fi.OpenRead();
|
|
||||||
var streamcontent = new StreamContent(fileStream);
|
|
||||||
streamcontent.Headers.ContentDisposition = dispo;
|
|
||||||
formdata.Add(streamcontent, fi.Name, fi.Name);
|
|
||||||
|
|
||||||
// content.Add(formdata);
|
|
||||||
client.BaseAddress = uri;
|
|
||||||
HttpRequestMessage put = new HttpRequestMessage(HttpMethod.Put, uri)
|
|
||||||
{
|
|
||||||
Content = formdata
|
|
||||||
};
|
|
||||||
put.Headers.Add("X-NuGet-Client-Version", clientVersion);
|
|
||||||
put.Headers.Add("X-NuGet-ApiKey", apikey);
|
|
||||||
put.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
|
||||||
|
|
||||||
HttpResponseMessage response = await client.SendAsync(put);
|
|
||||||
|
|
||||||
response.EnsureSuccessStatusCode();
|
|
||||||
report.StatusCode = response.StatusCode.ToString();
|
|
||||||
var respstream = await response.Content.ReadAsStreamAsync();
|
|
||||||
var sr = new StreamReader(respstream);
|
|
||||||
report.Message = await sr.ReadToEndAsync();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
catch (Exception rex)
|
|
||||||
{
|
|
||||||
report.Message = rex.Message;
|
|
||||||
report.StatusCode = "internal error";
|
|
||||||
Console.Error.WriteLine(rex.Message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ namespace nuget_cli
|
|||||||
|
|
||||||
partial class Program
|
partial class Program
|
||||||
{
|
{
|
||||||
|
static readonly ServerQueryHandler serverQueryHandler;
|
||||||
|
|
||||||
private static void SourceList(IEnumerable<string> sargs)
|
private static void SourceList(IEnumerable<string> sargs)
|
||||||
{
|
{
|
||||||
@ -29,7 +30,7 @@ namespace nuget_cli
|
|||||||
|
|
||||||
foreach (string pkg in pkgs)
|
foreach (string pkg in pkgs)
|
||||||
{
|
{
|
||||||
var report = await PushCommand.RunAsync(pkg,source,apiKey);
|
var report = await PushCommand.RunAsync(serverQueryHandler,pkg,source,apiKey);
|
||||||
|
|
||||||
pushReports.Add(report);
|
pushReports.Add(report);
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,11 @@ namespace nuget_cli
|
|||||||
private static string apiKey = null;
|
private static string apiKey = null;
|
||||||
private static string source = null;
|
private static string source = null;
|
||||||
|
|
||||||
|
static Program()
|
||||||
|
{
|
||||||
|
serverQueryHandler = new ServerQueryHandler();
|
||||||
|
}
|
||||||
|
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -68,6 +73,7 @@ namespace nuget_cli
|
|||||||
|
|
||||||
var push = new Command("push")
|
var push = new Command("push")
|
||||||
{
|
{
|
||||||
|
|
||||||
Run = async sargs =>
|
Run = async sargs =>
|
||||||
{
|
{
|
||||||
var pargs = pushoptions.Parse(sargs);
|
var pargs = pushoptions.Parse(sargs);
|
||||||
@ -114,6 +120,7 @@ namespace nuget_cli
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
commandSet.Run(args);
|
commandSet.Run(args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ namespace nuget_cli
|
|||||||
{
|
{
|
||||||
internal class PushCommand
|
internal class PushCommand
|
||||||
{
|
{
|
||||||
static internal async Task<PushReport> RunAsync(string pkg, string source, string apikey)
|
static internal async Task<PushReport> RunAsync(ServerQueryHandler queryHandler, string pkg, string source, string apikey)
|
||||||
{
|
{
|
||||||
FileInfo fi = new FileInfo(pkg);
|
FileInfo fi = new FileInfo(pkg);
|
||||||
var report = new PushReport
|
var report = new PushReport
|
||||||
@ -25,7 +25,7 @@ namespace nuget_cli
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await report.UploadFilesToServer(new Uri(source), fi, apikey);
|
await queryHandler.UploadFilesToServer(report, new Uri(source), fi, apikey);
|
||||||
}
|
}
|
||||||
catch (WebException ex)
|
catch (WebException ex)
|
||||||
{
|
{
|
||||||
|
70
src/nuget-cli/UploadFilesToServerUsingHttpClient.cs
Normal file
70
src/nuget-cli/UploadFilesToServerUsingHttpClient.cs
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace nuget_cli
|
||||||
|
{
|
||||||
|
public class HttpClientServerQueryHandler
|
||||||
|
{
|
||||||
|
internal async Task UploadFilesToServerUsingHttpClient(
|
||||||
|
PushReport report, Uri uri,
|
||||||
|
FileInfo fi, string apikey)
|
||||||
|
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
|
||||||
|
|
||||||
|
// using (MultipartContent content = new MultipartContent("ascasc"))
|
||||||
|
|
||||||
|
using (var formdata = new MultipartFormDataContent("NKdKd9Yk"))
|
||||||
|
{
|
||||||
|
using (HttpClient client = new HttpClient())
|
||||||
|
{
|
||||||
|
var dispo = new ContentDispositionHeaderValue("file")
|
||||||
|
{
|
||||||
|
FileName = fi.Name,
|
||||||
|
CreationDate = fi.CreationTime,
|
||||||
|
DispositionType = "form-data",
|
||||||
|
Size = fi.Length,
|
||||||
|
ModificationDate = fi.LastAccessTime
|
||||||
|
};
|
||||||
|
|
||||||
|
Stream fileStream = fi.OpenRead();
|
||||||
|
var streamcontent = new StreamContent(fileStream);
|
||||||
|
streamcontent.Headers.ContentDisposition = dispo;
|
||||||
|
formdata.Add(streamcontent, fi.Name, fi.Name);
|
||||||
|
|
||||||
|
// content.Add(formdata);
|
||||||
|
client.BaseAddress = uri;
|
||||||
|
HttpRequestMessage put = new HttpRequestMessage(HttpMethod.Put, uri)
|
||||||
|
{
|
||||||
|
Content = formdata
|
||||||
|
};
|
||||||
|
put.Headers.Add("X-NuGet-Client-Version", Constants.ClientVersion);
|
||||||
|
put.Headers.Add("X-NuGet-ApiKey", apikey);
|
||||||
|
put.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||||
|
|
||||||
|
HttpResponseMessage response = await client.SendAsync(put);
|
||||||
|
|
||||||
|
response.EnsureSuccessStatusCode();
|
||||||
|
report.StatusCode = response.StatusCode.ToString();
|
||||||
|
var respstream = await response.Content.ReadAsStreamAsync();
|
||||||
|
var sr = new StreamReader(respstream);
|
||||||
|
report.Message = await sr.ReadToEndAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception rex)
|
||||||
|
{
|
||||||
|
report.Message = rex.Message;
|
||||||
|
report.StatusCode = "internal error";
|
||||||
|
Console.Error.WriteLine(rex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user