11import crypto from "node:crypto" ;
2- import { fetchWithSsrFGuard } from "openclaw/plugin-sdk" ;
32import type { PlivoConfig , WebhookSecurityConfig } from "../config.js" ;
3+ import { getHeader } from "../http-headers.js" ;
44import type {
55 HangupCallInput ,
66 InitiateCallInput ,
@@ -17,6 +17,7 @@ import type {
1717import { escapeXml } from "../voice-mapping.js" ;
1818import { reconstructWebhookUrl , verifyPlivoWebhook } from "../webhook-security.js" ;
1919import type { VoiceCallProvider } from "./base.js" ;
20+ import { guardedJsonApiRequest } from "./shared/guarded-json-api.js" ;
2021
2122export interface PlivoProviderOptions {
2223 /** Override public URL origin for signature verification */
@@ -32,17 +33,6 @@ export interface PlivoProviderOptions {
3233type PendingSpeak = { text : string ; locale ?: string } ;
3334type PendingListen = { language ?: string } ;
3435
35- function getHeader (
36- headers : Record < string , string | string [ ] | undefined > ,
37- name : string ,
38- ) : string | undefined {
39- const value = headers [ name . toLowerCase ( ) ] ;
40- if ( Array . isArray ( value ) ) {
41- return value [ 0 ] ;
42- }
43- return value ;
44- }
45-
4636function createPlivoRequestDedupeKey ( ctx : WebhookContext ) : string {
4737 const nonceV3 = getHeader ( ctx . headers , "x-plivo-signature-v3-nonce" ) ;
4838 if ( nonceV3 ) {
@@ -96,33 +86,19 @@ export class PlivoProvider implements VoiceCallProvider {
9686 allowNotFound ?: boolean ;
9787 } ) : Promise < T > {
9888 const { method, endpoint, body, allowNotFound } = params ;
99- const { response , release } = await fetchWithSsrFGuard ( {
89+ return await guardedJsonApiRequest < T > ( {
10090 url : `${ this . baseUrl } ${ endpoint } ` ,
101- init : {
102- method,
103- headers : {
104- Authorization : `Basic ${ Buffer . from ( `${ this . authId } :${ this . authToken } ` ) . toString ( "base64" ) } ` ,
105- "Content-Type" : "application/json" ,
106- } ,
107- body : body ? JSON . stringify ( body ) : undefined ,
91+ method,
92+ headers : {
93+ Authorization : `Basic ${ Buffer . from ( `${ this . authId } :${ this . authToken } ` ) . toString ( "base64" ) } ` ,
94+ "Content-Type" : "application/json" ,
10895 } ,
109- policy : { allowedHostnames : [ this . apiHost ] } ,
96+ body,
97+ allowNotFound,
98+ allowedHostnames : [ this . apiHost ] ,
11099 auditContext : "voice-call.plivo.api" ,
100+ errorPrefix : "Plivo API error" ,
111101 } ) ;
112- try {
113- if ( ! response . ok ) {
114- if ( allowNotFound && response . status === 404 ) {
115- return undefined as T ;
116- }
117- const errorText = await response . text ( ) ;
118- throw new Error ( `Plivo API error: ${ response . status } ${ errorText } ` ) ;
119- }
120-
121- const text = await response . text ( ) ;
122- return text ? ( JSON . parse ( text ) as T ) : ( undefined as T ) ;
123- } finally {
124- await release ( ) ;
125- }
126102 }
127103
128104 verifyWebhook ( ctx : WebhookContext ) : WebhookVerificationResult {
0 commit comments