-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
224 lines (186 loc) · 9.92 KB
/
Copy pathProgram.cs
File metadata and controls
224 lines (186 loc) · 9.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Microsoft.Extensions.CommandLineUtils;
using JunarUpload;
public class Program
{
public static void Main(string[] args)
{
CommandLineApplication commandLineApplication = new CommandLineApplication(true);
CommandArgument names = null;
commandLineApplication.Command("key", (target) =>
names = target.Argument("app_key", "Enter the app_key for publishing to Junar.", false));
CommandOption key = commandLineApplication.Option("-k |--key <key>", "The app key for Junar.", CommandOptionType.SingleValue);
CommandOption url = commandLineApplication.Option("-u |--url <url>", "The URL of the dataset.", CommandOptionType.SingleValue);
CommandOption Title = commandLineApplication.Option("-t |--title <title>", "Name for your data. Up to 100 characters", CommandOptionType.SingleValue);
CommandOption Description = commandLineApplication.Option("-d |--description <description>", "Information about this file. Up to 140 characters", CommandOptionType.SingleValue);
CommandOption Category = commandLineApplication.Option("-c |--category <category>", "A valid category name", CommandOptionType.SingleValue);
CommandOption Tags = commandLineApplication.Option("-ta |--tags <tags>", "A colon separated values list like: tag1:tag2:tag3", CommandOptionType.MultipleValue);
CommandOption Notes = commandLineApplication.Option("-n |--notes <notes>", "Do not use if you don't need it", CommandOptionType.MultipleValue);
CommandOption License = commandLineApplication.Option("-l |--license <license>", "License for use of open data; Optional", CommandOptionType.MultipleValue);
CommandOption FilePath = commandLineApplication.Option("-f |--file_path <file_path>", "Path to readable spreadsheet file", CommandOptionType.MultipleValue);
CommandOption ContentType = commandLineApplication.Option("-ct |--ContentType <ContentType>", "Content Type for uploading file", CommandOptionType.MultipleValue);
CommandOption GUID = commandLineApplication.Option("-g |--guid <guid>", "Optional. use it to update existing data.", CommandOptionType.MultipleValue);
CommandOption TableId = commandLineApplication.Option("-a |--table_id <table_id>", "Use table0 for the first sheet (or single csv file)", CommandOptionType.MultipleValue);
CommandOption Get = commandLineApplication.Option("-G |--g <get>", "Http Get response instead of POST.", CommandOptionType.SingleValue);
commandLineApplication.HelpOption("-? | -h | --help");
commandLineApplication.OnExecute(async () =>
//commandLineApplication.OnExecute(() =>
{
UploadOptions options = new UploadOptions()
{
Title = Title.Value(),
Description = Description.Value(),
Category = Category.Value(),
Tags = Tags.Values,
Notes = Notes.Value(),
License = License.Value(),
FilePath = FilePath.Value(),
ContentType = ContentType.Value(),
GUID = GUID.Value(),
TableId = TableId.Value(),
Get = Get.Value()
};
//Program.AppSettings appset = new Program.AppSettings();
//string guey = AppSettings["GUID"];
string sKey = key.Value();
string sUrl = url.Value();
if (sKey.Length == 0 || sUrl.Length == 0)
{
throw new Exception("Invalid options");
}
//IApplicationEnvironment app = null;
//Program upload = new Program(app);
Program upload = new Program();
if (options.Get != null)
upload.GetResponse(sKey, sUrl);
else
await upload.SubmitFormViaPOST(sUrl, "file_data", options, sKey);
return 0;
});
commandLineApplication.Execute(args);
}
public string GetResponse(string key, string url)
{
using (HttpClient client = new HttpClient())
{
url = url + "?auth_key=" + key;
dynamic response = client.GetAsync(url).Result;
dynamic responseContent = new StreamReader(response.Content.ReadAsStreamAsync().Result).ReadToEnd();
Console.Out.Write(responseContent);
if (response.IsSuccessStatusCode == false)
{
return null;
}
return responseContent;
}
}
public async Task SubmitFormViaPOST(string serverUrl,
string fileControlName,
UploadOptions request,
string key)
{
FileInfo fileInfo = new FileInfo(request.FilePath);
Uri RequestUri = new Uri(serverUrl);
// Create HTTP transport objects
HttpRequestMessage httpRequest = null;
try
{
#region Build Request Content
// 27 dashes then current ticks as hexa decimal
string partBoundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
byte[] partBoundaryBytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + partBoundary + "\r\n");
string formContentType = "multipart/form-data; boundary=" + partBoundary;
MemoryStream requestStream = new MemoryStream();
List<KeyValuePair<string, string>> kvps = new List<KeyValuePair<string, string>>();
kvps.Add(new KeyValuePair<string, string>("auth_key", key));
kvps.Add(new KeyValuePair<string, string>("title", request.Title));
kvps.Add(new KeyValuePair<string, string>("description", request.Description));
kvps.Add(new KeyValuePair<string, string>("notes", request.Notes));
kvps.Add(new KeyValuePair<string, string>("meta_data", request.Notes));
kvps.Add(new KeyValuePair<string, string>("category", request.Category));
kvps.Add(new KeyValuePair<string, string>("tags", string.Join(",", request.Tags ?? new List<string>())));
if (!string.IsNullOrWhiteSpace(request.License))
kvps.Add(new KeyValuePair<string, string>("license", request.License));
if (!string.IsNullOrWhiteSpace(request.TableId))
kvps.Add(new KeyValuePair<string, string>("table_id", request.TableId));
else
kvps.Add(new KeyValuePair<string, string>("table_id", "0"));
if ((!string.IsNullOrWhiteSpace(request.GUID)))
{
kvps.Add(new KeyValuePair<string, string>("guid", request.GUID));
kvps.Add(new KeyValuePair<string, string>("clone", "True"));
}
string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
foreach (KeyValuePair<string, string> kv in kvps)
{
requestStream.Write(partBoundaryBytes, 0, partBoundaryBytes.Length);
string formItem = string.Format(formdataTemplate, kv.Key, kv.Value);
byte[] formItemBytes = System.Text.Encoding.ASCII.GetBytes(formItem);
requestStream.Write(formItemBytes, 0, formItemBytes.Length);
}
requestStream.Write(partBoundaryBytes, 0, partBoundaryBytes.Length);
FileInfo uplFile = new FileInfo(request.FilePath);
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
string header = string.Format(headerTemplate, fileControlName, uplFile.Name, request.ContentType);
byte[] headerBytes = System.Text.Encoding.ASCII.GetBytes(header);
requestStream.Write(headerBytes, 0, headerBytes.Length);
FileStream fileStream = new FileStream(request.FilePath, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[4096];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
requestStream.Write(buffer, 0, bytesRead);
}
fileStream.Dispose();
byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + partBoundary + "--\r\n");
requestStream.Write(trailer, 0, trailer.Length);
#endregion
#region creates HttpRequestMessage object
httpRequest = new HttpRequestMessage();
httpRequest.Method = HttpMethod.Post;
httpRequest.RequestUri = RequestUri;
requestStream.Position = 0;
httpRequest.Content = new StreamContent(requestStream);
httpRequest.Content.Headers.Remove("Content-Type");
httpRequest.Content.Headers.TryAddWithoutValidation("Content-Type", formContentType);
#endregion
#region Send the request and process response
// Send Request
HttpResponseMessage httpResponse = null;
try
{
HttpClient httpClient = new HttpClient();
httpClient.Timeout = TimeSpan.FromSeconds(300);
httpResponse = await httpClient.SendAsync(httpRequest);
System.Net.HttpStatusCode statusCode = httpResponse.StatusCode;
string errorResponse =
await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
Console.Out.Write(errorResponse);
if (statusCode != System.Net.HttpStatusCode.OK)
{
throw new Exception(errorResponse);
}
}
finally
{
if (httpResponse != null)
{
httpResponse.Dispose();
}
}
#endregion
}
finally
{
if (httpRequest != null)
{
httpRequest.Dispose();
}
}
}
}