forked from smartstore/SmartStoreNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
287 lines (234 loc) · 8.06 KB
/
Copy pathMainForm.cs
File metadata and controls
287 lines (234 loc) · 8.06 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using SmartStore.Net.WebApi;
using SmartStoreNetWebApiClient.Properties;
namespace SmartStoreNetWebApiClient
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
this.Text = Program.AppName;
this.Load += (object sender, EventArgs e) =>
{
var s = Settings.Default;
s.Reload();
cboMethod.SelectedIndex = 0;
radioJson.Checked = true;
radioOdata.Checked = true;
txtPublicKey.Text = s.ApiPublicKey;
txtSecretKey.Text = s.ApiSecretKey;
txtUrl.Text = s.ApiUrl;
txtVersion.Text = s.ApiVersion;
cboPath.Items.FromString(s.ApiPaths);
cboQuery.Items.FromString(s.ApiQuery);
cboContent.Items.FromString(s.ApiContent);
if (cboPath.Items.Count <= 0)
cboPath.Items.Add("/Customers");
cboMethod_changeCommitted(null, null);
radioApi_CheckedChanged(null, null);
openFileDialog1.Filter = "Supported files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png, *.csv, *.xlsx, *.txt, *.tab, *.zip) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png; *.csv; *.xlsx; *.txt; *.tab; *.zip";
openFileDialog1.DefaultExt = ".jpg";
openFileDialog1.FileName = "";
openFileDialog1.Title = "Please select files to upload";
openFileDialog1.Multiselect = true;
};
this.FormClosing += (object sender, FormClosingEventArgs e) =>
{
var s = Settings.Default;
s.ApiPublicKey = txtPublicKey.Text;
s.ApiSecretKey = txtSecretKey.Text;
s.ApiUrl = txtUrl.Text;
s.ApiVersion = txtVersion.Text;
Settings.Default[radioOdata.Checked ? "ApiPaths" : "ApiPaths2"] = cboPath.Items.IntoString();
s.ApiQuery = cboQuery.Items.IntoString();
s.ApiContent = cboContent.Items.IntoString();
s.Save();
};
}
private void CallTheApi()
{
if (txtUrl.Text.HasValue() && !txtUrl.Text.EndsWith("/"))
txtUrl.Text = txtUrl.Text + "/";
if (cboPath.Text.HasValue() && !cboPath.Text.StartsWith("/"))
cboPath.Text = "/" + cboPath.Text;
var context = new WebApiRequestContext
{
PublicKey = txtPublicKey.Text,
SecretKey = txtSecretKey.Text,
Url = txtUrl.Text + (radioOdata.Checked ? "odata/" : "api/") + txtVersion.Text + cboPath.Text,
HttpMethod = cboMethod.Text,
HttpAcceptType = (radioJson.Checked ? ApiConsumer.JsonAcceptType : ApiConsumer.XmlAcceptType)
};
if (cboQuery.Text.HasValue())
context.Url = string.Format("{0}?{1}", context.Url, cboQuery.Text);
if (!context.IsValid)
{
"Please enter Public-Key, Secret-Key, URL and method.".Box(MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
Debug.WriteLine(context.ToString());
return;
}
var apiConsumer = new ApiConsumer();
var response = new WebApiConsumerResponse();
var sb = new StringBuilder();
StringBuilder requestContent = null;
Dictionary<string, object> multiPartData = null;
lblRequest.Text = "Request: " + context.HttpMethod + " " + context.Url;
lblRequest.Refresh();
if (radioApi.Checked && txtFile.Text.HasValue())
{
if (string.Compare(context.HttpMethod, "POST", StringComparison.OrdinalIgnoreCase) != 0)
{
"Please select POST method for image upload.".Box(MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
var id1 = txtIdentfier1.Text.ToInt();
var id2 = txtIdentfier2.Text;
var pictureId = txtPictureId.Text.ToInt();
var moreData = txtMoreData.Text.EmptyNull().Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
var keyForId1 = "Id";
var keyForId2 = "";
multiPartData = new Dictionary<string, object>();
if (cboPath.Text.StartsWith("/Uploads/ProductImages"))
{
// only one identifier required: product id, sku or gtin
keyForId2 = "Sku";
}
else if (cboPath.Text.StartsWith("/Uploads/ImportFiles"))
{
// only one identifier required: import profile id or profile name
keyForId2 = "Name";
}
if (id1 != 0)
{
multiPartData.Add(keyForId1, id1);
}
if (id2.HasValue())
{
multiPartData.Add(keyForId2, id2);
}
// To delete existing import files... deleteExisting:true
// To start import... startImport:true
foreach (var str in moreData)
{
var data = str.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
if (data.Length == 2)
{
multiPartData.Add(data[0], data[1]);
}
}
apiConsumer.AddApiFileParameter(multiPartData, txtFile.Text, pictureId);
}
var webRequest = apiConsumer.StartRequest(context, cboContent.Text, multiPartData, out requestContent);
txtRequest.Text = requestContent.ToString();
var result = apiConsumer.ProcessResponse(webRequest, response, folderBrowserDialog1);
lblResponse.Text = "Response: " + response.Status;
sb.Append(response.Headers);
if (result && response.Content.HasValue())
{
if (radioJson.Checked && radioOdata.Checked)
{
var customers = response.TryParseCustomers();
if (customers != null)
{
sb.AppendLine("Parsed {0} customer(s):".FormatInvariant(customers.Count));
customers.ForEach(x => sb.AppendLine(x.ToString()));
sb.Append("\r\n");
}
}
}
sb.Append(response.Content);
txtResponse.Text = sb.ToString();
cboPath.InsertRolled(cboPath.Text, 64);
cboQuery.InsertRolled(cboQuery.Text, 64);
cboContent.InsertRolled(cboContent.Text, 64);
}
private void SavePathItems(bool odata)
{
Settings.Default[odata ? "ApiPaths2" : "ApiPaths"] = cboPath.Items.IntoString();
Settings.Default.Save();
cboPath.Text = "";
cboPath.Items.Clear();
cboPath.Items.FromString(odata ? Settings.Default.ApiPaths : Settings.Default.ApiPaths2);
}
private void MainForm_Shown(object sender, EventArgs e)
{
if (txtVersion.Text.Length == 0)
txtVersion.Text = "v1";
if (txtUrl.Text.Length == 0)
txtUrl.Text = "http://www.my-store.com/";
cboPath.Focus();
}
private void callApi_Click(object sender, EventArgs e)
{
clear_Click(null, null);
using (new HourGlass())
{
CallTheApi();
}
}
private void cboMethod_changeCommitted(object sender, EventArgs e)
{
bool enable = ApiConsumer.BodySupported(cboMethod.Text);
cboContent.Enabled = enable;
}
private void btnDeletePath_Click(object sender, EventArgs e)
{
cboPath.RemoveCurrent();
}
private void btnDeleteQuery_Click(object sender, EventArgs e)
{
cboQuery.RemoveCurrent();
}
private void btnDeleteContent_Click(object sender, EventArgs e)
{
cboContent.RemoveCurrent();
}
private void clear_Click(object sender, EventArgs e)
{
txtRequest.Clear();
lblRequest.Text = "Request";
txtResponse.Clear();
lblResponse.Text = "Response";
txtRequest.Refresh();
lblRequest.Refresh();
txtResponse.Refresh();
lblResponse.Refresh();
}
private void odata_Click(object sender, EventArgs e)
{
SavePathItems(true);
}
private void api_Click(object sender, EventArgs e)
{
SavePathItems(false);
}
private void radioApi_CheckedChanged(object sender, EventArgs e)
{
var show = radioApi.Checked;
lblFile.Visible = show;
txtFile.Visible = show;
btnFileOpen.Visible = show;
lblIdentifier1.Visible = show;
txtIdentfier1.Visible = show;
lblIdentfier2.Visible = show;
txtIdentfier2.Visible = show;
lblPictureId.Visible = show;
txtPictureId.Visible = show;
lblMoreData.Visible = show;
txtMoreData.Visible = show;
}
private void btnFileOpen_Click(object sender, EventArgs e)
{
var result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
txtFile.Text = string.Join(";", openFileDialog1.FileNames);
}
}
}
}