-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleAuto.cs
More file actions
174 lines (152 loc) · 5.58 KB
/
Copy pathExampleAuto.cs
File metadata and controls
174 lines (152 loc) · 5.58 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
using ApiClient;
using ApiClient.Models;
using DigikeyApiWrapper;
using ESNLib.Tools;
using ESNLib.Tools.WinForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Example
{
public partial class ExampleAuto : Form, AdminTools.IAdminForm
{
private ApiClientService service;
public ExampleAuto()
{
InitializeComponent();
Logger.Instance.Write("Application idle...");
ApiClientSettings.SetFilePath("ESN", "ApiClientWrapper");
service = new ApiClientService(ApiClientSettings.GetInstance());
#if DEBUG
Process.Start(Logger.Instance.FileOutputPath);
//Logger.Instance.Write(ApiClientSettings.GetInstance().ToString());
#endif
GetAccess();
}
public string GetAppPath()
{
return Application.ExecutablePath;
}
private void SetLabel(bool state)
{
if (state)
{
var setting = ApiClientSettings.GetInstance();
lblToken.Text = "Available";
lblToken.BackColor = Color.LightGreen;
}
else
{
lblToken.Text = "Expired";
lblToken.BackColor = Color.Crimson;
}
this.Focus();
}
private async void GetAccess()
{
// Verify listen uri, redirect uri
if (string.IsNullOrEmpty(ApiClientSettings.GetInstance().ListenUri)
|| string.IsNullOrEmpty(ApiClientSettings.GetInstance().RedirectUri))
{
return;
}
var client = new ApiClientWrapper();
var result = await client.GetAccess();
if (result.Success)
{
SetLabel(true);
if (result.NewService != null)
{
service = result.NewService;
}
}
else
{
if (!MiscTools.HasAdminPrivileges())
{
AdminTools.RunAsAdmin(this);
}
else
{
SetLabel(false);
var res = ESNLib.Controls.Dialog.ShowDialog(
new ESNLib.Controls.Dialog.DialogConfig(
"Unable to get access token... Check the config and the logs",
"Error"
)
{
Button1 = ESNLib.Controls.Dialog.ButtonType.Ignore,
Button2 = ESNLib.Controls.Dialog.ButtonType.Custom1,
CustomButton1Text = "Open log",
Icon = ESNLib.Controls.Dialog.DialogIcon.Error,
}
);
if (res.DialogResult == ESNLib.Controls.Dialog.DialogResult.Custom1)
{
Process.Start(Logger.Instance.FileOutputPath);
}
}
}
}
private void btnSearch_Click(object sender, EventArgs e)
{
ProductDetails();
}
private async void ProductDetails()
{
PartSearch ps = new PartSearch();
//string response = await ps.ProductDetails_Essentials(textBox1.Text);
string response = await service.ProductDetails(textBox1.Text);
// Simulate search result :
//string response = File.ReadAllText(@"C:\Workspace\01_Programming\DigikeyAPI\Example\bin\Debug\simulate.json");
richTextBox1.Text = response;
// Compress
var compressed = Zip(response);
File.WriteAllBytes("compressed.json", compressed);
}
public static byte[] Zip(string str)
{
var bytes = Encoding.UTF8.GetBytes(str);
using (var msi = new MemoryStream(bytes))
using (var mso = new MemoryStream())
{
using (var gs = new GZipStream(mso, CompressionMode.Compress))
{
msi.CopyTo(gs);
}
return mso.ToArray();
}
}
public static string Unzip(byte[] bytes)
{
using (var msi = new MemoryStream(bytes))
using (var mso = new MemoryStream())
{
using (var gs = new GZipStream(msi, CompressionMode.Decompress))
{
gs.CopyTo(mso);
}
return Encoding.UTF8.GetString(mso.ToArray());
}
}
private void lblToken_DoubleClick(object sender, EventArgs e)
{
Process.Start(Logger.Instance.FileOutputPath);
}
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.Text =
@"{""StandardPricing"":[{""BreakQuantity"":1,""UnitPrice"":0.86,""TotalPrice"":0.86}],""DetailedDescription"":""1000µF 35V Aluminum Electrolytic Capacitors Radial, Can 2000 Hrs @ 105°C"",""ManufacturerPartNumber"":""ECA-1VHG102"",""DigiKeyPartNumber"":""P5555-ND"",""Manufacturer"":{""ParameterId"":-1,""ValueId"":""10"",""Parameter"":""Manufacturer"",""Value"":""Panasonic Electronic Components""}}";
DigikeyPart myPart = PartSearch.DeserializeProductDetails(richTextBox1.Text);
}
}
}