-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DEVEXP-380: Support plugins addition in SinchClient parameters (#52)
- Loading branch information
1 parent
95c6ecc
commit 65a675d
Showing
22 changed files
with
478 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 87 additions & 37 deletions
124
packages/sdk-client/src/api/api-client-options-helper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,87 @@ | ||
// import { ApiTokenRequest, Oauth2TokenRequest, SigningRequest, XTimestampRequest } from '../plugins'; | ||
// // import { ApiClientOptions } from './api-client-options'; | ||
// | ||
// export const buildApiClientOptionsForProjectId = ( | ||
// projectId: string, | ||
// keyId: string, | ||
// keySecret: string, | ||
// ) => { | ||
// return { | ||
// projectId, | ||
// requestPlugins: [new Oauth2TokenRequest(keyId, keySecret)], | ||
// useServicePlanId: false, | ||
// }; | ||
// }; | ||
// | ||
// export const buildApiClientOptionForServicePlanId = ( | ||
// servicePlanId: string, | ||
// apiToken: string, | ||
// ) => { | ||
// return { | ||
// projectId: servicePlanId, | ||
// requestPlugins: [new ApiTokenRequest(apiToken)], | ||
// useServicePlanId: true, | ||
// }; | ||
// }; | ||
// | ||
// export const buildApiClientOptionForApplication = ( | ||
// applicationKey: string, | ||
// applicationSecret: string, | ||
// ) => { | ||
// return { | ||
// requestPlugins: [ | ||
// new XTimestampRequest(), | ||
// new SigningRequest(applicationKey, applicationSecret), | ||
// ], | ||
// }; | ||
// }; | ||
import { Region, SinchClientParameters } from '../domain'; | ||
import { ApiClientOptions } from './api-client-options'; | ||
import { | ||
ApiTokenRequest, | ||
Oauth2TokenRequest, | ||
SigningRequest, | ||
XTimestampRequest, | ||
} from '../plugins'; | ||
|
||
export const buildOAuth2ApiClientOptions = (params: SinchClientParameters, apiName: string): ApiClientOptions => { | ||
if (!params.projectId || !params.keyId || !params.keySecret) { | ||
throw new Error(`Invalid configuration for the ${apiName} API: "projectId", "keyId" and "keySecret" values must be provided`); | ||
} | ||
const apiClientOptions: ApiClientOptions = { | ||
projectId: params.projectId, | ||
requestPlugins: [new Oauth2TokenRequest(params.keyId, params.keySecret, params.authHostname)], | ||
useServicePlanId: false, | ||
}; | ||
addPlugins(apiClientOptions, params); | ||
return apiClientOptions; | ||
}; | ||
|
||
export const buildApplicationSignedApiClientOptions = ( | ||
params: SinchClientParameters, apiName: string, | ||
): ApiClientOptions => { | ||
if (!params.applicationKey || !params.applicationSecret) { | ||
throw new Error(`Invalid configuration for the ${apiName} API: "applicationKey" and "applicationSecret" values must be provided`); | ||
} | ||
const apiClientOptions: ApiClientOptions = { | ||
requestPlugins: [ | ||
new XTimestampRequest(), | ||
new SigningRequest(params.applicationKey, params.applicationSecret), | ||
], | ||
}; | ||
addPlugins(apiClientOptions, params); | ||
return apiClientOptions; | ||
}; | ||
|
||
export const buildFlexibleOAuth2OrApiTokenApiClientOptions = ( | ||
params: SinchClientParameters, region: Region, apiName: string, | ||
): ApiClientOptions => { | ||
let apiClientOptions: ApiClientOptions | undefined; | ||
// Check the region: if US or EU, try to use the OAuth2 authentication with the access key / secret under the project Id | ||
if (!params.forceServicePlanIdUsageForSmsApi | ||
&& (region === Region.UNITED_STATES || region === Region.EUROPE)) { | ||
// Let's check the required parameters for OAuth2 authentication | ||
if (params.projectId && params.keyId && params.keySecret) { | ||
apiClientOptions = { | ||
projectId: params.projectId, | ||
requestPlugins: [new Oauth2TokenRequest(params.keyId, params.keySecret, params.authHostname)], | ||
useServicePlanId: false, | ||
}; | ||
} | ||
} | ||
if (!apiClientOptions) { | ||
// The API client options couldn't be initialized for with the projectId unified authentication. | ||
// Let's try with the servicePlanId | ||
if (params.servicePlanId && params.apiToken) { | ||
apiClientOptions = { | ||
projectId: params.servicePlanId, | ||
requestPlugins: [new ApiTokenRequest(params.apiToken)], | ||
useServicePlanId: true, | ||
}; | ||
} | ||
} | ||
if (!apiClientOptions) { | ||
throw new Error(`Invalid parameters for the ${apiName} API: check your configuration`); | ||
} | ||
addPlugins(apiClientOptions, params); | ||
return apiClientOptions; | ||
}; | ||
|
||
const addPlugins = (apiClientOptions: ApiClientOptions, params: SinchClientParameters) => { | ||
if (params.requestPlugins && params.requestPlugins.length > 0) { | ||
if (!apiClientOptions.requestPlugins) { | ||
apiClientOptions.requestPlugins = []; | ||
} | ||
apiClientOptions.requestPlugins.push(...params.requestPlugins); | ||
} | ||
if (params.responsePlugins && params.responsePlugins.length > 0) { | ||
if (!apiClientOptions.responsePlugins) { | ||
apiClientOptions.responsePlugins = []; | ||
} | ||
apiClientOptions.responsePlugins.push(...params.responsePlugins); | ||
} | ||
return apiClientOptions; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,28 @@ | ||
export * from './api-client-options'; | ||
// export * from './api-client-options-helper'; | ||
export * from './api-client'; | ||
export * from './api-errors'; | ||
export * from './api-interface'; | ||
export * from './callback-webhooks-interface'; | ||
export { | ||
ApiClientOptions, | ||
} from './api-client-options'; | ||
export { | ||
buildOAuth2ApiClientOptions, | ||
buildApplicationSignedApiClientOptions, | ||
buildFlexibleOAuth2OrApiTokenApiClientOptions, | ||
} from './api-client-options-helper'; | ||
export { | ||
ApiClient, | ||
ApiListPromise, | ||
FileBuffer, | ||
PageResult, | ||
PaginatedApiProperties, | ||
PaginationEnum, | ||
} from './api-client'; | ||
export { | ||
GenericError, | ||
RequestFailedError, | ||
EmptyResponseError, | ||
ResponseJSONParseError, | ||
} from './api-errors'; | ||
export { | ||
Api, | ||
} from './api-interface'; | ||
export { | ||
CallbackProcessor, | ||
} from './callback-webhooks-interface'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/sdk-client/src/plugins/exception/exception.response.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.