Skip to content

Commit

Permalink
feat: add png rendering action to the Metabase piece
Browse files Browse the repository at this point in the history
  • Loading branch information
valentin-mourtialon committed Feb 5, 2025
1 parent 45daf4e commit 1ffcdcb
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class AxiosHttpClient extends BaseHttpClient {
const axiosRequestMethod = this.getAxiosRequestMethod(request.method);
const timeout = request.timeout ? request.timeout : 0;
const queryParams = request.queryParams || {}
const responseType = request.responseType || 'json';

for (const [key, value] of urlQueryParams) {
queryParams[key] = value
Expand All @@ -40,6 +41,7 @@ export class AxiosHttpClient extends BaseHttpClient {
headers,
data: request.body,
timeout,
responseType,
};

if (request.retries && request.retries > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export type HttpRequest<RequestBody extends HttpRequestBody = any> = {
queryParams?: QueryParams | undefined;
timeout?: number;
retries?: number;
responseType?: 'arraybuffer' | 'json' | 'blob' | 'text';
};
2 changes: 1 addition & 1 deletion packages/pieces/community/metabase/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "@activepieces/piece-metabase",
"version": "0.1.2"
"version": "0.1.3"
}
5 changes: 3 additions & 2 deletions packages/pieces/community/metabase/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Property,
} from '@activepieces/pieces-framework';
import { getQuestion } from './lib/actions/get-question';
import { getQuestionPngPreview } from './lib/actions/get-png-rendering';
import { queryMetabaseApi } from './lib/common';
import { HttpMethod } from '@activepieces/pieces-common';

Expand Down Expand Up @@ -50,7 +51,7 @@ export const metabase = createPiece({
auth: metabaseAuth,
minimumSupportedRelease: '0.30.0',
logoUrl: 'https://cdn.activepieces.com/pieces/metabase.png',
authors: ['AdamSelene', 'abuaboud'],
actions: [getQuestion],
authors: ['AdamSelene', 'abuaboud', 'valentin-mourtialon'],
actions: [getQuestion, getQuestionPngPreview],
triggers: [],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { createAction, Property } from '@activepieces/pieces-framework';
import { metabaseAuth } from '../..';
import { queryMetabaseApi } from '../common';
import { HttpMethod } from '@activepieces/pieces-common';

export const getQuestionPngPreview = createAction({
name: 'getQuestionPngPreview',
auth: metabaseAuth,
requireAuth: true,
displayName: 'Get Question PNG Preview',
description:
'Get PNG preview rendering (low resolution) of a Metabase card/question.',
props: {
questionId: Property.ShortText({
displayName: 'Metabase question ID',
required: true,
}),
},
async run({ auth, propsValue, files }) {
const questionId = propsValue.questionId.split('-')[0];

const response = await queryMetabaseApi(
{
endpoint: `pulse/preview_card_png/${questionId}`,
method: HttpMethod.GET,
headers: {
Accept: 'image/png',
},
responseType: 'arraybuffer',
},
auth
);

if (response.error) {
throw new Error(response.error);
}

const fileUrl = await files.write({
fileName: `metabase_question_${questionId}.png`,
data: Buffer.from(response, 'base64'),
});

return {
fileName: `metabase_question_${questionId}.png`,
file: fileUrl,
};
},
});
2 changes: 2 additions & 0 deletions packages/pieces/community/metabase/src/lib/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export async function queryMetabaseApi(
queryParams?: QueryParams;
headers?: HttpHeaders;
body?: object;
responseType?: 'arraybuffer' | 'json' | 'blob' | 'text';
},
auth: StaticPropsValue<CustomAuthProps>
) {
Expand All @@ -30,6 +31,7 @@ export async function queryMetabaseApi(
'X-API-KEY': auth.apiKey as string,
},
body: JSON.stringify(params.body),
responseType: params.responseType,
};
const response = await httpClient.sendRequest(request);
return response.body;
Expand Down

0 comments on commit 1ffcdcb

Please sign in to comment.