-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
399 lines (356 loc) · 14.3 KB
/
Copy pathProgram.cs
File metadata and controls
399 lines (356 loc) · 14.3 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
using IDTExpress.NET.Models.Requests;
using IDTExpress.NET.Models.Responses;
namespace IDTExpress.NET
{
public class Program
{
public static async Task Main(string[] args)
{
// API key and secret should be securely stored and fetched.
string apiKey = "test";
string apiSecret = "Test";
// Check if API key and secret are provided
if (string.IsNullOrEmpty(apiKey) || string.IsNullOrEmpty(apiSecret))
{
Console.WriteLine("API key and secret are not set.");
return;
}
// Initialize API client
var client = new IdtExpressApiClient(apiKey, apiSecret);
// Fetch and display country coverage
var countries = await GetCountryCoverage(client);
// Fetch and display regions for a specific country
var regions = await GetRegions(client);
// Fetch DID groups based on regions
await GetDidGroups(client, regions);
// Browse available numbers for a specific DID group
await BrowseAvailableNumbers(client);
// Create an order and retrieve the order ID
var orderId = await CreateOrder(client);
// Fetch details of the created order if the order ID exists
if (!string.IsNullOrEmpty(orderId))
{
await GetOrder(client, orderId);
}
// Fetch and display all orders
await GetOrders(client);
// Get a number from the API and delete it if found
var number = await GetNumbers(client);
if (!string.IsNullOrEmpty(number))
{
await DeleteNumber(client, number);
}
}
/// <summary>
/// Fetches the country coverage from the API.
/// </summary>
private static async Task<CountryCoverage[]?> GetCountryCoverage(IdtExpressApiClient client)
{
try
{
Console.WriteLine("Fetching country coverage...");
var countries = await client.GetCountryCoverageAsync();
// Display the list of countries
if (countries != null && countries.Any())
{
foreach (var country in countries)
{
Console.WriteLine($"Country: {country.Name}, ISO Code: {country.Iso}");
}
}
else
{
Console.WriteLine("No countries found.");
}
return countries;
}
catch (IdtExpressApiException ex)
{
Console.WriteLine($"Error fetching country coverage: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"Unexpected error: {ex.Message}");
}
return null;
}
/// <summary>
/// Fetches DID groups for the specified regions.
/// </summary>
private static async Task GetDidGroups(IdtExpressApiClient client, List<Region>? regions)
{
if (regions == null) return;
foreach (var region in regions)
{
try
{
Console.WriteLine($"Fetching DID groups for '{region.Name}' - '{region.Code}'...");
// Fetch DID groups for each region
var didGroupsResponse = await client.GetDidGroupsAsync(region.Code.Split('-')[0], region.Code);
// Display DID group details
if (didGroupsResponse?.DidGroups != null && didGroupsResponse.DidGroups.Any())
{
Console.WriteLine($"Fetched {didGroupsResponse.DidGroups.Count} DID groups.");
foreach (var group in didGroupsResponse.DidGroups)
{
Console.WriteLine($"DID Group: {group.Name}, ID: {group.Id}");
Console.WriteLine($" Area Code: {group.AreaCode}");
Console.WriteLine($" NXX: {group.Nxx}");
Console.WriteLine($" Toll-Free: {group.TollFree}");
Console.WriteLine($" Supports Browse: {group.SupportsBrowse}");
Console.WriteLine($" Quantity: {group.Quantity}");
Console.WriteLine($" Fees - Setup: {group.Fees.SetupFee}, Monthly: {group.Fees.MonthlyFee}, Per Minute: {group.Fees.PerMinuteRate}");
Console.WriteLine($" Country: {group.Country.Name} ({group.Country.Iso})");
Console.WriteLine($" Region: {group.Region?.Name} ({group.Region?.Code})");
}
}
else
{
Console.WriteLine("No DID groups found.");
}
}
catch (IdtExpressApiException ex)
{
Console.WriteLine($"Error fetching DID groups: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"Unexpected error: {ex.Message}");
}
}
}
/// <summary>
/// Fetches regions for a specific country.
/// </summary>
private static async Task<List<Region>?> GetRegions(IdtExpressApiClient client)
{
try
{
string countryIsoCode = "US"; // Specify the country code
Console.WriteLine($"Fetching regions for country ISO code {countryIsoCode}...");
var regionsResponse = await client.GetRegionsAsync(countryIsoCode);
if (regionsResponse?.Regions != null && regionsResponse.Regions.Any())
{
Console.WriteLine($"Fetched {regionsResponse.Regions.Count} regions.");
foreach (var region in regionsResponse.Regions)
{
Console.WriteLine($"Region: {region.Name}, Code: {region.Code}");
}
}
else
{
Console.WriteLine("No regions found.");
}
return regionsResponse?.Regions;
}
catch (IdtExpressApiException ex)
{
Console.WriteLine($"Error fetching regions: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"Unexpected error: {ex.Message}");
}
return null;
}
/// <summary>
/// Browses available numbers for a specific DID group.
/// </summary>
private static async Task BrowseAvailableNumbers(IdtExpressApiClient client)
{
try
{
string didGroupId = "3054"; // Replace with a valid DID group ID
Console.WriteLine($"Browsing available numbers for DID group ID: {didGroupId}...");
var browseResponse = await client.BrowseAvailableNumbersAsync(didGroupId);
// Display available numbers
if (browseResponse?.Numbers != null && browseResponse.Numbers.Any())
{
foreach (var number in browseResponse.Numbers)
{
Console.WriteLine($"Number: {number.Number}, SKU: {number.Sku}");
}
}
else
{
Console.WriteLine("No available numbers found.");
}
}
catch (IdtExpressApiException ex)
{
Console.WriteLine($"Error browsing available numbers: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"Unexpected error: {ex.Message}");
}
}
/// <summary>
/// Creates a new order for numbers.
/// </summary>
private static async Task<string?> CreateOrder(IdtExpressApiClient client)
{
try
{
var createOrderRequest = new CreateOrderRequest
{
OrderItems = new List<OrderItem>
{
new OrderItem
{
DidGroupId = 3054, // Replace with a valid group ID
Quantity = 1,
DidSkus = new List<string> { "example-sku" } // Replace with a valid SKU
}
}
};
Console.WriteLine("Creating an order...");
var createOrderResponse = await client.CreateOrderAsync(createOrderRequest);
if (createOrderResponse != null)
{
var orderId = createOrderResponse.Order.Id;
Console.WriteLine($"Order created successfully. Order ID: {orderId}, Status: {createOrderResponse.Order.Status}");
return orderId;
}
else
{
Console.WriteLine("Order creation failed, no response received.");
return null;
}
}
catch (IdtExpressApiException ex)
{
Console.WriteLine($"Error creating order: {ex.Message}");
return null;
}
catch (Exception ex)
{
Console.WriteLine($"Unexpected error: {ex.Message}");
return null;
}
}
/// <summary>
/// Fetches details of an order by ID.
/// </summary>
private static async Task GetOrder(IdtExpressApiClient client, string orderId)
{
try
{
Console.WriteLine($"Fetching details for order ID: {orderId}...");
var orderResponse = await client.GetOrderAsync(orderId);
if (orderResponse?.Order != null)
{
var order = orderResponse.Order;
Console.WriteLine($"Order ID: {order.Id}");
Console.WriteLine($"Status: {order.Status}");
Console.WriteLine($"Total Quantity: {order.Ordered.Quantity}");
Console.WriteLine($"Created At: {order.CreatedAt}");
}
else
{
Console.WriteLine("Order details not found.");
}
}
catch (IdtExpressApiException ex)
{
Console.WriteLine($"Error fetching order details: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"Unexpected error: {ex.Message}");
}
}
/// <summary>
/// Fetches and displays all orders.
/// </summary>
private static async Task GetOrders(IdtExpressApiClient client)
{
try
{
Console.WriteLine("Fetching orders...");
var ordersResponse = await client.GetOrdersAsync(page: 1, pageSize: 10);
if (ordersResponse?.Orders != null && ordersResponse.Orders.Any())
{
Console.WriteLine($"Fetched {ordersResponse.Orders.Count} orders.");
foreach (var order in ordersResponse.Orders)
{
Console.WriteLine($"Order ID: {order.Id}, Status: {order.Status}, Created At: {order.CreatedAt}");
}
}
else
{
Console.WriteLine("No orders found.");
}
}
catch (IdtExpressApiException ex)
{
Console.WriteLine($"Error fetching orders: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"Unexpected error: {ex.Message}");
}
}
/// <summary>
/// Fetches numbers from the API.
/// </summary>
private static async Task<string?> GetNumbers(IdtExpressApiClient client, int page = 1, int pageSize = 10)
{
try
{
Console.WriteLine("Fetching numbers...");
var numbersResponse = await client.GetNumbersAsync();
if (numbersResponse?.Numbers != null && numbersResponse.Numbers.Any())
{
foreach (var number in numbersResponse.Numbers)
{
Console.WriteLine($"Number: {number.NumberValue}, DudGroup: {number.DidGroup.Id}, Status: {number.Status}");
}
return numbersResponse.Numbers.FirstOrDefault().NumberValue;
}
else
{
Console.WriteLine("No numbers found.");
return null;
}
}
catch (IdtExpressApiException ex)
{
Console.WriteLine($"Error fetching numbers: {ex.Message}");
return null;
}
catch (Exception ex)
{
Console.WriteLine($"Unexpected error: {ex.Message}");
return null;
}
}
/// <summary>
/// Deletes a number from the account.
/// </summary>
private static async Task DeleteNumber(IdtExpressApiClient client, string number)
{
try
{
Console.WriteLine($"Deleting number: {number}...");
var deleteResponse = await client.DeleteNumberAsync(number);
if (deleteResponse != null)
{
Console.WriteLine($"Number: {deleteResponse.Number} has been {deleteResponse.Status}.");
}
else
{
Console.WriteLine("Failed to delete the number, no response received.");
}
}
catch (IdtExpressApiException ex)
{
Console.WriteLine($"Error deleting number: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"Unexpected error: {ex.Message}");
}
}
}
}