Skip to content

Commit 37e3dce

Browse files
DX-2469
1 parent e977ea7 commit 37e3dce

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

src/main/java/com/bandwidth/voice/controllers/APIController.java

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,137 @@ private ApiResponse<Void> handleModifyCallResponse(
463463
return new ApiResponse<Void>(response.getStatusCode(), response.getHeaders(), null);
464464
}
465465

466+
/**
467+
* Interrupts and replaces an active call's BXML document.
468+
* @param accountId Required parameter: Example:
469+
* @param callId Required parameter: Example:
470+
* @param body Required parameter: Example: Needs to be a valid xml string
471+
* @throws ApiException Represents error response from the server.
472+
* @throws IOException Signals that an I/O exception of some sort has occurred.
473+
*/
474+
public ApiResponse<Void> modifyCallBxml(
475+
final String accountId,
476+
final String callId,
477+
final String body) throws ApiException, IOException {
478+
HttpRequest request = buildModifyCallBxmlRequest(accountId, callId, body);
479+
authManagers.get("voice").apply(request);
480+
481+
HttpResponse response = getClientInstance().execute(request, false);
482+
HttpContext context = new HttpContext(request, response);
483+
484+
return handleModifyCallBxmlResponse(context);
485+
}
486+
487+
/**
488+
* Interrupts and replaces an active call's BXML document.
489+
* @param accountId Required parameter: Example:
490+
* @param callId Required parameter: Example:
491+
* @param body Required parameter: Example: valid xml string
492+
* @return Returns the Void wrapped in ApiResponse response from the API call
493+
*/
494+
public CompletableFuture<ApiResponse<Void>> modifyCallBxmlAsync(
495+
final String accountId,
496+
final String callId,
497+
final String body) {
498+
return makeHttpCallAsync(() -> buildModifyCallBxmlRequest(accountId, callId, body),
499+
req -> authManagers.get("voice").applyAsync(req)
500+
.thenCompose(request -> getClientInstance()
501+
.executeAsync(request, false)),
502+
context -> handleModifyCallBxmlResponse(context));
503+
}
504+
505+
/**
506+
* Builds the HttpRequest object for modifyCallBxml.
507+
*/
508+
private HttpRequest buildModifyCallBxmlRequest(
509+
final String accountId,
510+
final String callId,
511+
final String body) {
512+
//the base uri for api requests
513+
String baseUri = config.getBaseUri(Server.VOICEDEFAULT);
514+
515+
//prepare query string for API call
516+
final StringBuilder queryBuilder = new StringBuilder(baseUri
517+
+ "/api/v2/accounts/{accountId}/calls/{callId}/bxml");
518+
519+
//process template parameters
520+
Map<String, SimpleEntry<Object, Boolean>> templateParameters = new HashMap<>();
521+
templateParameters.put("accountId",
522+
new SimpleEntry<Object, Boolean>(accountId, false));
523+
templateParameters.put("callId",
524+
new SimpleEntry<Object, Boolean>(callId, false));
525+
ApiHelper.appendUrlWithTemplateParameters(queryBuilder, templateParameters);
526+
527+
//load all headers for the outgoing API request
528+
Headers headers = new Headers();
529+
headers.add("user-agent", BaseController.userAgent);
530+
headers.add("content-type", "application/xml");
531+
532+
//prepare and invoke the API call request to fetch the response
533+
HttpRequest request = getClientInstance().putBody(queryBuilder, headers, null, body);
534+
535+
// Invoke the callback before request if its not null
536+
if (getHttpCallback() != null) {
537+
getHttpCallback().onBeforeRequest(request);
538+
}
539+
540+
return request;
541+
}
542+
543+
/**
544+
* Processes the response for modifyCallBxml.
545+
* @return An object of type void
546+
*/
547+
private ApiResponse<Void> handleModifyCallBxmlResponse(
548+
HttpContext context) throws ApiException, IOException {
549+
HttpResponse response = context.getResponse();
550+
551+
//invoke the callback after response if its not null
552+
if (getHttpCallback() != null) {
553+
getHttpCallback().onAfterResponse(context);
554+
}
555+
556+
//Error handling using HTTP status codes
557+
int responseCode = response.getStatusCode();
558+
559+
if (responseCode == 400) {
560+
throw new ApiErrorException(
561+
"Something's not quite right... Your request is invalid. Please fix it before trying again.",
562+
context);
563+
}
564+
if (responseCode == 401) {
565+
throw new ApiException(
566+
"Your credentials are invalid. Please use your Bandwidth dashboard credentials to authenticate to the API.",
567+
context);
568+
}
569+
if (responseCode == 403) {
570+
throw new ApiErrorException("User unauthorized to perform this action.", context);
571+
}
572+
if (responseCode == 404) {
573+
throw new ApiErrorException(
574+
"The resource specified cannot be found or does not belong to you.", context);
575+
}
576+
if (responseCode == 415) {
577+
throw new ApiErrorException(
578+
"We don't support that media type. If a request body is required, please send it to us as `application/xml`.",
579+
context);
580+
}
581+
if (responseCode == 429) {
582+
throw new ApiErrorException(
583+
"You're sending requests to this endpoint too frequently. Please slow your request rate down and try again.",
584+
context);
585+
}
586+
if (responseCode == 500) {
587+
throw new ApiErrorException("Something unexpected happened. Please try again.",
588+
context);
589+
}
590+
//handle errors defined at the API level
591+
validateResponse(response, context);
592+
593+
return new ApiResponse<Void>(response.getStatusCode(), response.getHeaders(), null);
594+
}
595+
596+
466597
/**
467598
* Pauses or resumes a recording.
468599
* @param accountId Required parameter: Example:

0 commit comments

Comments
 (0)