Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apitraffic/develop #1

Merged
merged 3 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions packages/pieces/community/apitraffic/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"extends": [
"../../../../.eslintrc.base.json"
],
"ignorePatterns": [
"!**/*"
],
"overrides": [
{
"files": [
"*.ts",
"*.tsx",
"*.js",
"*.jsx"
],
"rules": {}
},
{
"files": [
"*.ts",
"*.tsx"
],
"rules": {}
},
{
"files": [
"*.js",
"*.jsx"
],
"rules": {}
}
]
}
7 changes: 7 additions & 0 deletions packages/pieces/community/apitraffic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# pieces-apitraffic

This library was generated with [Nx](https://nx.dev).

## Building

Run `nx build pieces-apitraffic` to build the library.
4 changes: 4 additions & 0 deletions packages/pieces/community/apitraffic/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "@apitraffic/piece-apitraffic",
"version": "0.0.12"
}
45 changes: 45 additions & 0 deletions packages/pieces/community/apitraffic/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "pieces-apitraffic",
"$schema": "../../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/pieces/community/apitraffic/src",
"projectType": "library",
"release": {
"version": {
"generatorOptions": {
"packageRoot": "dist/{projectRoot}",
"currentVersionResolver": "git-tag"
}
}
},
"tags": [],
"targets": {
"build": {
"executor": "@nx/js:tsc",
"outputs": [
"{options.outputPath}"
],
"options": {
"outputPath": "dist/packages/pieces/community/apitraffic",
"tsConfig": "packages/pieces/community/apitraffic/tsconfig.lib.json",
"packageJson": "packages/pieces/community/apitraffic/package.json",
"main": "packages/pieces/community/apitraffic/src/index.ts",
"assets": [
"packages/pieces/community/apitraffic/*.md"
],
"buildableProjectDepsInPackageJsonType": "dependencies",
"updateBuildableProjectDepsInPackageJson": true
}
},
"nx-release-publish": {
"options": {
"packageRoot": "dist/{projectRoot}"
}
},
"lint": {
"executor": "@nx/eslint:lint",
"outputs": [
"{options.outputFile}"
]
}
}
}
18 changes: 18 additions & 0 deletions packages/pieces/community/apitraffic/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

import { createPiece } from "@activepieces/pieces-framework";
import { PieceCategory } from '@activepieces/shared';

import { streamViewMatch } from './lib/triggers/stream-view-match';
import { apiTrafficAuth } from './lib/auth';

export const apitraffic = createPiece({
displayName: "ApiTraffic",
description : "",
auth: apiTrafficAuth,
minimumSupportedRelease: '0.20.0',
logoUrl: "https://cdn.apitraffic.io/images/icon-color-140x140.svg",
categories: [PieceCategory.DEVELOPER_TOOLS],
authors: ["jasonfill"],
actions: [],
triggers: [streamViewMatch],
});
104 changes: 104 additions & 0 deletions packages/pieces/community/apitraffic/src/lib/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@

import {
httpClient,
HttpMethod,
HttpRequest,
} from '@activepieces/pieces-common';

import { ApiTrafficAuthType, GetBucketsResponse} from './common/types';

