|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Net; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Microsoft.Extensions.Logging; |
| 7 | +using Svix.Abstractions; |
| 8 | +using Svix.Api; |
| 9 | +using Svix.Client; |
| 10 | +using Svix.Model; |
| 11 | +using Svix.Models; |
| 12 | + |
| 13 | +namespace Svix |
| 14 | +{ |
| 15 | + public sealed class OperationalWebhookEndpoint : SvixResourceBase, IOperationalWebhookEndpoint |
| 16 | + { |
| 17 | + private readonly IWebhookEndpointApi _opWebhookEndpointApi; |
| 18 | + |
| 19 | + public OperationalWebhookEndpoint(ISvixClient svixClient, IWebhookEndpointApi endpoingApi) |
| 20 | + : base(svixClient) |
| 21 | + { |
| 22 | + _opWebhookEndpointApi = endpoingApi ?? throw new ArgumentNullException(nameof(_opWebhookEndpointApi)); |
| 23 | + } |
| 24 | + |
| 25 | + public OperationalWebhookEndpointOut Create( |
| 26 | + OperationalWebhookEndpointIn endpoint, string idempotencyKey = default) |
| 27 | + { |
| 28 | + try |
| 29 | + { |
| 30 | + var lEndpoint = _opWebhookEndpointApi.CreateOperationalWebhookEndpoint( |
| 31 | + endpoint, |
| 32 | + idempotencyKey); |
| 33 | + |
| 34 | + return lEndpoint; |
| 35 | + } |
| 36 | + catch (ApiException e) |
| 37 | + { |
| 38 | + Logger?.LogError(e, $"{nameof(Create)} failed"); |
| 39 | + |
| 40 | + if (Throw) |
| 41 | + throw; |
| 42 | + |
| 43 | + return null; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + public async Task<OperationalWebhookEndpointOut> CreateAsync( |
| 48 | + OperationalWebhookEndpointIn endpoint, string idempotencyKey = default, |
| 49 | + CancellationToken cancellationToken = default) |
| 50 | + { |
| 51 | + try |
| 52 | + { |
| 53 | + var lEndpoint = await _opWebhookEndpointApi.CreateOperationalWebhookEndpointAsync( |
| 54 | + endpoint, |
| 55 | + idempotencyKey, |
| 56 | + cancellationToken); |
| 57 | + |
| 58 | + return lEndpoint; |
| 59 | + } |
| 60 | + catch (ApiException e) |
| 61 | + { |
| 62 | + Logger?.LogError(e, $"{nameof(CreateAsync)} failed"); |
| 63 | + |
| 64 | + if (Throw) |
| 65 | + throw; |
| 66 | + |
| 67 | + return null; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public bool Delete(string endpointId, string idempotencyKey = default) |
| 72 | + { |
| 73 | + try |
| 74 | + { |
| 75 | + var lResponse = _opWebhookEndpointApi.DeleteOperationalWebhookEndpointWithHttpInfo( |
| 76 | + endpointId); |
| 77 | + |
| 78 | + return lResponse.StatusCode == HttpStatusCode.NoContent; |
| 79 | + } |
| 80 | + catch (ApiException e) |
| 81 | + { |
| 82 | + Logger?.LogError(e, $"{nameof(Delete)} failed"); |
| 83 | + |
| 84 | + if (Throw) |
| 85 | + throw; |
| 86 | + |
| 87 | + return false; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public async Task<bool> DeleteAsync(string endpointId, string idempotencyKey = default, |
| 92 | + CancellationToken cancellationToken = default) |
| 93 | + { |
| 94 | + try |
| 95 | + { |
| 96 | + var lResponse = await _opWebhookEndpointApi.DeleteOperationalWebhookEndpointWithHttpInfoAsync( |
| 97 | + endpointId, |
| 98 | + cancellationToken); |
| 99 | + |
| 100 | + return lResponse.StatusCode == HttpStatusCode.NoContent; |
| 101 | + } |
| 102 | + catch (ApiException e) |
| 103 | + { |
| 104 | + Logger?.LogError(e, $"{nameof(DeleteAsync)} failed"); |
| 105 | + |
| 106 | + if (Throw) |
| 107 | + throw; |
| 108 | + |
| 109 | + return false; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + public OperationalWebhookEndpointOut Get(string endpointId, string idempotencyKey = default) |
| 114 | + { |
| 115 | + try |
| 116 | + { |
| 117 | + var lEndpoint = _opWebhookEndpointApi.GetOperationalWebhookEndpoint(endpointId); |
| 118 | + return lEndpoint; |
| 119 | + } |
| 120 | + catch (ApiException e) |
| 121 | + { |
| 122 | + Logger?.LogError(e, $"{nameof(Get)} failed"); |
| 123 | + |
| 124 | + if (Throw) |
| 125 | + throw; |
| 126 | + |
| 127 | + return null; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + public async Task<OperationalWebhookEndpointOut> GetAsync(string endpointId, string idempotencyKey = default, |
| 132 | + CancellationToken cancellationToken = default) |
| 133 | + { |
| 134 | + try |
| 135 | + { |
| 136 | + var lEndpoint = await _opWebhookEndpointApi.GetOperationalWebhookEndpointAsync( |
| 137 | + endpointId, |
| 138 | + cancellationToken); |
| 139 | + |
| 140 | + return lEndpoint; |
| 141 | + } |
| 142 | + catch (ApiException e) |
| 143 | + { |
| 144 | + Logger?.LogError(e, $"{nameof(GetAsync)} failed"); |
| 145 | + |
| 146 | + if (Throw) |
| 147 | + throw; |
| 148 | + |
| 149 | + return null; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + public string GetSecret(string endpointId, string idempotencyKey = default) |
| 154 | + { |
| 155 | + try |
| 156 | + { |
| 157 | + var lSecret = _opWebhookEndpointApi.GetOperationalWebhookEndpointSecret( |
| 158 | + endpointId); |
| 159 | + |
| 160 | + return lSecret?.Key; |
| 161 | + } |
| 162 | + catch (ApiException e) |
| 163 | + { |
| 164 | + Logger?.LogError(e, $"{nameof(GetSecret)} failed"); |
| 165 | + |
| 166 | + if (Throw) |
| 167 | + throw; |
| 168 | + |
| 169 | + return null; |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + public async Task<string> GetSecretAsync(string endpointId, string idempotencyKey = default, |
| 174 | + CancellationToken cancellationToken = default) |
| 175 | + { |
| 176 | + try |
| 177 | + { |
| 178 | + var lSecret = await _opWebhookEndpointApi.GetOperationalWebhookEndpointSecretAsync( |
| 179 | + endpointId, |
| 180 | + cancellationToken); |
| 181 | + |
| 182 | + return lSecret.Key; |
| 183 | + } |
| 184 | + catch (ApiException e) |
| 185 | + { |
| 186 | + Logger?.LogError(e, $"{nameof(GetSecretAsync)} failed"); |
| 187 | + |
| 188 | + if (Throw) |
| 189 | + throw; |
| 190 | + |
| 191 | + return null; |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + public ListResponseOperationalWebhookEndpointOut List(ListOptions options = null, |
| 196 | + string idempotencyKey = default) |
| 197 | + { |
| 198 | + try |
| 199 | + { |
| 200 | + var lEndpoints = _opWebhookEndpointApi.ListOperationalWebhookEndpoints( |
| 201 | + options?.Limit, |
| 202 | + options?.Iterator, |
| 203 | + options?.Order); |
| 204 | + |
| 205 | + return lEndpoints; |
| 206 | + } |
| 207 | + catch (ApiException e) |
| 208 | + { |
| 209 | + Logger?.LogError(e, $"{nameof(List)} failed"); |
| 210 | + |
| 211 | + if (Throw) |
| 212 | + throw; |
| 213 | + |
| 214 | + return new ListResponseEndpointOut(); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + public async Task<ListResponseOperationalWebhookEndpointOut> ListAsync( |
| 219 | + ListOptions options = null, string idempotencyKey = default, |
| 220 | + CancellationToken cancellationToken = default) |
| 221 | + { |
| 222 | + try |
| 223 | + { |
| 224 | + var lEndpoints = await _opWebhookEndpointApi.ListOperationalWebhookEndpointsAsync( |
| 225 | + options?.Limit, |
| 226 | + options?.Iterator, |
| 227 | + options?.Order, |
| 228 | + cancellationToken); |
| 229 | + |
| 230 | + return lEndpoints; |
| 231 | + } |
| 232 | + catch (ApiException e) |
| 233 | + { |
| 234 | + Logger?.LogError(e, $"{nameof(ListAsync)} failed"); |
| 235 | + |
| 236 | + if (Throw) |
| 237 | + throw; |
| 238 | + |
| 239 | + return new ListResponseEndpointOut(); |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + public bool RotateSecret(string endpointId, OperationalWebhookEndpointSecretIn secret, string idempotencyKey = default) |
| 244 | + { |
| 245 | + try |
| 246 | + { |
| 247 | + var lResponse = _opWebhookEndpointApi.RotateOperationalWebhookEndpointSecretWithHttpInfo( |
| 248 | + endpointId, |
| 249 | + secret, |
| 250 | + idempotencyKey); |
| 251 | + |
| 252 | + return lResponse.StatusCode == HttpStatusCode.NoContent; |
| 253 | + } |
| 254 | + catch (ApiException e) |
| 255 | + { |
| 256 | + Logger?.LogError(e, $"{nameof(RotateSecret)} failed"); |
| 257 | + |
| 258 | + if (Throw) |
| 259 | + throw; |
| 260 | + |
| 261 | + return false; |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + public async Task<bool> RotateSecretAsync(string endpointId, OperationalWebhookEndpointSecretIn secret, |
| 266 | + string idempotencyKey = default, CancellationToken cancellationToken = default) |
| 267 | + { |
| 268 | + try |
| 269 | + { |
| 270 | + var lResponse = await _opWebhookEndpointApi.RotateOperationalWebhookEndpointSecretWithHttpInfoAsync( |
| 271 | + endpointId, |
| 272 | + secret, |
| 273 | + idempotencyKey); |
| 274 | + |
| 275 | + return lResponse.StatusCode == HttpStatusCode.NoContent; |
| 276 | + } |
| 277 | + catch (ApiException e) |
| 278 | + { |
| 279 | + Logger?.LogError(e, $"{nameof(RotateSecretAsync)} failed"); |
| 280 | + |
| 281 | + if (Throw) |
| 282 | + throw; |
| 283 | + |
| 284 | + return false; |
| 285 | + } |
| 286 | + } |
| 287 | + |
| 288 | + public OperationalWebhookEndpointOut Update(string endpointId, |
| 289 | + OperationalWebhookEndpointUpdate endpoint, string idempotencyKey = default) |
| 290 | + { |
| 291 | + try |
| 292 | + { |
| 293 | + var lEndpoint = _opWebhookEndpointApi.UpdateOperationalWebhookEndpoint( |
| 294 | + endpointId, |
| 295 | + endpoint); |
| 296 | + |
| 297 | + return lEndpoint; |
| 298 | + } |
| 299 | + catch (ApiException e) |
| 300 | + { |
| 301 | + Logger?.LogError(e, $"{nameof(Update)} failed"); |
| 302 | + |
| 303 | + if (Throw) |
| 304 | + throw; |
| 305 | + |
| 306 | + return null; |
| 307 | + } |
| 308 | + } |
| 309 | + |
| 310 | + public async Task<OperationalWebhookEndpointOut> UpdateAsync(string endpointId, |
| 311 | + OperationalWebhookEndpointUpdate endpoint, string idempotencyKey = default, |
| 312 | + CancellationToken cancellationToken = default) |
| 313 | + { |
| 314 | + try |
| 315 | + { |
| 316 | + var lEndpoint = await _opWebhookEndpointApi.UpdateOperationalWebhookEndpointAsync( |
| 317 | + endpointId, |
| 318 | + endpoint, |
| 319 | + cancellationToken); |
| 320 | + |
| 321 | + return lEndpoint; |
| 322 | + } |
| 323 | + catch (ApiException e) |
| 324 | + { |
| 325 | + Logger?.LogError(e, $"{nameof(UpdateAsync)} failed"); |
| 326 | + |
| 327 | + if (Throw) |
| 328 | + throw; |
| 329 | + |
| 330 | + return null; |
| 331 | + } |
| 332 | + } |
| 333 | + } |
| 334 | +} |
0 commit comments