first commit
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
import type {
|
||||
IDataObject,
|
||||
IExecuteFunctions,
|
||||
ILoadOptionsFunctions,
|
||||
IHookFunctions,
|
||||
IWebhookFunctions,
|
||||
IHttpRequestMethods,
|
||||
IRequestOptions,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
function getAuthenticationTypeFromApiKey(data: string): 'accessToken' | 'apiKey' {
|
||||
// The access token is a JWT, so it will always include dots to separate
|
||||
// header, payoload and signature.
|
||||
return data.includes('.') ? 'accessToken' : 'apiKey';
|
||||
}
|
||||
|
||||
export async function getAuthenticationType(
|
||||
this: IExecuteFunctions | IWebhookFunctions | IHookFunctions | ILoadOptionsFunctions,
|
||||
): Promise<'accessToken' | 'apiKey'> {
|
||||
const authentication = this.getNodeParameter('authentication', 0) as string;
|
||||
if (authentication === 'apiKey') {
|
||||
const { apiKey } = await this.getCredentials<{ apiKey: string }>('calendlyApi');
|
||||
return getAuthenticationTypeFromApiKey(apiKey);
|
||||
} else {
|
||||
return 'accessToken';
|
||||
}
|
||||
}
|
||||
|
||||
export async function calendlyApiRequest(
|
||||
this: IExecuteFunctions | IWebhookFunctions | IHookFunctions | ILoadOptionsFunctions,
|
||||
method: IHttpRequestMethods,
|
||||
resource: string,
|
||||
|
||||
body: any = {},
|
||||
query: IDataObject = {},
|
||||
uri?: string,
|
||||
option: IDataObject = {},
|
||||
): Promise<any> {
|
||||
const authenticationType = await getAuthenticationType.call(this);
|
||||
|
||||
const headers: IDataObject = {
|
||||
'Content-Type': 'application/json',
|
||||
};
|
||||
|
||||
let endpoint = 'https://api.calendly.com';
|
||||
|
||||
// remove once API key is deprecated
|
||||
if (authenticationType === 'apiKey') {
|
||||
endpoint = 'https://calendly.com/api/v1';
|
||||
}
|
||||
|
||||
let options: IRequestOptions = {
|
||||
headers,
|
||||
method,
|
||||
body,
|
||||
qs: query,
|
||||
uri: uri || `${endpoint}${resource}`,
|
||||
json: true,
|
||||
};
|
||||
|
||||
if (!Object.keys(body as IDataObject).length) {
|
||||
delete options.form;
|
||||
}
|
||||
if (!Object.keys(query).length) {
|
||||
delete options.qs;
|
||||
}
|
||||
options = Object.assign({}, options, option);
|
||||
|
||||
const credentialsType =
|
||||
(this.getNodeParameter('authentication', 0) as string) === 'apiKey'
|
||||
? 'calendlyApi'
|
||||
: 'calendlyOAuth2Api';
|
||||
return await this.helpers.requestWithAuthentication.call(this, credentialsType, options);
|
||||
}
|
||||
Reference in New Issue
Block a user