ex to output
This commit is contained in:
8
.build
8
.build
@ -16,9 +16,9 @@
|
||||
<delete dir="test/isnd.tests/obj/" failonerror="false" />
|
||||
</target>
|
||||
<target name="build" description="build all">
|
||||
<solution configuration="release">
|
||||
<referenceprojects>
|
||||
</referenceprojects>
|
||||
</solution>
|
||||
<exec program="dotnet" commandline="build" />
|
||||
</target>
|
||||
<target name="test" description="build all">
|
||||
<exec program="dotnet" commandline="test" />
|
||||
</target>
|
||||
</project>
|
||||
|
@ -9,43 +9,50 @@ namespace isn
|
||||
{
|
||||
public static class UploadFilesToServerUsingHttpClient
|
||||
{
|
||||
public static PushReport UploadFilesToServer(Uri uri, FileInfo fi,
|
||||
public static PushReport UploadFilesToServer(this HttpClient client, Uri uri, FileInfo fi,
|
||||
string apikey)
|
||||
{
|
||||
return UploadFilesToServerAsync(client, uri, fi, apikey).Result;
|
||||
}
|
||||
|
||||
public static async Task<PushReport> UploadFilesToServerAsync(this HttpClient client, Uri uri, FileInfo fi,
|
||||
string apikey)
|
||||
{
|
||||
using (var client = new HttpClient())
|
||||
|
||||
client.DefaultRequestHeaders.Add("X-NuGet-Client-Version", Constants.ClientVersion);
|
||||
client.DefaultRequestHeaders.Add("X-NuGet-ApiKey", apikey);
|
||||
|
||||
using (var multipartFormDataContent = new MultipartFormDataContent())
|
||||
{
|
||||
client.DefaultRequestHeaders.Add("X-NuGet-Client-Version", Constants.ClientVersion);
|
||||
client.DefaultRequestHeaders.Add("X-NuGet-ApiKey", apikey);
|
||||
using (var multipartFormDataContent = new MultipartFormDataContent())
|
||||
/* var values = new[]
|
||||
{
|
||||
new KeyValuePair<string, string>("Id", Guid.NewGuid().ToString()),
|
||||
new KeyValuePair<string, string>("Key", "awesome"),
|
||||
new KeyValuePair<string, string>("From", "khalid@home.com")
|
||||
//other values
|
||||
};foreach (var keyValuePair in values)
|
||||
{
|
||||
multipartFormDataContent.Add(new StringContent(keyValuePair.Value),
|
||||
String.Format("\"{0}\"", keyValuePair.Key));
|
||||
} */
|
||||
multipartFormDataContent.Add(new ByteArrayContent(File.ReadAllBytes(fi.FullName)),
|
||||
'"' + "File" + '"',
|
||||
'"' + fi.Name + '"');
|
||||
|
||||
var result = await client.PutAsync(uri, multipartFormDataContent);
|
||||
|
||||
if (result.IsSuccessStatusCode)
|
||||
{
|
||||
/* var values = new[]
|
||||
{
|
||||
new KeyValuePair<string, string>("Id", Guid.NewGuid().ToString()),
|
||||
new KeyValuePair<string, string>("Key", "awesome"),
|
||||
new KeyValuePair<string, string>("From", "khalid@home.com")
|
||||
//other values
|
||||
};foreach (var keyValuePair in values)
|
||||
{
|
||||
multipartFormDataContent.Add(new StringContent(keyValuePair.Value),
|
||||
String.Format("\"{0}\"", keyValuePair.Key));
|
||||
} */
|
||||
multipartFormDataContent.Add(new ByteArrayContent(File.ReadAllBytes(fi.FullName)),
|
||||
'"' + "File" + '"',
|
||||
'"' + fi.Name + '"');
|
||||
string report = await result.Content.ReadAsStringAsync();
|
||||
Console.WriteLine(report);
|
||||
|
||||
var result = client.PutAsync(uri, multipartFormDataContent).Result;
|
||||
result.EnsureSuccessStatusCode();
|
||||
if (result.IsSuccessStatusCode)
|
||||
{
|
||||
Task.Run(async ()=>
|
||||
{
|
||||
string report = await result.Content.ReadAsStringAsync();
|
||||
Console.WriteLine(report);
|
||||
}).Wait();
|
||||
|
||||
}
|
||||
return new PushReport();
|
||||
}
|
||||
else
|
||||
{
|
||||
string ereport = await result.Content.ReadAsStringAsync();
|
||||
Console.WriteLine(ereport);
|
||||
}
|
||||
return new PushReport();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
@ -30,47 +31,22 @@ namespace isn
|
||||
};
|
||||
return report;
|
||||
}
|
||||
|
||||
using (var client = new HttpClient())
|
||||
try
|
||||
{
|
||||
Console.WriteLine("Connecting to "+ pubRes.Id);
|
||||
return UploadFilesToServerUsingHttpClient.UploadFilesToServer(new Uri(pubRes.Id), fi, apikey);
|
||||
return client.UploadFilesToServer(new Uri(pubRes.Id), fi, apikey);
|
||||
}
|
||||
catch (WebException ex)
|
||||
catch (HttpRequestException hrex)
|
||||
{
|
||||
Console.Error.WriteLine(ex.Message);
|
||||
var report = new PushReport
|
||||
{
|
||||
PkgName = fi.Name
|
||||
PkgName = fi.Name,
|
||||
Message = "HttpRequest: " + hrex.Message,
|
||||
StackTrace = hrex.StackTrace,
|
||||
StatusCode = hrex.HResult.ToString()
|
||||
};
|
||||
report.StatusCode = ex.Status.ToString();
|
||||
report.OK = false;
|
||||
if (ex.Response != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (Stream respStream = ex.Response.GetResponseStream())
|
||||
{
|
||||
StreamReader sr = new StreamReader(respStream);
|
||||
string json = sr.ReadToEnd();
|
||||
var res = JsonConvert.DeserializeObject<IsndErrorMessage>(json);
|
||||
report.Message = res.msg;
|
||||
|
||||
// ecode == 1 => package already present server side.
|
||||
report.AlreadyPresent = res.ecode == 1;
|
||||
}
|
||||
}
|
||||
catch (Exception iex)
|
||||
{
|
||||
report.Message = iex.Message;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
report.Message = ex.Message;
|
||||
}
|
||||
Console.Error.WriteLine(hrex.Message);
|
||||
return report;
|
||||
}
|
||||
catch (Exception ex)
|
||||
@ -82,7 +58,7 @@ namespace isn
|
||||
StackTrace = ex.StackTrace
|
||||
};
|
||||
Console.Error.WriteLine(ex.Message);
|
||||
throw;
|
||||
return report;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user