Skip to content

Commit

Permalink
DEVEXP-379: Support hostname override in SinchClient parameters (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
asein-sinch authored Apr 5, 2024
1 parent 5a27af8 commit 95c6ecc
Show file tree
Hide file tree
Showing 69 changed files with 972 additions and 407 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@typescript-eslint/no-var-requires": "off",
"semi": "warn",
"comma-dangle": "warn",
"quotes": ["warn", "single"],
"jest-extended/prefer-to-be-true": "warn",
"jest-extended/prefer-to-be-false": "error",
"jest-formatting/padding-around-describe-blocks": 2,
Expand Down
20 changes: 10 additions & 10 deletions packages/conversation/src/rest/v1/app/app-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ export class AppApi extends ConversationDomainApi {
};

const body: RequestBody = data['appCreateRequestBody'] ? JSON.stringify(data['appCreateRequestBody']) : '{}';
const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/apps`;
const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/apps`;

const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined);
const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams);
const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams);

return this.client.processCall<AppResponse>({
url,
Expand All @@ -85,11 +85,11 @@ export class AppApi extends ConversationDomainApi {
};

const body: RequestBody = '';
const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/apps/${data['app_id']}`;
const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/apps/${data['app_id']}`;

const requestOptions
= await this.client.prepareOptions(basePathUrl, 'DELETE', getParams, headers, body || undefined);
const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams);
const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams);

return this.client.processCall<any>({
url,
Expand All @@ -113,10 +113,10 @@ export class AppApi extends ConversationDomainApi {
};

const body: RequestBody = '';
const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/apps/${data['app_id']}`;
const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/apps/${data['app_id']}`;

const requestOptions = await this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined);
const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams);
const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams);

return this.client.processCall<AppResponse>({
url,
Expand All @@ -140,10 +140,10 @@ export class AppApi extends ConversationDomainApi {
};

const body: RequestBody = '';
const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/apps`;
const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/apps`;

const requestOptions = await this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined);
const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams);
const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams);

return this.client.processCall<ListAppsResponse>({
url,
Expand All @@ -167,11 +167,11 @@ export class AppApi extends ConversationDomainApi {
};

const body: RequestBody = data['appUpdateRequestBody'] ? JSON.stringify(data['appUpdateRequestBody']) : '{}';
const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/apps/${data['app_id']}`;
const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/apps/${data['app_id']}`;

const requestOptions
= await this.client.prepareOptions(basePathUrl, 'PATCH', getParams, headers, body || undefined);
const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams);
const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams);

return this.client.processCall<AppResponse>({
url,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ export class CapabilityApi extends ConversationDomainApi {
const body: RequestBody = data['lookupCapabilityRequestBody']
? JSON.stringify(data['lookupCapabilityRequestBody'])
: '{}';
const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/capability:query`;
const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/capability:query`;

const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined);
const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams);
const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams);

return this.client.processCall<LookupCapabilityResponse>({
url,
Expand Down
26 changes: 13 additions & 13 deletions packages/conversation/src/rest/v1/contact/contact-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ export class ContactApi extends ConversationDomainApi {

const body: RequestBody
= data['contactCreateRequestBody'] ? JSON.stringify(data['contactCreateRequestBody']) : '{}';
const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/contacts`;
const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/contacts`;

const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined);
const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams);
const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams);

return this.client.processCall<Contact>({
url,
Expand All @@ -114,11 +114,11 @@ export class ContactApi extends ConversationDomainApi {
};

const body: RequestBody = '';
const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/contacts/${data['contact_id']}`;
const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/contacts/${data['contact_id']}`;

const requestOptions
= await this.client.prepareOptions(basePathUrl, 'DELETE', getParams, headers, body || undefined);
const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams);
const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams);

return this.client.processCall<any>({
url,
Expand All @@ -145,10 +145,10 @@ export class ContactApi extends ConversationDomainApi {

const body: RequestBody
= data['getChannelProfileRequestBody'] ? JSON.stringify(data['getChannelProfileRequestBody']) : '{}';
const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/contacts:getChannelProfile`;
const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/contacts:getChannelProfile`;

const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined);
const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams);
const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams);

return this.client.processCall<GetChannelProfileResponse>({
url,
Expand All @@ -174,10 +174,10 @@ export class ContactApi extends ConversationDomainApi {
};

const body: RequestBody = '';
const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/contacts/${data['contact_id']}`;
const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/contacts/${data['contact_id']}`;

const requestOptions = await this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined);
const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams);
const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams);

