forked from yavordimitrov/SmartStoreNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
181 lines (151 loc) · 4.97 KB
/
Copy pathMainForm.cs
File metadata and controls
181 lines (151 loc) · 4.97 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
using System;
using System.Diagnostics;
using System.Windows.Forms;
using SmartStoreNetWebApiClient.Properties;
using SmartStore.Net.WebApi;
using SmartStoreNetWebApiClient.Misc;
using System.Web;
using System.Globalization;
using System.Text;
namespace SmartStoreNetWebApiClient
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
this.FormClosing += MainForm_Closing;
this.Text = Program.AppName;
cboMethod.SelectedIndex = 0;
radioJson.Checked = true;
radioOdata.Checked = true;
txtPublicKey.Text = Settings.Default.ApiPublicKey;
txtSecretKey.Text = Settings.Default.ApiSecretKey;
cboPath.Items.FromString(Settings.Default.ApiPaths);
cboQuery.Items.FromString(Settings.Default.ApiQuery);
cboContent.Items.FromString(Settings.Default.ApiContent);
if (cboPath.Items.Count <= 0)
cboPath.Items.Add("/Customers");
cboMethod_changeCommitted(null, null);
}
private void CallTheApi()
{
if (!string.IsNullOrWhiteSpace(txtUrl.Text) && !txtUrl.Text.EndsWith("/"))
txtUrl.Text = txtUrl.Text + "/";
if (!string.IsNullOrWhiteSpace(cboPath.Text) && !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 (!string.IsNullOrWhiteSpace(cboQuery.Text))
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 requestContent = new StringBuilder();
var response = new WebApiConsumerResponse();
var sb = new StringBuilder();
lblRequest.Text = "Request: " + context.HttpMethod + " " + context.Url;
lblRequest.Refresh();
var webRequest = apiConsumer.StartRequest(context, cboContent.Text, requestContent);
txtRequest.Text = requestContent.ToString();
bool result = apiConsumer.ProcessResponse(webRequest, response);
lblResponse.Text = "Response: " + response.Status;
sb.Append(response.Headers);
if (result && radioJson.Checked && radioOdata.Checked)
{
var customers = apiConsumer.TryParseCustomers(response);
if (customers != null)
{
sb.AppendLine(string.Format("Parsed {0} customer(s):", customers.Count));
foreach (var customer in customers)
sb.AppendLine(customer.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_Closing(object sender, FormClosingEventArgs e)
{
Settings.Default["ApiPublicKey"] = txtPublicKey.Text;
Settings.Default["ApiSecretKey"] = txtSecretKey.Text;
Settings.Default[radioOdata.Checked ? "ApiPaths" : "ApiPaths2"] = cboPath.Items.IntoString();
Settings.Default["ApiQuery"] = cboQuery.Items.IntoString();
Settings.Default["ApiContent"] = cboContent.Items.IntoString();
Settings.Default.Save();
}
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);
}
}
}