GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — IDTExpress.NET/IdtExpressApiClient.cs at main · Spire-Recovery-Solutions/IDTExpress.NET

Expand file tree

/

IdtExpressApiClient.cs

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
224 lines (200 loc) · 9.79 KB
/

IdtExpressApiClient.cs

Copy path

File metadata and controls

224 lines (200 loc) · 9.79 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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 IDTExpress.NET.Models.Requests;
using IDTExpress.NET.Models.Responses;
using System.Net;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
namespace IDTExpress.NET;
/// <summary>
/// A client for interacting with the IDT Express API, providing functionality for managing DIDs, orders, and phone numbers.
/// </summary>
public class IdtExpressApiClient
{
private readonly HttpClient _httpClient;
/// <summary>
/// Initializes a new instance of the <see cref="IdtExpressApiClient"/> class.
/// </summary>
/// <param name="apiKey">The API key for authenticating requests.</param>
/// <param name="apiSecret">The API secret for authenticating requests.</param>
/// <param name="baseUrl">The base URL for the API. Defaults to the sandbox environment.</param>
public IdtExpressApiClient(string apiKey, string apiSecret, string baseUrl = "https://sandbox-api.idtexpress.com/v1/")
{
_httpClient = new HttpClient { BaseAddress = new Uri(baseUrl) };
_httpClient.DefaultRequestHeaders.Add("x-api-key", apiKey);
_httpClient.DefaultRequestHeaders.Add("x-api-secret", apiSecret);
}
/// <summary>
/// Sends an HTTP request to the API and returns a response of the specified type.
/// </summary>
/// <typeparam name="TRequest">The type of the request object.</typeparam>
/// <typeparam name="TResponse">The type of the response object.</typeparam>
/// <param name="method">The HTTP method (GET, POST, PUT, DELETE).</param>
/// <param name="endpoint">The API endpoint to send the request to.</param>
/// <param name="request">The request object to send (optional).</param>
/// <returns>The response from the API.</returns>
private async Task<TResponse> SendRequestAsync<TRequest, TResponse>(HttpMethod method, string endpoint,
TRequest? request = default)
{
var requestInfo =
IdtExpressJsonSerializerContext.Default.GetTypeInfo(typeof(TRequest)) as JsonTypeInfo<TRequest>;
var responseInfo =
IdtExpressJsonSerializerContext.Default.GetTypeInfo(typeof(TResponse)) as
JsonTypeInfo<TResponse>;
if (requestInfo == null || responseInfo == null)
{
throw new InvalidOperationException(
$"Type {typeof(TRequest)} or {typeof(TResponse)} is not registered in IdtExpressJsonSerializerContext.");
}
using var content = request != null
? new StringContent(JsonSerializer.Serialize(request, requestInfo), Encoding.UTF8, "application/json")
: null;
try
{
HttpResponseMessage response = method switch
{
{ } m when m == HttpMethod.Get => await _httpClient.GetAsync(endpoint),
{ } m when m == HttpMethod.Post => await _httpClient.PostAsync(endpoint, content),
{ } m when m == HttpMethod.Put => await _httpClient.PutAsync(endpoint, content),
{ } m when m == HttpMethod.Delete => await _httpClient.DeleteAsync(endpoint),
_ => throw new NotSupportedException($"HTTP method {method} is not supported.")
};
var responseContent = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode)
{
var apiResponse = JsonSerializer.Deserialize(responseContent, responseInfo);
if (apiResponse == null || apiResponse == null)
{
throw new IdtExpressApiException("Failed to deserialize the response or response data is null.");
}
return apiResponse;
}
var errorResponse =
JsonSerializer.Deserialize(responseContent, IdtExpressJsonSerializerContext.Default.ErrorResponse);
throw errorResponse != null
? new IdtExpressApiException(errorResponse, response.StatusCode)
: new IdtExpressApiException(response.StatusCode.ToString(), responseContent ?? "No response content",
response.StatusCode);
}
catch (IdtExpressApiException)
{
throw;
}
catch (Exception ex)
{
throw new IdtExpressApiException("An unexpected error occurred", ex.Message,
HttpStatusCode.InternalServerError);
}
}
/// <summary>
/// Retrieves the country coverage for DIDs.
/// </summary>
/// <returns>An array of countries with available DID coverage.</returns>
public async Task<CountryCoverage[]> GetCountryCoverageAsync()
{
var response = await SendRequestAsync<object, CountryCoverageResponse>(HttpMethod.Get, "dids/coverage/countries");
return response.Countries.ToArray();
}
/// <summary>
/// Retrieves regions for a specific country.
/// </summary>
/// <param name="countryIso">The ISO 3166 country code.</param>
/// <returns>A response containing the regions available for the specified country.</returns>
public async Task<RegionsResponse> GetRegionsAsync(string countryIso)
{
var endpoint = $"dids/coverage/countries/{countryIso}/regions";
return await SendRequestAsync<object, RegionsResponse>(HttpMethod.Get, endpoint);
}
/// <summary>
/// Retrieves available DID groups based on country, region, and toll-free options.
/// </summary>
/// <param name="countryIso">The ISO country code (e.g., "US").</param>
/// <param name="regionCode">Optional region code (required for some countries, except Toll-Free numbers).</param>
/// <param name="tollFree">Optional flag to specify whether to retrieve Toll-Free numbers.</param>
/// <returns>A response containing the available DID groups.</returns>
public async Task<DidGroupsResponse> GetDidGroupsAsync(string countryIso, string? regionCode = null, bool? tollFree = null)
{
var queryParams = new List<string> { $"country_iso={countryIso}" };
if (!string.IsNullOrEmpty(regionCode))
{
queryParams.Add($"region_code={regionCode}");
}
if (tollFree.HasValue)
{
queryParams.Add($"toll_free={tollFree.Value.ToString().ToLower()}");
}
var endpoint = $"dids/coverage/did_groups?{string.Join("&", queryParams)}";
return await SendRequestAsync<object, DidGroupsResponse>(HttpMethod.Get, endpoint);
}
/// <summary>
/// Browses available phone numbers for a specific DID group.
/// </summary>
/// <param name="didGroupId">The ID of the DID group.</param>
/// <returns>A response containing the available phone numbers and their SKUs.</returns>
public async Task<BrowseAvailableNumbersResponse> BrowseAvailableNumbersAsync(string didGroupId)
{
var endpoint = $"dids/coverage/did_groups/{didGroupId}/browse_numbers";
return await SendRequestAsync<object, BrowseAvailableNumbersResponse>(HttpMethod.Get, endpoint);
}
/// <summary>
/// Creates an order for DID numbers.
/// </summary>
/// <param name="request">The order request details.</param>
/// <returns>A response containing the details of the created order.</returns>
public async Task<OrderResponse> CreateOrderAsync(CreateOrderRequest request)
{
const string endpoint = "dids/orders";
return await SendRequestAsync<CreateOrderRequest, OrderResponse>(HttpMethod.Post, endpoint, request);
}
/// <summary>
/// Retrieves the details of a specific order.
/// </summary>
/// <param name="orderId">The ID of the order.</param>
/// <returns>A response containing the details of the order.</returns>
public async Task<OrderResponse> GetOrderAsync(string orderId)
{
var endpoint = $"dids/orders/{orderId}";
return await SendRequestAsync<object, OrderResponse>(HttpMethod.Get, endpoint);
}
/// <summary>
/// Retrieves a list of DID orders with optional filtering.
/// </summary>
/// <param name="page">The page number to retrieve (default is 1).</param>
/// <param name="pageSize">The number of records per page (default is 10).</param>
/// <param name="filterByStatus">Optional filter by order status.</param>
/// <returns>A response containing the list of DID orders.</returns>
public async Task<OrdersResponse> GetOrdersAsync(int page = 1, int pageSize = 10, string? filterByStatus = null)
{
var queryParams = new List<string>
{
$"page={page}",
$"page_size={pageSize}"
};
if (!string.IsNullOrEmpty(filterByStatus))
{
queryParams.Add($"filter_by_status={filterByStatus}");
}
var endpoint = $"dids/orders?{string.Join("&", queryParams)}";
return await SendRequestAsync<object, OrdersResponse>(HttpMethod.Get, endpoint);
}
/// <summary>
/// Retrieves a list of phone numbers with pagination.
/// </summary>
/// <param name="page">The page number to retrieve (default is 1).</param>
/// <param name="pageSize">The number of records per page (default is 10).</param>
/// <returns>A response containing the list of phone numbers.</returns>
public async Task<NumberResponse> GetNumbersAsync(int page = 1, int pageSize = 10)
{
var endpoint = $"dids/numbers?page={page}&page_size={pageSize}";
return await SendRequestAsync<object, NumberResponse>(HttpMethod.Get, endpoint);
}
/// <summary>
/// Deletes a phone number from the account.
/// </summary>
/// <param name="number">The phone number to delete.</param>
/// <returns>A response indicating whether the deletion was successful.</returns>
public async Task<DeleteNumberResponse> DeleteNumberAsync(string number)
{
var endpoint = $"dids/numbers/{number}";
return await SendRequestAsync<object, DeleteNumberResponse>(HttpMethod.Delete, endpoint);
}
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.