return this.client.processCall<Contact>({
url,
Expand All @@ -204,7 +204,7 @@ export class ContactApi extends ConversationDomainApi {
};

const body: RequestBody = '';
const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/contacts`;
const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/contacts`;

const requestOptionsPromise = this.client.prepareOptions(basePathUrl, 'GET', getParams, headers, body || undefined);

Expand Down Expand Up @@ -245,10 +245,10 @@ export class ContactApi extends ConversationDomainApi {
};

const body: RequestBody = data['mergeContactRequestBody'] ? JSON.stringify(data['mergeContactRequestBody']) : '{}';
const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/contacts/${data['destination_id']}:merge`;
const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/contacts/${data['destination_id']}:merge`;

const requestOptions = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined);
const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams);
const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams);

return this.client.processCall<Contact>({
url,
Expand All @@ -274,11 +274,11 @@ export class ContactApi extends ConversationDomainApi {
const body: RequestBody = data['updateContactRequestBody']
? JSON.stringify(data['updateContactRequestBody'])
: '{}';
const basePathUrl = `${this.client.apiClientOptions.basePath}/v1/projects/${this.client.apiClientOptions.projectId}/contacts/${data['contact_id']}`;
const basePathUrl = `${this.client.apiClientOptions.hostname}/v1/projects/${this.client.apiClientOptions.projectId}/contacts/${data['contact_id']}`;

const requestOptions
= await this.client.prepareOptions(basePathUrl, 'PATCH', getParams, headers, body || undefined);
const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams);
const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams);

return this.client.processCall<Contact>({
url,
Expand Down
20 changes: 10 additions & 10 deletions packages/conversation/src/rest/v1/conversation-domain-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ export class ConversationDomainApi implements Api {
}

/**
* Update the default basePath for the API
* @param {string} basePath - The new base path to use for the APIs.
* Update the default hostname for the API
* @param {string} hostname - The new hostname to use for the APIs.
*/
public setBasePath(basePath: string) {
public setHostname(hostname: string) {
try {
this.client = this.getSinchClient();
this.client.apiClientOptions.basePath = basePath;
this.client.apiClientOptions.hostname = hostname;
} catch (error) {
console.error('Impossible to set a new base path, the credentials need to be provided first.');
console.error('Impossible to set a new hostname, the credentials need to be provided first.');
throw error;
}
}
Expand All @@ -41,7 +41,7 @@ export class ConversationDomainApi implements Api {
public setRegion(region: Region) {
this.sinchClientParameters.region = region;
if (this.client) {
this.client.apiClientOptions.basePath = this.buildBasePath(region);
this.client.apiClientOptions.hostname = this.buildHostname(region);
}
}

Expand Down Expand Up @@ -83,7 +83,7 @@ export class ConversationDomainApi implements Api {
}
const apiClientOptions = this.buildApiClientOptions(this.sinchClientParameters);
this.client = new ApiFetchClient(apiClientOptions);
this.client.apiClientOptions.basePath = this.buildBasePath(region);
this.client.apiClientOptions.hostname = this.buildHostname(region);
}
return this.client;
}
Expand All @@ -100,13 +100,13 @@ export class ConversationDomainApi implements Api {
};
}

private buildBasePath(region: Region) {
private buildHostname(region: Region) {
switch (this.apiName) {
case 'TemplatesV1Api':
case 'TemplatesV2Api':
return `https://${region}.template.api.sinch.com`;
return this.sinchClientParameters.conversationTemplatesHostname ?? `https://${region}.template.api.sinch.com`;
default:
return `https://${region}.conversation.api.sinch.com`;
return this.sinchClientParameters.conversationHostname ?? `https://${region}.conversation.api.sinch.com`;
}
}

Expand Down
34 changes: 20 additions & 14 deletions packages/conversation/src/rest/v1/conversation-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,26 @@ export class ConversationService {
}

/**
* Update the default basePath for each API
*
* @param {string} basePath - The new base path to use for all the APIs.
* Update the default hostname for each API (except Templates)
* @param {string} hostname - The new hostname to use for all the APIs.
*/
public setBasePath(basePath: string) {
this.contact.setBasePath(basePath);
this.app.setBasePath(basePath);
this.events.setBasePath(basePath);
this.messages.setBasePath(basePath);
this.transcoding.setBasePath(basePath);
this.capability.setBasePath(basePath);
this.conversation.setBasePath(basePath);
this.webhooks.setBasePath(basePath);
this.templatesV1.setBasePath(basePath);
this.templatesV2.setBasePath(basePath);
public setHostname(hostname: string) {
this.contact.setHostname(hostname);
this.app.setHostname(hostname);
this.events.setHostname(hostname);
this.messages.setHostname(hostname);
this.transcoding.setHostname(hostname);
this.capability.setHostname(hostname);
this.conversation.setHostname(hostname);
this.webhooks.setHostname(hostname);
}

/**
* Update the default hostname for the Templates API
* @param {string} hostname - The new hostname to use for all the APIs.
*/
public setTemplatesHostname(hostname: string) {
this.templatesV1.setHostname(hostname);
this.templatesV2.setHostname(hostname);
}
}
Loading

0 comments on commit 95c6ecc

Please sign in to comment.