const apiTrafficAPI = async (
auth: ApiTrafficAuthType,
method: HttpMethod,
api: string,
body: object = {}
) => {

const { apiToken } = auth;

let host = "api.apitraffic.io";

const baseUrl = `https://${host}/v1`;

const request: HttpRequest = {
method: method,
url: `${baseUrl}${api}`,
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiToken}`
}
};

if(method === HttpMethod.POST || method === HttpMethod.PUT){
request.body = body;
}

const response = await httpClient.sendRequest(request);

let data = {};
let success = false;

// If the response is a JSON object, then we know that the request was successful
if (typeof response.body === 'object') {
data = response.body;
console.log(data);
success = true;
}

return {
success: success,
data: data,
};
};

export async function getUser(auth: ApiTrafficAuthType) {
const api = '/authentication/verify';

const apiReply = await apiTrafficAPI(auth, HttpMethod.GET, api, auth);
const response = {success: false, data: {}};

if(apiReply.success){
response.success = true;
response.data = apiReply.data;
}

return response;

}

export async function getAccounts(auth: ApiTrafficAuthType) {
const api = '/accounts';
return await apiTrafficAPI(auth, HttpMethod.GET, api, auth);
}

export async function getBuckets(auth: ApiTrafficAuthType): Promise<GetBucketsResponse> {
const api = `/accounts/${auth.accountSid}/buckets`;
const apiReply = await apiTrafficAPI(auth, HttpMethod.GET, api);
const response = {success: false, data: []};

if(apiReply.success){
response.success = true;
response.data = [];
}

return response;
}

export async function getStreamViews(auth: ApiTrafficAuthType, bucketSid: string) {
const api = `/accounts/${auth.accountSid}/buckets/${bucketSid}`;
return await apiTrafficAPI(auth, HttpMethod.GET, api, auth);
}
export async function updateWebhook(auth: ApiTrafficAuthType, url: string, enabled: boolean) {
const api = `/webhooks`;

const segments = url.split('/');
const externalWorkflowId = segments.pop();

const body = {
externalWorkflowId,
url,
enabled
};

return await apiTrafficAPI(auth, HttpMethod.PUT, api, body);
}
50 changes: 50 additions & 0 deletions packages/pieces/community/apitraffic/src/lib/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

import { PieceAuth } from "@activepieces/pieces-framework";
import { getUser } from "./api";
import { ApiTrafficAuthType } from './common/types';

const markdown = `
Obtain your ApiTraffic API Token
1. Login to your account at https://app.apitraffic.com
2. Click on your initials in the upper right, and select API Keys
3. Click on "New Token"
4. Copy the token and paste it in the field below
`;

export const apiTrafficAuth = PieceAuth.CustomAuth({
description: markdown,
props: {
apiToken: PieceAuth.SecretText({
displayName: 'API Token',
description: 'The API token for your ApiTraffic account',
required: true
})
},
validate: async ({ auth }) => {

try {
await validateAuth(auth);
return {
valid: true,
};
} catch (e) {
return {
valid: false,
error: (e as Error)?.message,
};
}
},
required: true
});

const validateAuth = async (auth: ApiTrafficAuthType) => {
const response = await getUser(auth);

if (response.success !== true) {
throw new Error(
`Authentication failed. Please check your domain and API key and try again.`
);
}else{
auth.accountSid = "acc_123";//response.data.defaultAccountSid;
}
};
33 changes: 33 additions & 0 deletions packages/pieces/community/apitraffic/src/lib/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Property } from '@activepieces/pieces-framework';
import { getBuckets } from '../api';
import { ApiTrafficAuthType } from './types';

export const bucketsDropdown = Property.Dropdown<string>({
displayName: 'Bucket',
description: 'Bucket',
required: true,
refreshers: [],
async options({ auth }) {

if (!auth) {
return {
disabled: true,
placeholder: 'Connect ApiTraffic account',
options: [],
};
}

const buckets = await getBuckets(auth as ApiTrafficAuthType);

const options = buckets.data.map((bucket) => ({
label: bucket.name,
value: bucket.sid,
}));

return {
disabled: false,
placeholder: 'Select bucket',
options,
};
},
});
32 changes: 32 additions & 0 deletions packages/pieces/community/apitraffic/src/lib/common/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export type ApiTrafficAuthType = {
accountSid?: string;
apiToken: string;
};

export type ApiResponseType = {
success: boolean;
data: object;
}

export type BucketType = {
sid: string;
accountSid: string;
name: string;
verifySslCerts: boolean;
appIsConnected: boolean;
createdAt: string;
};

export type UserType = {
sid: string;
firstName: string;
lastName: string;
defaultAccountSid: string;
createdAt?: string;
};



export type GetBucketsResponse = {success: boolean, data: BucketType[]};
export type GetUserResponse = {success: boolean, data: UserType};

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

import { createTrigger, TriggerStrategy } from '@activepieces/pieces-framework';
import { updateWebhook } from '../api';
import { apiTrafficAuth } from '../auth';

export const streamViewMatch = createTrigger({
auth: apiTrafficAuth,
name: 'streamViewMatch',
displayName: 'New Stream View Request',
description: 'Triggers when a request matches a stream view criteria.',
props: {},
sampleData: {},
type: TriggerStrategy.WEBHOOK,
async onEnable(context){
// implement webhook creation logic
const { auth, webhookUrl } = context;
//console.log('Start: ON ENABLE');
//console.log(store);
//console.log(auth);
//console.log(propsValue);
//console.log('End: ON ENABLE');

await updateWebhook(auth, webhookUrl, true);


},
async onDisable(context){
// implement webhook deletion logic
const { auth, webhookUrl } = context;
//console.log('Start: ON DISABLE');
//console.log(store);
//console.log(auth);
//console.log(propsValue);
//console.log('End: ON DISABLE');

await updateWebhook(auth, webhookUrl, false);
},
async run(context){
return [context.payload.body]
}
})
Loading
Loading