private static string GetUrlSuffix(BaseAppInstruction instruction) { switch (instruction) { case NotificationInstruction _: return("/Notification"); case OtpInstruction _: return("/OTP"); case RequestDtmfInstruction _: return("/DTMF"); default: throw new NotImplementedException($"No known endpoint for sending a {instruction.GetType().Name} to the CM VoiceApi."); } }
/// <summary> /// Sends a notification instruction to CM. /// </summary> /// <param name="instruction">The instruction, containing all information on the call to send out.</param> /// <param name="cancellationToken">Token to cancel the operation.</param> public async Task <VoiceApiResult <CallQueuedEvent> > SendAsync(BaseAppInstruction instruction, CancellationToken cancellationToken = default) { var urlSuffix = GetUrlSuffix(instruction); using (var request = new HttpRequestMessage(HttpMethod.Post, _url + urlSuffix)) { request.Headers.Add(ApiKeyHeader, _apiKey.ToString()); request.Content = new StringContent(JsonSerializer.Serialize(Convert.ChangeType(instruction, instruction.GetType())), Encoding.UTF8, "application/json"); using (var result = await _httpClient.SendAsync(request, cancellationToken).ConfigureAwait(false)) { cancellationToken.ThrowIfCancellationRequested(); var reply = ""; if (result.Content != null) { reply = await result.Content.ReadAsStringAsync().ConfigureAwait(false); } return(new VoiceApiResult <CallQueuedEvent> { HttpStatusCode = result.StatusCode, Success = result.IsSuccessStatusCode, Content = reply }); } } }