2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2022-07-19 01:09:06 -07:00
|
|
|
IAuthenticateGeneric,
|
|
|
|
ICredentialDataDecryptedObject,
|
|
|
|
ICredentialTestRequest,
|
|
|
|
ICredentialType,
|
|
|
|
IHttpRequestHelper,
|
|
|
|
INodeProperties,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
export class MetabaseApi implements ICredentialType {
|
|
|
|
name = 'metabaseApi';
|
2022-12-02 12:54:28 -08:00
|
|
|
|
2022-07-19 01:09:06 -07:00
|
|
|
displayName = 'Metabase API';
|
2022-12-02 12:54:28 -08:00
|
|
|
|
2022-07-19 01:09:06 -07:00
|
|
|
documentationUrl = 'metabase';
|
2022-12-02 12:54:28 -08:00
|
|
|
|
2022-07-19 01:09:06 -07:00
|
|
|
properties: INodeProperties[] = [
|
|
|
|
{
|
|
|
|
displayName: 'Session Token',
|
|
|
|
name: 'sessionToken',
|
|
|
|
type: 'hidden',
|
2023-08-09 01:10:08 -07:00
|
|
|
|
2022-07-19 01:09:06 -07:00
|
|
|
typeOptions: {
|
|
|
|
expirable: true,
|
|
|
|
},
|
|
|
|
default: '',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'URL',
|
|
|
|
name: 'url',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Username',
|
|
|
|
name: 'username',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Password',
|
|
|
|
name: 'password',
|
|
|
|
type: 'string',
|
|
|
|
typeOptions: {
|
|
|
|
password: true,
|
|
|
|
},
|
|
|
|
default: '',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
// method will only be called if "sessionToken" (the expirable property)
|
|
|
|
// is empty or is expired
|
|
|
|
async preAuthentication(this: IHttpRequestHelper, credentials: ICredentialDataDecryptedObject) {
|
|
|
|
// make reques to get session token
|
|
|
|
const url = credentials.url as string;
|
|
|
|
const { id } = (await this.helpers.httpRequest({
|
|
|
|
method: 'POST',
|
|
|
|
url: `${url.endsWith('/') ? url.slice(0, -1) : url}/api/session`,
|
|
|
|
body: {
|
|
|
|
username: credentials.username,
|
|
|
|
password: credentials.password,
|
|
|
|
},
|
|
|
|
})) as { id: string };
|
|
|
|
return { sessionToken: id };
|
|
|
|
}
|
2022-12-02 12:54:28 -08:00
|
|
|
|
2022-07-19 01:09:06 -07:00
|
|
|
authenticate: IAuthenticateGeneric = {
|
|
|
|
type: 'generic',
|
|
|
|
properties: {
|
|
|
|
headers: {
|
|
|
|
'X-Metabase-Session': '={{$credentials.sessionToken}}',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2022-12-02 12:54:28 -08:00
|
|
|
|
2022-07-19 01:09:06 -07:00
|
|
|
test: ICredentialTestRequest = {
|
|
|
|
request: {
|
|
|
|
baseURL: '={{$credentials?.url}}',
|
|
|
|
url: '/api/user/current',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|